Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ MAKEFLAGS += --no-print-directory

# -----------------------------
# User-configurable variables (edit this)
# INFRA_SERVICES: long-running infra (db, broker, cache, ...)
# INFRA_INIT_SERVICES: one-shot services that prepare INFRA_SERVICES
# -----------------------------
PROJECT_NAME ?= $(notdir $(abspath .))
INFRA_SERVICES ?= db_pg
INFRA_INIT_SERVICES ?=

# -----------------------------
# Internal vars / aliases
Expand Down Expand Up @@ -117,10 +120,10 @@ up: docker-env
$(DOCKER_COMPOSE) up --build --force-recreate

upd-local: local-env
$(DOCKER_COMPOSE) up -d --build --force-recreate $(INFRA_SERVICES)
$(DOCKER_COMPOSE) up -d --build --force-recreate $(INFRA_SERVICES) $(INFRA_INIT_SERVICES)

up-local: local-env
$(DOCKER_COMPOSE) up --build --force-recreate $(INFRA_SERVICES)
$(DOCKER_COMPOSE) up --build --force-recreate $(INFRA_SERVICES) $(INFRA_INIT_SERVICES)

down:
$(DOCKER_COMPOSE) down
Expand All @@ -135,6 +138,9 @@ test-docker: docker-env
$(DC_TEST_DOCKER) down -v --remove-orphans >/dev/null 2>&1 || true; \
if [ -n "$(strip $(INFRA_SERVICES))" ]; then \
$(DC_TEST_DOCKER) up -d --build --wait --wait-timeout 180 $(INFRA_SERVICES); \
if [ -n "$(strip $(INFRA_INIT_SERVICES))" ]; then \
$(DC_TEST_DOCKER) up --build $(INFRA_INIT_SERVICES) >/dev/null; \
fi; \
else \
echo "INFRA_SERVICES is empty, skipping infra startup"; \
fi; \
Expand Down
34 changes: 10 additions & 24 deletions scripts/dishka/plot_dependencies_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,23 @@
import dishka.plotter
from dishka import AsyncContainer

from app.config.loader import (
load_app_settings,
load_cookie_settings,
load_jwt_settings,
load_password_hasher_settings,
load_postgres_settings,
load_session_settings,
load_sqla_settings,
)
from app.main.run import make_ioc_container


def generate_dependency_graph_d2(container: AsyncContainer) -> str:
from app.main.run import make_app


def generate_d2_dependency_graph(container: AsyncContainer) -> str:
"""
Generates a dependency graph for the container in `d2` format.
Generates dependency graph for container in `d2` format.
See https://d2lang.com for rendering instructions.
"""
return dishka.plotter.render_d2(container)


async def main() -> None:
async with make_ioc_container(
app_settings=load_app_settings(),
postgres_settings=load_postgres_settings(),
sqla_settings=load_sqla_settings(),
password_hasher_settings=load_password_hasher_settings(),
jwt_settings=load_jwt_settings(),
session_settings=load_session_settings(),
cookie_settings=load_cookie_settings(),
)() as container:
print(generate_dependency_graph_d2(container))
app = make_app()
container = app.state.dishka_container
try:
print(generate_d2_dependency_graph(container))
finally:
await container.close()


Expand Down
46 changes: 12 additions & 34 deletions src/app/main/run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections.abc import AsyncIterator, Callable
from contextlib import AbstractAsyncContextManager, asynccontextmanager

from dishka import AsyncContainer, Provider, make_async_container
from dishka import Provider, make_async_container
from dishka.integrations.fastapi import setup_dishka
from fastapi import FastAPI

Expand Down Expand Up @@ -44,31 +44,6 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
return lifespan


def make_ioc_container(
*di_providers: Provider,
app_settings: AppSettings,
postgres_settings: PostgresSettings,
sqla_settings: SqlaSettings,
password_hasher_settings: PasswordHasherSettings,
jwt_settings: JwtSettings,
session_settings: SessionSettings,
cookie_settings: CookieSettings,
) -> AsyncContainer:
return make_async_container(
*get_providers(),
*di_providers,
context={
AppSettings: app_settings,
PostgresSettings: postgres_settings,
SqlaSettings: sqla_settings,
PasswordHasherSettings: password_hasher_settings,
JwtSettings: jwt_settings,
SessionSettings: session_settings,
CookieSettings: cookie_settings,
},
)


def make_app(
*di_providers: Provider,
app_settings: AppSettings | None = None,
Expand Down Expand Up @@ -106,15 +81,18 @@ def make_app(
lifespan=make_lifespan(),
root_path=app_settings.ROOT_PATH.rstrip("/"),
)
container = make_ioc_container(
container = make_async_container(
*get_providers(),
*di_providers,
app_settings=app_settings,
postgres_settings=postgres_settings,
sqla_settings=sqla_settings,
password_hasher_settings=password_hasher_settings,
jwt_settings=jwt_settings,
session_settings=session_settings,
cookie_settings=cookie_settings,
context={
AppSettings: app_settings,
PostgresSettings: postgres_settings,
SqlaSettings: sqla_settings,
PasswordHasherSettings: password_hasher_settings,
JwtSettings: jwt_settings,
SessionSettings: session_settings,
CookieSettings: cookie_settings,
},
)
setup_dishka(container, app)
setup_middlewares(app, cookie_settings)
Expand Down