|
3 | 3 | from fastapi.responses import JSONResponse |
4 | 4 | from starlette.status import HTTP_504_GATEWAY_TIMEOUT, HTTP_422_UNPROCESSABLE_ENTITY |
5 | 5 |
|
6 | | -from app.config import get_settings, Settings |
| 6 | +from app.config import get_settings |
7 | 7 | from app.exceptions import TimeseriesValidationError, TimeseriesTimeoutError |
8 | 8 | from app.routers.v1 import api as v1_api |
9 | 9 | from app.routers.v2 import api as v2_api |
10 | 10 |
|
| 11 | +import sentry_sdk |
| 12 | +from sentry_sdk.integrations.asgi import SentryAsgiMiddleware |
| 13 | + |
| 14 | +import logging |
| 15 | + |
11 | 16 |
|
12 | 17 | app = FastAPI(title="SKOPE API Services") |
13 | 18 |
|
| 19 | +settings = get_settings() |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +if settings.is_production: |
| 25 | + sentry_sdk.init(dsn=settings.sentry_dsn) |
| 26 | + try: |
| 27 | + app.add_middleware(SentryAsgiMiddleware) |
| 28 | + except Exception: |
| 29 | + logger.error("Unable to initialize Sentry middleware") |
| 30 | + pass |
14 | 31 |
|
15 | 32 | # Since the whole API is public there is no danger in allowing all cross origin requests for now |
16 | 33 | app.add_middleware( |
17 | 34 | CORSMiddleware, |
18 | | - allow_origins=get_settings().allowed_origins, |
| 35 | + allow_origins=settings.allowed_origins, |
19 | 36 | allow_methods=["*"], |
20 | 37 | allow_headers=["*"], |
21 | 38 | ) |
22 | 39 |
|
23 | 40 |
|
24 | 41 | @app.get("/settings") |
25 | | -async def info(settings: Settings = Depends(get_settings)): |
| 42 | +async def info(): |
26 | 43 | info_dict = dict(settings.__dict__) |
27 | 44 | info_dict.update(logfile=settings.logging_config_file) |
28 | 45 | return info_dict |
29 | 46 |
|
30 | 47 |
|
| 48 | +@app.get("/test-sentry") |
| 49 | +async def test_sentry(): |
| 50 | + raise Exception("Testing sentry integration") |
| 51 | + |
| 52 | + |
31 | 53 | @app.exception_handler(TimeseriesTimeoutError) |
32 | 54 | async def timeseries_timeout_error_handler( |
33 | 55 | request: Request, exc: TimeseriesTimeoutError |
|
0 commit comments