-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
95 lines (82 loc) · 2.91 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Root function for FastAPI
"""
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from starlette.exceptions import HTTPException as StarletteHTTPException
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
import openapi_description
import uvicorn
from observability import (
LOGGER,
tracer,
PrometheusMiddleware,
metrics
)
import settings
import clients
app = FastAPI(
docs_url="/api",
redoc_url=None,
openapi_url="/api/openapi.json",
openapi_tags=openapi_description.TAGS,
title=openapi_description.API_DESCRIPTION["title"],
version=openapi_description.API_DESCRIPTION["version"])
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"]
)
# Setting metrics middleware
app.add_route("/metrics", metrics)
app.add_middleware(PrometheusMiddleware, app_name=settings.APP_NAME)
traces = tracer.get_tracer(__name__)
FastAPIInstrumentor.instrument_app(app, tracer_provider=tracer)
LOGGER = LOGGER.getChild(__name__)
#import clients
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
"""Start middleware and return 422 error"""
LOGGER.debug("Start Middleware")
response = await call_next(request)
if response.status_code == 422:
response = JSONResponse(status_code=response.status_code, content={
"code":"422.01",
"message": "Error in the attributs"
})
LOGGER.debug("End middleware")
return response
@app.exception_handler(StarletteHTTPException)
async def raise_exception(request: Request, exc: StarletteHTTPException):
"""Function to raise exception and adapt error message"""
if exc.status_code == 404:
LOGGER.warning(f"Status Code {exc.status_code} on path {request.url}")
return JSONResponse(status_code=exc.status_code, content={
"code":"404.01",
"message": "Resource not found"
})
if exc.status_code == 500:
LOGGER.warning(f"Status Code {exc.status_code} on path {request.url}")
return JSONResponse(status_code=exc.status_code, content={
"code":"500.01",
"message":"Internal error"
})
if exc.status_code == 405:
LOGGER.warning(f"Status Code {exc.status_code} on path {request.url}")
return JSONResponse(status_code=exc.status_code, content={
"code":"405.01",
"message": "Method not allowed"
})
LOGGER.warning(f"Status Code {exc.status_code} on path {request.url}")
return JSONResponse(status_code=exc.status_code, content=exc.detail)
@app.get("/")
async def root():
"""Route test"""
return {"Hello":"World"}
app.include_router(clients.router)
if __name__ == "__main__":
uvicorn.run(app, host=settings.HOST, port=settings.PORT)