|
| 1 | +import os |
| 2 | +import sentry_sdk |
| 3 | + |
| 4 | +from dotenv import load_dotenv |
| 5 | +from fastapi.responses import JSONResponse |
| 6 | +from fastapi.openapi.utils import get_openapi |
| 7 | +from scalar_fastapi.scalar_fastapi import Layout |
| 8 | +from fastapi.middleware.cors import CORSMiddleware |
| 9 | +from scalar_fastapi import get_scalar_api_reference |
| 10 | +from fastapi import FastAPI, Request, HTTPException, status |
| 11 | + |
| 12 | +from src import api_router |
| 13 | +from src.db.models import init_db |
| 14 | +from src.share.logging import Logging |
| 15 | + |
| 16 | +load_dotenv() |
| 17 | +_logger = Logging().get_logger() |
| 18 | + |
| 19 | +if os.environ.get("SENTRY_DSN"): |
| 20 | + # Initialize Sentry with your DSN |
| 21 | + sentry_sdk.init( |
| 22 | + dsn=os.environ.get("SENTRY_DSN"), |
| 23 | + # Add data like request headers and IP for users, |
| 24 | + # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info |
| 25 | + send_default_pii=True, |
| 26 | + # Set traces_sample_rate to 1.0 to capture 100% |
| 27 | + # of transactions for tracing. |
| 28 | + traces_sample_rate=1.0, |
| 29 | + _experiments={ |
| 30 | + # Set continuous_profiling_auto_start to True |
| 31 | + # to automatically start the profiler on when |
| 32 | + # possible. |
| 33 | + "continuous_profiling_auto_start": True, |
| 34 | + }, |
| 35 | + ) |
| 36 | + |
| 37 | +app = FastAPI() |
| 38 | + |
| 39 | +app.add_middleware( |
| 40 | + CORSMiddleware, |
| 41 | + allow_origins=["*"], |
| 42 | + allow_credentials=True, |
| 43 | + allow_methods=["*"], |
| 44 | + allow_headers=["*"], |
| 45 | +) |
| 46 | + |
| 47 | + |
| 48 | +# Global handler for all exceptions |
| 49 | +@app.exception_handler(Exception) |
| 50 | +async def global_exception_handler(request: Request, exc: Exception): |
| 51 | + |
| 52 | + # Send the exception to Sentry |
| 53 | + if os.environ.get("SENTRY_DSN"): |
| 54 | + sentry_sdk.capture_exception(exc) |
| 55 | + |
| 56 | + # You can customize the response based on the exception type |
| 57 | + if isinstance(exc, HTTPException): |
| 58 | + _logger.error( |
| 59 | + f"[{request.method}] {request.url} - {exc.status_code} - {exc.detail}") |
| 60 | + return JSONResponse( |
| 61 | + status_code=exc.status_code, |
| 62 | + content={"detail": exc.detail}, |
| 63 | + ) |
| 64 | + |
| 65 | + # For any other exceptions, return a 500 error |
| 66 | + return JSONResponse( |
| 67 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 68 | + content={"detail": "Internal server error"}, |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +@app.get("/api-doc", include_in_schema=False) |
| 73 | +async def scalar_html(): |
| 74 | + return get_scalar_api_reference( |
| 75 | + openapi_url=app.openapi_url, |
| 76 | + title="Pattern-Core API", |
| 77 | + hide_models=True, |
| 78 | + layout=Layout.MODERN |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def custom_openapi(): |
| 83 | + if app.openapi_schema: |
| 84 | + return app.openapi_schema |
| 85 | + |
| 86 | + openapi_schema = get_openapi( |
| 87 | + title="Pattern-Core API", |
| 88 | + version="1.0.0", |
| 89 | + description="Pattern Core API Documentation for creating user, workspace, project, tool, and conversation.", |
| 90 | + routes=app.routes, |
| 91 | + ) |
| 92 | + |
| 93 | + app.openapi_schema = openapi_schema |
| 94 | + return app.openapi_schema |
| 95 | + |
| 96 | + |
| 97 | +app.openapi = custom_openapi |
| 98 | + |
| 99 | +app.include_router(api_router) |
| 100 | + |
| 101 | +init_db() |
0 commit comments