-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
71 lines (56 loc) · 1.77 KB
/
run.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import time
from fastapi import FastAPI, status
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.requests import Request
from fastapi.responses import JSONResponse, RedirectResponse
from exception.exception import BaseAPIException
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.exception_handler(BaseAPIException)
async def http_exception_handler(request: Request, exception: BaseAPIException):
return JSONResponse(
status_code=exception.status_code,
content={
"message": exception.message,
"detail": exception.detail,
"error_code": exception.error_code,
},
)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request, exception: RequestValidationError
):
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={
"message": "Validation Error",
"detail": exception.errors(),
},
)
@app.middleware("https")
async def add_process_time_header(request: Request, call_next):
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response
@app.get("/", status_code=status.HTTP_307_TEMPORARY_REDIRECT, include_in_schema=False)
async def root_endpoint():
return RedirectResponse(url="/redoc")
# app.include_router(
# container_router,
# prefix="/containers",
# tags=["containers"],
# )
# app.include_router(
# tenant_router,
# prefix="/tenants",
# tags=["tenants"],
# )