-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (48 loc) · 2.32 KB
/
main.py
File metadata and controls
59 lines (48 loc) · 2.32 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
from pathlib import Path
from fastapi import FastAPI, HTTPException, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from app.api.v1.routes.auth import router as auth_router
from app.api.v1.routes.cafes import router as cafes_router
from app.api.v1.routes.catalog import router as catalog_router
from app.api.v1.routes.orders import router as orders_router
from app.api.v1.routes.payments import router as payments_router
from app.api.v1.routes.roles import router as roles_router
from app.api.v1.routes.subscriptions import router as subscriptions_router
from app.api.v1.routes.users import router as users_router
from app.core.constants import MSG_REQUEST_FAILED, MSG_VALIDATION_FAILED
from app.core.response import error_response
app = FastAPI(title="Multi Cafe POS API")
app.include_router(auth_router)
app.include_router(roles_router)
app.include_router(users_router)
app.include_router(cafes_router)
app.include_router(catalog_router)
app.include_router(orders_router)
app.include_router(payments_router)
app.include_router(subscriptions_router)
uploads_dir = Path(__file__).resolve().parent / "uploads"
uploads_dir.mkdir(parents=True, exist_ok=True)
app.mount("/uploads", StaticFiles(directory=str(uploads_dir)), name="uploads")
@app.exception_handler(HTTPException)
async def http_exception_handler(_: Request, exc: HTTPException) -> JSONResponse:
"""Convert HTTP exceptions into the standard API response format."""
return JSONResponse(
status_code=exc.status_code,
content=error_response(message=MSG_REQUEST_FAILED, error=exc.detail),
)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(_: Request, exc: RequestValidationError) -> JSONResponse:
"""Convert validation errors into the standard API response format."""
return JSONResponse(
status_code=422,
content=error_response(message=MSG_VALIDATION_FAILED, error=exc.errors()),
)
@app.exception_handler(Exception)
async def unhandled_exception_handler(_: Request, exc: Exception) -> JSONResponse:
"""Convert unexpected exceptions into the standard API response format."""
return JSONResponse(
status_code=500,
content=error_response(message=MSG_REQUEST_FAILED, error=str(exc)),
)