← full-stack-fastapi-template / backend/app/main.py
| 1 | import sentry_sdk |
| 2 | from fastapi import FastAPI |
| 3 | from fastapi.routing import APIRoute |
| 4 | from starlette.middleware.cors import CORSMiddleware |
| 5 | |
| 6 | from app.api.main import api_router |
| 7 | from app.core.config import settings |
| 8 | |
| 9 | |
| 10 | def custom_generate_unique_id(route: APIRoute) -> str: |
| 11 | return f"{route.tags[0]}-{route.name}" |
| 12 | |
| 13 | |
| 14 | if settings.SENTRY_DSN and settings.ENVIRONMENT != "local": |
| 15 | sentry_sdk.init(dsn=str(settings.SENTRY_DSN), enable_tracing=True) |
| 16 | |
| 17 | app = FastAPI( |
| 18 | title=settings.PROJECT_NAME, |
| 19 | openapi_url=f"{settings.API_V1_STR}/openapi.json", |
| 20 | generate_unique_id_function=custom_generate_unique_id, |
| 21 | ) |
| 22 | |
| 23 | # Set all CORS enabled origins |
| 24 | if settings.all_cors_origins: |
| 25 | app.add_middleware( |
| 26 | CORSMiddleware, |
| 27 | allow_origins=settings.all_cors_origins, |
| 28 | allow_credentials=True, |
| 29 | allow_methods=["*"], |
| 30 | allow_headers=["*"], |
| 31 | ) |
| 32 | |
| 33 | app.include_router(api_router, prefix=settings.API_V1_STR) |
| 34 |