feat(): format & proper startup (make main async for uvicorn to inherit current asyncio event loop)

This commit is contained in:
doas root
2025-11-10 23:19:51 +03:00
parent 5ca3e72834
commit 08a2ae6456
5 changed files with 809 additions and 309 deletions
+64 -63
View File
@@ -13,6 +13,7 @@ dependencies = [
"argon2_cffi==23.1.0",
"cryptography==46.0.3",
"httpx==0.28.1",
"psycopg[binary]>=3.2.12",
]
[dependency-groups]
@@ -37,7 +38,7 @@ tests = [
dev = [
{ include-group = "tests" },
{ include-group = "linters" },
{ include-group = "migrations"},
{ include-group = "migrations" },
]
[project.scripts]
@@ -61,17 +62,17 @@ exclude = [
"src/template_project/migrations",
]
enable_error_code = [
"truthy-bool",
"truthy-iterable",
"redundant-expr",
"unused-awaitable",
"ignore-without-code",
"possibly-undefined",
"redundant-self",
"explicit-override",
"mutable-override",
"unimported-reveal",
"deprecated",
"truthy-bool",
"truthy-iterable",
"redundant-expr",
"unused-awaitable",
"ignore-without-code",
"possibly-undefined",
"redundant-self",
"explicit-override",
"mutable-override",
"unimported-reveal",
"deprecated",
]
[tool.ruff]
@@ -86,63 +87,63 @@ exclude = [
[tool.ruff.lint]
select = [
"A", # flake8-builtins
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"C90", # maccabe
"COM", # flake8-commas
"D", # pydocstyle
"DTZ", # flake8-datetimez
"E", # pycodestyle
"ERA", # flake8-eradicate
"EXE", # flake8-executable
"F", # pyflakes
"FA", # flake8-future-annotations
"FBT", # flake8-boolean-trap
"FLY", # pyflint
"FURB", # refurb
"G", # flake8-logging-format
"I", # isort
"ICN", # flake8-import-conventions
"ISC", # flake8-implicit-str-concat
"LOG", # flake8-logging
"N", # pep8-naming
"PERF", # perflint
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"PTH", # flake8-use-pathlib
"Q", # flake8-quotes
"RET", # flake8-return
"RSE", # flake8-raise
"RUF", # ruff
"S", # flake8-bandit
"SIM", # flake8-simpify
"SLF", # flake8-self
"SLOT", # flake8-slots
"T100", # flake8-debugger
"TRY", # tryceratops
"UP", # pyupgrade
"W", # pycodestyle
"YTT", # flake8-2020
"A", # flake8-builtins
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"C90", # maccabe
"COM", # flake8-commas
"D", # pydocstyle
"DTZ", # flake8-datetimez
"E", # pycodestyle
"ERA", # flake8-eradicate
"EXE", # flake8-executable
"F", # pyflakes
"FA", # flake8-future-annotations
"FBT", # flake8-boolean-trap
"FLY", # pyflint
"FURB", # refurb
"G", # flake8-logging-format
"I", # isort
"ICN", # flake8-import-conventions
"ISC", # flake8-implicit-str-concat
"LOG", # flake8-logging
"N", # pep8-naming
"PERF", # perflint
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"PTH", # flake8-use-pathlib
"Q", # flake8-quotes
"RET", # flake8-return
"RSE", # flake8-raise
"RUF", # ruff
"S", # flake8-bandit
"SIM", # flake8-simpify
"SLF", # flake8-self
"SLOT", # flake8-slots
"T100", # flake8-debugger
"TRY", # tryceratops
"UP", # pyupgrade
"W", # pycodestyle
"YTT", # flake8-2020
]
ignore = [
"A005", # allow to shadow stdlib and builtin module names
"COM812", # trailing comma, conflicts with `ruff format`
# Different doc rules that we don't really care about:
"D",
"ISC001", # implicit string concat conflicts with `ruff format`
"ISC003", # prefer explicit string concat over implicit concat
"PLR09", # we have our own complexity rules
"PLR2004", # do not report magic numbers
"PLR6301", # do not require classmethod / staticmethod when self not used
"TRY003", # long exception messages from `tryceratops`
"A005", # allow to shadow stdlib and builtin module names
"COM812", # trailing comma, conflicts with `ruff format`
# Different doc rules that we don't really care about:
"D",
"ISC001", # implicit string concat conflicts with `ruff format`
"ISC003", # prefer explicit string concat over implicit concat
"PLR09", # we have our own complexity rules
"PLR2004", # do not report magic numbers
"PLR6301", # do not require classmethod / staticmethod when self not used
"TRY003", # long exception messages from `tryceratops`
]
external = [ "WPS" ]
external = ["WPS"]
[tool.ruff.lint.per-file-ignores]
"tests/*.py" = [
"S101", # asserts
"S101", # asserts
]
[tool.ruff.lint.isort]
+7
View File
@@ -1,3 +1,5 @@
import asyncio
import sys
from logging.config import fileConfig
import os
from pathlib import Path
@@ -7,6 +9,9 @@ from sqlalchemy import pool
from alembic import context
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
from template_project.web_api.configuration import load_configuration
# this is the Alembic Config object, which provides
@@ -23,11 +28,13 @@ if config.config_file_name is not None:
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from template_project.adapters.data_gateways.tables import meta_data
target_metadata = meta_data
configuration = load_configuration(Path(os.environ["CONFIGURATION_PATH"]))
config.set_main_option("sqlalchemy.url", configuration.database.url.get_value())
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
+9 -8
View File
@@ -1,6 +1,7 @@
import argparse
import asyncio
import os
import selectors
import sys
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
@@ -70,27 +71,27 @@ def make_asgi_application(
return app
def _main(
async def _main(
configuration_path: Path,
) -> None:
configuration = load_configuration(configuration_path)
ioc = make_ioc(configuration)
asgi_application = make_asgi_application(ioc)
uvicorn.run(
asgi_application,
port=configuration.server.port,
config = uvicorn.Config(
app=asgi_application,
host=configuration.server.host,
port=configuration.server.port,
log_config=LOG_CONFIG,
access_log=configuration.server.access_log,
)
server = uvicorn.Server(config)
await server.serve()
def main() -> None:
if sys.platform == "win32":
from asyncio import WindowsSelectorEventLoopPolicy # noqa: PLC0415
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("configuration", default=None)
@@ -103,7 +104,7 @@ def main() -> None:
"pass the path to the config or specify it in the environment variables `CONFIGURATION_PATH`",
)
_main(Path(configuration_path))
asyncio.run(_main(Path(configuration_path)))
if __name__ == "__main__":
@@ -1,3 +1,5 @@
import asyncio
from dishka import FromDishka
from dishka.integrations.fastapi import DishkaRoute
from fastapi import APIRouter
Generated
+727 -238
View File
File diff suppressed because it is too large Load Diff