-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (45 loc) · 1.9 KB
/
main.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
51
52
53
54
55
56
from fastapi import FastAPI, Request, status
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from controllers.address_routes import router as address_router
from controllers.cart_items_routes import router as cart_items_router
from controllers.cart_routes import router as cart_router
from controllers.product_routes import router as product_router
from controllers.user_routes import router as user_router
from project_logs.logging import set_logging
async def startup_db_client():
global app
app.test = True
# app.database = DataBase()
# await app.database.connect_db()
async def shutdown_db_client():
global app
# await app.database.disconnect_db()
app = FastAPI()
app.include_router(user_router, tags=["user"], prefix="/user")
app.include_router(product_router, tags=["products"], prefix="/products")
app.include_router(address_router, tags=["address"], prefix="/user/{user_id}/address")
app.include_router(cart_router, tags=["cart"], prefix="/cart")
app.include_router(cart_items_router, tags=["cart_item"], prefix="/cart/{cart_id}/item")
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
project_errors = []
project_error = {}
error_log = set_logging("errors")
for error in exc.errors():
project_error["error_loc"] = error["loc"]
project_error["error_type"] = error["type"]
project_error["error_msg"] = error["msg"]
project_errors.append(project_error)
error_log.error(project_error)
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder(project_errors),
)
@app.get("/")
async def home(request: Request):
return {
"status": "OK",
"msg": "Welcome to LFE Shooping Cart LuizaCode",
}