|
1 | | -import logging |
2 | | -from contextlib import asynccontextmanager |
3 | | - |
| 1 | +import sentry_sdk |
4 | 2 | from fastapi import FastAPI |
5 | | -from fastapi.staticfiles import StaticFiles |
| 3 | +from fastapi.routing import APIRoute |
| 4 | +from starlette.middleware.cors import CORSMiddleware |
6 | 5 |
|
7 | 6 | from app.api.main import api_router |
8 | 7 | from app.core.config import settings |
9 | | -from app.core.middleware import setup_cors |
10 | | - |
11 | | -logging.basicConfig( |
12 | | - level=logging.INFO, |
13 | | - format="%(asctime)s %(levelname)s: %(message)s", |
14 | | - datefmt="%Y-%m-%d %H:%M:%S", |
15 | | -) |
16 | | -logger = logging.getLogger(__name__) |
17 | 8 |
|
18 | 9 |
|
19 | | -@asynccontextmanager |
20 | | -async def lifespan(app: FastAPI): |
21 | | - """ |
22 | | - Function that runs before the application starts, |
23 | | - and again when the application is shutting down. |
| 10 | +def custom_generate_unique_id(route: APIRoute) -> str: |
| 11 | + return f"{route.tags[0]}-{route.name}" |
24 | 12 |
|
25 | | - For now, just a placeholder, but we can use this to setup |
26 | | - database connections, etc. |
27 | | - """ |
28 | | - yield |
29 | 13 |
|
| 14 | +if settings.SENTRY_DSN and settings.ENVIRONMENT != "local": |
| 15 | + sentry_sdk.init(dsn=str(settings.SENTRY_DSN), enable_tracing=True) |
30 | 16 |
|
31 | 17 | app = FastAPI( |
32 | | - title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json", lifespan=lifespan |
| 18 | + title=settings.PROJECT_NAME, |
| 19 | + openapi_url=f"{settings.API_V1_STR}/openapi.json", |
| 20 | + generate_unique_id_function=custom_generate_unique_id, |
33 | 21 | ) |
34 | 22 |
|
35 | | -# Use our custom CORS middleware instead of the default FastAPI one |
36 | | -setup_cors(app) |
| 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 | + ) |
37 | 32 |
|
38 | 33 | app.include_router(api_router, prefix=settings.API_V1_STR) |
39 | | - |
40 | | -# Mount the static files directory to serve static files |
41 | | -# app.mount("/static", StaticFiles(directory="static"), name="static") |
42 | | - |
43 | | - |
44 | | -@app.get("/") |
45 | | -def root(): |
46 | | - """ |
47 | | - Root endpoint for health checks |
48 | | - """ |
49 | | - return {"message": "Hello! This is the API root. Go to /docs for API documentation."} |
50 | | - |
51 | | - |
52 | | -@app.get("/health") |
53 | | -def health_check(): |
54 | | - """ |
55 | | - Health check endpoint |
56 | | - """ |
57 | | - return {"status": "ok"} |
0 commit comments