-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
49 lines (36 loc) · 1.25 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
import sys
import time
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import RedirectResponse
from loguru import logger
from app.routers import products, categories, ticket, reports
# Configure loguru
logger.remove()
logger.add(
sys.stdout, colorize=True, format="<green>{time}</green> <level>{message}</level>"
)
app = FastAPI()
# API router
api_router = FastAPI()
@api_router.middleware("http")
async def add_process_time_header(request: Request, call_next):
start_time = time.perf_counter()
response = await call_next(request)
process_time = time.perf_counter() - start_time
response.headers["X-Process-Time"] = str(process_time)
logger.info(f"Processed {request.url} in {process_time:.3f} seconds")
return response
# Include routers
api_router.include_router(products.router)
api_router.include_router(categories.router)
api_router.include_router(ticket.router)
api_router.include_router(reports.router)
# Mount the API router
app.mount("/api", api_router)
# Serve static files
app.mount("/", StaticFiles(directory="static", html=True), name="static")
# Redirect /api to /api/docs
@app.get("/api")
async def redirect_to_docs():
return RedirectResponse(url="/api/docs")