11from fastapi import FastAPI
22from fastapi .middleware .cors import CORSMiddleware
3- from app . core . logging import setup_logging
3+
44from app .api .v1 .router import api_router
55from app .core .config import get_settings
6+ from app .core .errors import NotFound , not_found_handler
7+ from app .core .logging import setup_logging
68from app .core .middleware import RequestLoggingMiddleware
7- from app . core . errors import not_found_handler , NotFound
9+
810settings = get_settings ()
911
1012
@@ -16,15 +18,17 @@ def create_app() -> FastAPI:
1618 # (middleware executes in reverse order - last added runs first)
1719 app .add_middleware (RequestLoggingMiddleware )
1820
19- # Allow frontend (localhost:3000 and others) to call the API during development .
20- # CORS must be added last so it runs first and wraps all responses including errors.
21+ # Allow the configured frontend to make credentialed API requests .
22+ # Expose X-Request-ID so the frontend can use it when reporting errors.
2123 app .add_middleware (
2224 CORSMiddleware ,
23- allow_origins = ["*" ], # permissive for dev; adjust in prod
24- allow_credentials = False , # must be False when using wildcard origins
25+ allow_origins = [settings . FRONTEND_URL . rstrip ( "/" )],
26+ allow_credentials = True ,
2527 allow_methods = ["*" ],
2628 allow_headers = ["*" ],
29+ expose_headers = ["X-Request-ID" ],
2730 )
31+
2832 app .include_router (api_router , prefix = settings .API_PREFIX )
2933
3034 # error handler
@@ -39,7 +43,8 @@ def health_check():
3943 return {
4044 "status" : "healthy" ,
4145 }
42-
46+
4347 return app
4448
49+
4550app = create_app ()
0 commit comments