-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (65 loc) · 1.85 KB
/
main.py
File metadata and controls
78 lines (65 loc) · 1.85 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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from starlette.requests import Request
from app.application.errors import ApplicationError
from app.domain.errors import DomainError
from app.infrastructure import DBConnection
from app.infrastructure.errors import InfrastructureError
from app.presentation.handlers import generate_trajectory_router, health_check_router
DBConnection.init_db()
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(generate_trajectory_router)
app.include_router(health_check_router)
@app.exception_handler(ApplicationError)
async def application_error_handler(
_: Request,
exc: ApplicationError,
) -> JSONResponse:
return JSONResponse(
status_code=exc.status_code,
content={
"error": exc.detail,
"type": exc.type.value,
},
)
@app.exception_handler(InfrastructureError)
async def infrastructure_error_handler(
_: Request,
exc: InfrastructureError,
) -> JSONResponse:
return JSONResponse(
status_code=exc.status_code,
content={
"error": exc.detail,
"type": exc.type.value,
},
)
@app.exception_handler(DomainError)
async def domain_error_handler(
_: Request,
exc: DomainError,
) -> JSONResponse:
return JSONResponse(
status_code=exc.status_code,
content={
"error": exc.detail,
"type": exc.type.value,
},
)
@app.exception_handler(HTTPException)
async def http_exception_handler(
_: Request,
exc: HTTPException,
) -> JSONResponse:
return JSONResponse(
status_code=exc.status_code,
content={"error": exc.detail},
)