-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
50 lines (39 loc) · 1.28 KB
/
api.py
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
from contextlib import asynccontextmanager
import logging
from fastapi import FastAPI, Request, status
from fastapi.responses import JSONResponse
from pydantic_core import ValidationError
from db.database import init_db, engine
from routes import login, order, product, user
# from routes import login, orders, product
@asynccontextmanager
async def lifespan(_: FastAPI):
"""
Startup and Shutdown event
"""
await init_db()
yield
await engine.dispose()
app = FastAPI(lifespan=lifespan)
@app.exception_handler(Exception)
async def internal_server_error_handler(request: Request, exc: Exception):
logging.error(exc)
err_msg = f"{request.url}: {str(exc)}"
logging.error(err_msg)
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"message": err_msg}
)
@app.exception_handler(ValidationError)
async def model_validation_handler(request: Request, exc: ValidationError):
logging.error(exc)
err_msg = f"{request.url}: {str(exc)}"
logging.error(err_msg)
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"message": err_msg}
)
app.include_router(login.router)
app.include_router(user.router)
app.include_router(product.router)
app.include_router(order.router)