diff --git a/app/database.py b/app/database.py index af99fa6..f8b29a1 100644 --- a/app/database.py +++ b/app/database.py @@ -25,9 +25,15 @@ class Database: ) def init(self) -> typing.Self: - self.logger.info("Initializing database") - tortoise.run_async(tortoise.Tortoise.init(config=self.config)) self.logger.info("Migrating database") tortoise.run_async(tortoise.migrations.api.migrate(config=self.config)) + self.logger.info("Initializing database") + tortoise.run_async( + tortoise.Tortoise.init(config=self.config, _enable_global_fallback=True) + ) self.logger.info("Database ready") return self + + def __enter__(self) -> typing.Self: + + return self diff --git a/app/server.py b/app/server.py index 3bb149c..060b8ea 100644 --- a/app/server.py +++ b/app/server.py @@ -1,9 +1,11 @@ +import logging import typing import flask import gunicorn.app.base from app import PKG_NAME +from app.models.comment import Comment if typing.TYPE_CHECKING: from app.params import Parameters @@ -21,6 +23,7 @@ class Server(gunicorn.app.base.BaseApplication): PKG_NAME, static_folder="dist/assets", template_folder="dist" ) self._index_content: str | None = None + self.logger: logging.Logger = logging.getLogger(self.__class__.__name__) super().__init__() def register_routes(self) -> typing.Self: @@ -28,6 +31,16 @@ class Server(gunicorn.app.base.BaseApplication): def index() -> str: return flask.render_template("index.html") + @self.app.route("/api/comments", methods=["GET"]) + async def get_comments() -> list: + return await Comment.all().order_by("created_at").values() + + @self.app.route("/api/comments", methods=["POST"]) + async def post_comment() -> tuple[str, int]: + data = flask.request.get_json() + await Comment.create(content=data["content"]) + return ("", 204) + return self @typing.override @@ -48,5 +61,10 @@ class Server(gunicorn.app.base.BaseApplication): self.run() else: self.app.run( - host=self.bind, port=self.port, debug=self.debug, load_dotenv=False + host=self.bind, + port=self.port, + debug=self.debug, + load_dotenv=False, + use_evalex=False, + use_reloader=False, ) diff --git a/pyproject.toml b/pyproject.toml index ccfaa8a..409ef1c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ description = "" readme = "README.md" requires-python = ">=3.14" dependencies = [ - "flask>=3.1.3,<4.0.0", + "flask[async]>=3.1.3,<4.0.0", "gunicorn>=26.0.0,<27.0.0", "tortoise-orm[asyncpg]>=1.1.7,<2.0.0", ] diff --git a/resources/ts/App.vue b/resources/ts/App.vue index aac6234..3334e1b 100644 --- a/resources/ts/App.vue +++ b/resources/ts/App.vue @@ -1,7 +1,36 @@