forked from preneond/python-fastapi-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (58 loc) · 1.96 KB
/
Copy pathmain.py
File metadata and controls
72 lines (58 loc) · 1.96 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
import logging
import uvicorn
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from starlette import status
from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request
from src.config.settings import get_settings
from src.core.database import DatabaseConnection
from src.core.responses import error_response
from src.routers import api_router
settings = get_settings()
app = FastAPI(
title=settings.api_config.title,
description=settings.api_config.description,
version=settings.api_config.version,
docs_url=settings.api_config.docs_url,
)
app.add_middleware(
CORSMiddleware,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.on_event("startup")
async def startup_event() -> None:
# setup logger
logger = logging.getLogger("uvicorn.access")
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
logger.addHandler(handler)
app.state.logger = logger
# setup db connection
db_connection = DatabaseConnection(dsn=settings.db_connection.postgres_uri)
app.state.db_connection = db_connection
@app.on_event("shutdown")
async def shutdown_event() -> None:
db_connection = app.state.db_connection
db_connection.close()
# exception handling
@app.exception_handler(RequestValidationError)
@app.exception_handler(Exception)
async def exception_handler(request: Request, exc: Exception) -> JSONResponse:
logger = request.app.state.logger
logger.error(f"{exc}")
return error_response(errors=str(exc), status_code=status.HTTP_400_BAD_REQUEST)
app.include_router(api_router)
if __name__ == "__main__":
settings = get_settings()
server = settings.uvicorn
uvicorn.run(
app="main:app",
host=server.host,
port=server.port,
log_level=server.log_level,
reload=server.reload,
)