-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (61 loc) · 2.33 KB
/
Copy pathmain.py
File metadata and controls
74 lines (61 loc) · 2.33 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
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from datetime import UTC, datetime
from fastapi import FastAPI, Request
from fastapi.exceptions import HTTPException
from fastapi.responses import JSONResponse
from app.application.router import router as application_router
from app.auth.router import router as auth_router
from app.blockchain.service_impl import BlockchainServiceImpl
from app.core.config import settings
from app.core.exceptions import AppException
from app.db.seed import Seeder
from app.db.session import AsyncSessionLocal, engine
from app.event.router import router as event_router
from app.s3.router import router as s3_router
from app.store.router import router as store_router
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
if settings.seed_db:
blockchain = (
BlockchainServiceImpl()
if settings.blockchain_rpc_url and settings.server_private_key
else None
)
await Seeder(engine, AsyncSessionLocal, blockchain).run()
yield
app = FastAPI(title=settings.app_name, debug=settings.debug, lifespan=lifespan)
app.include_router(auth_router, prefix="/api")
app.include_router(s3_router, prefix="/api")
app.include_router(store_router, prefix="/api")
app.include_router(event_router, prefix="/api")
app.include_router(application_router, prefix="/api")
@app.exception_handler(AppException)
async def app_exception_handler(_request: Request, exc: AppException) -> JSONResponse:
return JSONResponse(
status_code=exc.status_code,
content={
"status": exc.status_code,
"data": {
"timestamp": datetime.now(UTC).isoformat(),
"message": exc.message,
"code": exc.code,
},
},
)
@app.exception_handler(HTTPException)
async def http_exception_handler(_request: Request, exc: HTTPException) -> JSONResponse:
return JSONResponse(
status_code=exc.status_code,
content={
"status": exc.status_code,
"data": {
"timestamp": datetime.now(UTC).isoformat(),
"message": str(exc.detail),
"code": str(exc.status_code),
},
},
)
@app.get("/health")
def health() -> dict[str, str]:
return {"status": "ok"}