forked from pgorecki/python-ddd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (83 loc) · 2.94 KB
/
Copy pathmain.py
File metadata and controls
107 lines (83 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import time
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from pydantic import ValidationError
from api.dependencies import oauth2_scheme # noqa
from api.routers import bidding, catalog, diagnostics, iam
from config.api_config import ApiConfig
from config.container import ApplicationContainer
from seedwork.domain.exceptions import DomainException, EntityNotFoundException
from seedwork.infrastructure.database import Base
from seedwork.infrastructure.logging import LoggerFactory, logger
# configure logger prior to first usage
LoggerFactory.configure(logger_name="api")
# dependency injection container
config = ApiConfig()
container = ApplicationContainer(config=config)
db_engine = container.db_engine()
logger.info(f"using db engine {db_engine}, creating tables")
Base.metadata.create_all(db_engine)
logger.info("setup complete")
app = FastAPI(debug=config.DEBUG)
app.include_router(catalog.router)
app.include_router(bidding.router)
app.include_router(iam.router)
app.include_router(diagnostics.router)
app.container = container # type: ignore
@app.exception_handler(ValidationError)
async def pydantic_validation_exception_handler(request: Request, exc: ValidationError):
return JSONResponse(
status_code=422,
content={
"detail": exc.errors(),
},
)
# startup
try:
import uuid
from modules.iam.application.services import IamService
with container.application().transaction_context() as ctx:
iam_service = ctx[IamService]
iam_service.create_user(
user_id=uuid.UUID(int=1),
email="user1@example.com",
password="password",
access_token="token",
)
except ValueError as e:
...
@app.exception_handler(DomainException)
async def domain_exception_handler(request: Request, exc: DomainException):
if container.config.DEBUG:
raise exc
return JSONResponse(
status_code=500,
content={"message": f"Oops! {exc} did something. There goes a rainbow..."},
)
@app.exception_handler(EntityNotFoundException)
async def entity_not_found_exception_handler(
request: Request, exc: EntityNotFoundException
):
return JSONResponse(
status_code=404,
content={
"message": f"Entity {exc.kwargs} not found in {exc.repository.__class__.__name__}"
},
)
@app.middleware("http")
async def add_lato_application(request: Request, call_next):
request.state.lato_application = container.application()
return await call_next(request)
@app.middleware("http")
async def add_process_time(request: Request, call_next):
start_time = time.time()
try:
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response
finally:
pass
@app.get("/")
async def root():
return {"info": "Online auctions API. See /docs for documentation"}