|
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 |
| 11 | +from fastapi.middleware.cors import CORSMiddleware |
| 12 | +from fastapi.responses import JSONResponse |
8 | 13 |
|
9 | 14 | from src import routers |
10 | 15 | from src.authentication import authenticate_user |
| 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 |
11 | 18 |
|
| 19 | +# Load environment variables and set up logging |
12 | 20 | load_dotenv() |
| 21 | +setup_logging() |
| 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() |
13 | 31 |
|
14 | 32 | app = FastAPI( |
15 | 33 | debug=False, |
|
42 | 60 | {"name": "trends", "description": "CRUD operations on trends."}, |
43 | 61 | {"name": "users", "description": "CRUD operations on users."}, |
44 | 62 | {"name": "choices", "description": "List valid options for forms fields."}, |
| 63 | + {"name": "favourites", "description": "Manage user's favorite signals."}, |
45 | 64 | ], |
46 | 65 | docs_url="/", |
47 | 66 | redoc_url=None, |
48 | 67 | ) |
49 | 68 |
|
| 69 | +# Add global exception handler to report errors to Bugsnag |
| 70 | +@app.exception_handler(Exception) |
| 71 | +async def global_exception_handler(request: Request, exc: Exception): |
| 72 | + logging.error(f"Unhandled exception: {str(exc)}", exc_info=True) |
| 73 | + |
| 74 | + if BUGSNAG_ENABLED: |
| 75 | + import bugsnag |
| 76 | + bugsnag.notify( |
| 77 | + exc, |
| 78 | + metadata={ |
| 79 | + "request": { |
| 80 | + "url": str(request.url), |
| 81 | + "method": request.method, |
| 82 | + "headers": dict(request.headers), |
| 83 | + "client": request.client.host if request.client else None, |
| 84 | + } |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + return JSONResponse( |
| 89 | + status_code=500, |
| 90 | + content={"detail": "Internal server error"}, |
| 91 | + ) |
| 92 | + |
| 93 | +# allow cors |
| 94 | +app.add_middleware( |
| 95 | + CORSMiddleware, |
| 96 | + allow_origins=["*"], |
| 97 | + allow_credentials=True, |
| 98 | + allow_methods=["*"], |
| 99 | + allow_headers=["*"], |
| 100 | +) |
| 101 | + |
| 102 | +# Add Bugsnag exception handling middleware |
| 103 | +# Important: Add middleware AFTER registering exception handlers |
| 104 | +bugsnag_app = get_bugsnag_middleware(app) |
50 | 105 |
|
51 | 106 | for router in routers.ALL: |
52 | 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"} |
| 139 | + |
| 140 | +# Use the Bugsnag middleware wrapped app for ASGI |
| 141 | +app = bugsnag_app |
0 commit comments