Skip to content

Commit 7cdcb0a

Browse files
committed
add settings to ioc
1 parent 7298c7a commit 7cdcb0a

File tree

7 files changed

+103
-92
lines changed

7 files changed

+103
-92
lines changed

app/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import granian
22
from granian.constants import Interfaces, Loops
33

4-
from app.settings import settings
4+
from app import ioc
55

66

77
if __name__ == "__main__":
8+
settings = ioc.IOCContainer.settings.sync_resolve()
89
granian.Granian(
910
target="app.application:application",
1011
address="0.0.0.0", # noqa: S104

app/application.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
from fastapi import FastAPI
22
from that_depends.providers import DIContextMiddleware
33

4-
from app import exceptions
4+
from app import exceptions, ioc
55
from app.api.decks import ROUTER
66
from app.exceptions import DatabaseValidationError
7-
from app.settings import settings
87

98

109
def get_app() -> FastAPI:
10+
settings = ioc.IOCContainer.settings.sync_resolve()
1111
_app = FastAPI(
1212
title=settings.service_name,
1313
debug=settings.debug,
1414
)
15-
1615
_app.include_router(ROUTER, prefix="/api")
17-
1816
_app.add_middleware(DIContextMiddleware)
19-
2017
_app.add_exception_handler(
2118
DatabaseValidationError,
2219
exceptions.database_validation_exception_handler, # type: ignore[arg-type]
2320
)
24-
2521
return _app
2622

2723

app/db/resource.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
from sqlalchemy.ext import asyncio as sa
55

6-
from app.settings import settings
6+
from app.settings import Settings
77

88

99
logger = logging.getLogger(__name__)
1010

1111

12-
async def create_sa_engine() -> typing.AsyncIterator[sa.AsyncEngine]:
12+
async def create_sa_engine(settings: Settings) -> typing.AsyncIterator[sa.AsyncEngine]:
1313
logger.info("Initializing SQLAlchemy engine")
1414
engine = sa.create_async_engine(
1515
url=settings.db_dsn,

app/ioc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
from app.db.resource import create_sa_engine, create_session
77
from app.repositories.decks import CardsRepository, DecksRepository
8+
from app.settings import Settings
89

910

1011
class IOCContainer(BaseContainer):
11-
database_engine = providers.AsyncResource(create_sa_engine)
12+
settings = providers.Singleton(Settings)
13+
14+
database_engine = providers.AsyncResource(create_sa_engine, settings=typing.cast(Settings, settings))
1215
session = providers.AsyncContextResource(create_session, engine=typing.cast(AsyncEngine, database_engine))
1316

1417
decks_repo = providers.Factory(DecksRepository, session=session)

app/settings.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,3 @@ def db_dsn(self) -> URL:
3232
self.db_port,
3333
self.db_database,
3434
)
35-
36-
37-
settings = Settings()

migrations/env.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
from alembic import context
44
from sqlalchemy import URL, create_engine
55

6+
from app import ioc
67
from app.models import METADATA
7-
from app.settings import settings
88

99

1010
def get_dsn() -> URL:
11+
settings = ioc.IOCContainer.settings.sync_resolve()
1112
db_dsn = settings.db_dsn
1213
return db_dsn.set(drivername="postgresql")
1314

poetry.lock

Lines changed: 91 additions & 78 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)