-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (54 loc) · 2.09 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
import logging
import sentry_sdk
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from starlette.middleware.cors import CORSMiddleware
from app.api.api_v1.api import api_router
from app.constants import VALIDATION_ERROR_CODE
from app.core.config import settings
from app.exceptions import BaseHttpException
from app.utils.format import format_validation_errors
# setup loggers
logging.basicConfig(filename="./logging.conf")
# get root logger
logger = logging.getLogger(
__name__
) # the __name__ resolve to "main" since we are at the root of the project.
if settings.ENVIRONMENT != "development" and settings.SENTRY_DSN:
sentry_sdk.init(
dsn=settings.SENTRY_DSN,
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
traces_sample_rate=0.6,
)
app = FastAPI(title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json")
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
errors = format_validation_errors(errors=exc.errors())
content = {"errors": errors, "error_code": VALIDATION_ERROR_CODE}
return JSONResponse(
status_code=400,
content=content,
)
@app.exception_handler(BaseHttpException)
async def http_exception_handler(request: Request, exc: BaseHttpException):
content = {"errors": exc.detail, "error_code": exc.error_code}
return JSONResponse(
status_code=exc.status_code,
content=content,
)
# Set all CORS enabled origins
if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router, prefix=settings.API_V1_STR)
@app.get("/sentry-debug")
async def trigger_error():
division_by_zero = 1 / 0 # noqa