Skip to content

Commit

Permalink
Deprecate Procfile and move to dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsmie committed Nov 28, 2023
1 parent 6b8b551 commit 807c5ba
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 71 deletions.
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM python:3.11-bookworm as builder

RUN pip install poetry==1.7.1

ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache

WORKDIR /app

COPY pyproject.toml poetry.lock ./
RUN touch README.md

RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --without dev --no-root

FROM python:3.11-slim-bookworm as runtime

ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH"

RUN mkdir /data

COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}

COPY src/rollbot ./rollbot

ENTRYPOINT ["python", "-m", "rollbot"]
27 changes: 6 additions & 21 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ packages = [
]

[tool.poetry.dependencies]
python = "==3.11.6"
py-cord = {extras = ["speed"], version="*"}
numpy = "^1.22.1"
matplotlib = "^3.5.1"
lark = "*"
redis = "^5"
python = "~3.11"
py-cord = {extras = ["speed"], version="^2"}
numpy = "^1"
matplotlib = "^3"
lark = "^1"

[tool.poetry.group.dev.dependencies]
ruff = "^0.1.6"
Expand Down
18 changes: 18 additions & 0 deletions src/rollbot/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import logging
import os

from rollbot.bot import bot
from rollbot.db import SQLiteDB
from rollbot.varenv import var_env_provider


logging.basicConfig(
level=logging.INFO,
format="[%(levelname)s] %(asctime)s - %(message)s",
)
token = os.environ["DISCORD_TOKEN"]
db = SQLiteDB(os.environ["SQLITE_PATH"])
var_env_provider.set_db(db)

bot.run(token)
db.close()
30 changes: 0 additions & 30 deletions src/rollbot/config.py

This file was deleted.

41 changes: 36 additions & 5 deletions src/rollbot/db.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
import redis
from urllib.parse import urlparse
import sqlite3
import json


def make_db(url):
url = urlparse(url)
return redis.Redis(host=url.hostname, port=url.port, password=url.password)
class SQLiteDB:
def __init__(self, database_url: str):
self.conn = sqlite3.connect(database_url)
self.cursor = self.conn.cursor()

# Create a table for key-value pairs if it doesn't exist
self.cursor.execute(
"""
CREATE TABLE IF NOT EXISTS key_value (
key TEXT PRIMARY KEY,
value TEXT
)
"""
)
self.conn.commit()

def get(self, key: str) -> dict[str, str]:
self.cursor.execute("SELECT value FROM key_value WHERE key = ?", (key,))
result = self.cursor.fetchone()
if result is not None:
return json.loads(result[0])
else:
return {}

def set(self, key: str, value: dict[str, str]) -> None:
serialized_value = json.dumps(value)
self.cursor.execute(
"INSERT OR REPLACE INTO key_value (key, value) VALUES (?, ?)",
(key, serialized_value),
)
self.conn.commit()

def close(self):
self.conn.close()
17 changes: 8 additions & 9 deletions src/rollbot/varenv/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Optional, Dict, Any
import json
from rollbot.db import SQLiteDB


class VarEnv:
def __init__(self, name: str, items: Dict[str, str] = None):
def __init__(self, name: str, items: dict[str, str] = None) -> None:
self.dirty = False
self.name = name
self.items = items or {}
Expand All @@ -15,28 +14,28 @@ def set(self, key: str, value: str) -> None:
self.items[key] = value
self.dirty = True

def get(self, key: str) -> Optional[str]:
def get(self, key: str) -> str | None:
return self.items.get(key)


class VarEnvProvider:
def __init__(self):
self.db: Any = None
self.db: SQLiteDB | None = None

def set_db(self, db):
self.db = db

def get(self, name: str) -> VarEnv:
if not self.db:
return VarEnv(name)
data = self.db.get(f"VarEnv[{name}]")
data = self.db.get(name)
if not data:
return VarEnv(name)
return VarEnv(name, json.loads(data))
return VarEnv(name, data)

def update(self, varenv: VarEnv):
def update(self, varenv: VarEnv) -> None:
if varenv.dirty and self.db is not None:
self.db.set(f"VarEnv[{varenv.name}]", json.dumps(varenv.items))
self.db.set(varenv.name, varenv.items)


var_env_provider = VarEnvProvider()

0 comments on commit 807c5ba

Please sign in to comment.