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