Skip to content

Commit 4b687f3

Browse files
authored
Merge pull request #22 from getmarkus/cm-branch-20
feat: implement application factory and update dependencies
2 parents 06ef8b9 + 0e2526d commit 4b687f3

File tree

23 files changed

+166
-73
lines changed

23 files changed

+166
-73
lines changed

.trunk/configs/.yamllint.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
rules:
2+
quoted-strings:
3+
required: only-when-needed
4+
extra-allowed: ["{|}"]
5+
key-duplicates: {}
6+
octal-values:
7+
forbid-implicit-octal: true

.trunk/trunk.yaml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
33
version: 0.1
44
cli:
5-
version: 1.22.9
5+
version: 1.22.10
66
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
77
plugins:
88
sources:
99
- id: trunk
10-
ref: v1.6.6
10+
ref: v1.6.7
1111
uri: https://github.com/trunk-io/plugins
1212
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
1313
runtimes:
@@ -20,11 +20,16 @@ lint:
2020
- git-diff-check
2121
- bandit
2222
enabled:
23-
24-
25-
26-
23+
24+
25+
26+
27+
28+
2729
28-
30+
2931
30-
32+
33+
actions:
34+
enabled:
35+
- trunk-upgrade-available

app/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

app/core/factory.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from contextlib import asynccontextmanager
2+
3+
from fastapi import FastAPI
4+
5+
from app.interface_adapters import api_router
6+
from app.interface_adapters.containers import Container
7+
from app.interface_adapters.exceptions import AppException
8+
from app.interface_adapters.middleware.error_handler import app_exception_handler
9+
from app.resource_adapters.persistence.sqlmodel.database import get_engine
10+
from config import settings
11+
12+
13+
def create_container():
14+
container = Container()
15+
container.wire(packages=["app"])
16+
return container
17+
18+
19+
def create_app(lifespan_handler=None) -> FastAPI:
20+
if lifespan_handler is None:
21+
22+
@asynccontextmanager
23+
async def default_lifespan(app: FastAPI):
24+
# Initialize container and wire dependencies
25+
container = create_container()
26+
27+
# Initialize database if using SQLModel
28+
get_engine()
29+
30+
app.state.running = True
31+
32+
yield
33+
34+
app.state.running = False
35+
container.unwire()
36+
37+
lifespan_handler = default_lifespan
38+
39+
app = FastAPI(
40+
lifespan=lifespan_handler,
41+
title=settings.project_name,
42+
openapi_url="/v1/openapi.json",
43+
)
44+
45+
# Register global exception handler
46+
app.add_exception_handler(AppException, app_exception_handler)
47+
48+
# Register routes
49+
app.include_router(api_router, prefix="/v1")
50+
51+
return app

app/core/ports/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

app/core/usecases/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

app/domain/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class UserEvent(BaseEvent):
1919
timestamp: datetime
2020

2121

22-
class User(AggregateRoot[BaseCommand, BaseEvent]):
22+
class User(AggregateRoot):
2323
email: EmailStr
2424
is_active: bool = False
2525
full_name: str
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from dependency_injector import containers, providers
2+
from sqlmodel import Session
3+
4+
from app.resource_adapters.persistence.in_memory.issues import InMemoryIssueRepository
5+
from app.resource_adapters.persistence.sqlmodel.database import get_engine
6+
from app.resource_adapters.persistence.sqlmodel.issues import SQLModelIssueRepository
7+
from config import settings
8+
9+
10+
class Container(containers.DeclarativeContainer):
11+
wiring_config = containers.WiringConfiguration(packages=["app"])
12+
13+
# Database
14+
db_engine = providers.Singleton(get_engine)
15+
db_session = providers.Factory(Session, bind=db_engine)
16+
17+
# Repositories
18+
issue_repository = providers.Factory(
19+
(
20+
SQLModelIssueRepository
21+
if settings.execution_mode == "sqlmodel"
22+
else InMemoryIssueRepository
23+
),
24+
session=db_session if settings.execution_mode == "sqlmodel" else None,
25+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)