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:
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user