|
3 | 3 | the frontend platform with the backend database. |
4 | 4 | """ |
5 | 5 |
|
| 6 | +import os |
| 7 | +import logging |
| 8 | +import datetime |
6 | 9 | from dotenv import load_dotenv |
7 | | -from fastapi import Depends, FastAPI |
| 10 | +from fastapi import Depends, FastAPI, Request |
8 | 11 | from fastapi.middleware.cors import CORSMiddleware |
| 12 | +from fastapi.responses import JSONResponse |
9 | 13 |
|
10 | 14 | from src import routers |
11 | 15 | from src.authentication import authenticate_user |
12 | 16 | from src.config.logging_config import setup_logging |
| 17 | +from src.bugsnag_config import configure_bugsnag, setup_bugsnag_logging, get_bugsnag_middleware, BUGSNAG_ENABLED |
13 | 18 |
|
| 19 | +# Load environment variables and set up logging |
14 | 20 | load_dotenv() |
15 | 21 | setup_logging() |
16 | 22 |
|
| 23 | +# Get application version |
| 24 | +app_version = os.environ.get("RELEASE_VERSION", "dev") |
| 25 | +app_env = os.environ.get("ENVIRONMENT", "development") |
| 26 | +logging.info(f"Starting application - version: {app_version}, environment: {app_env}") |
| 27 | + |
| 28 | +# Configure Bugsnag for error tracking |
| 29 | +configure_bugsnag() |
| 30 | +setup_bugsnag_logging() |
| 31 | + |
17 | 32 | app = FastAPI( |
18 | 33 | debug=False, |
19 | 34 | title="Future Trends and Signals API", |
|
60 | 75 | allow_headers=["*"], |
61 | 76 | ) |
62 | 77 |
|
| 78 | +# Add Bugsnag exception handling middleware |
| 79 | +app = get_bugsnag_middleware(app) |
| 80 | + |
| 81 | +# Add global exception handler to report errors to Bugsnag |
| 82 | +@app.exception_handler(Exception) |
| 83 | +async def global_exception_handler(request: Request, exc: Exception): |
| 84 | + logging.error(f"Unhandled exception: {str(exc)}", exc_info=True) |
| 85 | + |
| 86 | + if BUGSNAG_ENABLED: |
| 87 | + import bugsnag |
| 88 | + bugsnag.notify( |
| 89 | + exc, |
| 90 | + metadata={ |
| 91 | + "request": { |
| 92 | + "url": str(request.url), |
| 93 | + "method": request.method, |
| 94 | + "headers": dict(request.headers), |
| 95 | + "client": request.client.host if request.client else None, |
| 96 | + } |
| 97 | + } |
| 98 | + ) |
| 99 | + |
| 100 | + return JSONResponse( |
| 101 | + status_code=500, |
| 102 | + content={"detail": "Internal server error"}, |
| 103 | + ) |
| 104 | + |
63 | 105 |
|
64 | 106 | for router in routers.ALL: |
65 | 107 | app.include_router(router=router, dependencies=[Depends(authenticate_user)]) |
| 108 | + |
| 109 | +# Add diagnostic endpoint for health checks and Bugsnag verification |
| 110 | +@app.get("/_health", include_in_schema=False) |
| 111 | +async def health_check(): |
| 112 | + """Health check endpoint that also shows the current environment and version.""" |
| 113 | + return { |
| 114 | + "status": "ok", |
| 115 | + "environment": app_env, |
| 116 | + "version": app_version, |
| 117 | + "bugsnag_enabled": BUGSNAG_ENABLED |
| 118 | + } |
| 119 | + |
| 120 | +# Test endpoint to trigger a test error report to Bugsnag if enabled |
| 121 | +@app.get("/_test-error", include_in_schema=False) |
| 122 | +async def test_error(): |
| 123 | + """Trigger a test error to verify Bugsnag is working.""" |
| 124 | + if BUGSNAG_ENABLED: |
| 125 | + import bugsnag |
| 126 | + bugsnag.notify( |
| 127 | + Exception("Test error triggered via /_test-error endpoint"), |
| 128 | + metadata={ |
| 129 | + "test_info": { |
| 130 | + "environment": app_env, |
| 131 | + "version": app_version, |
| 132 | + "timestamp": str(datetime.datetime.now()) |
| 133 | + } |
| 134 | + } |
| 135 | + ) |
| 136 | + return {"status": "error_reported", "message": "Test error sent to Bugsnag"} |
| 137 | + else: |
| 138 | + return {"status": "disabled", "message": "Bugsnag is not enabled"} |
0 commit comments