33import logging
44import os
55import re
6+ import sys
67from pathlib import Path
78
89import yaml
@@ -114,28 +115,29 @@ async def i18n_middleware(request: Request, call_next):
114115
115116
116117def create_app () -> FastAPI :
117- _setup_env ()
118+ if "pytest" not in sys .modules :
119+ _setup_env ()
118120
119- if os .environ .get ("CI" ):
120- import pytest
121+ if os .environ .get ("CI" ):
122+ import pytest
121123
122- pytest .skip ("Skipping in CI" , allow_module_level = True )
124+ pytest .skip ("Skipping in CI" , allow_module_level = True )
123125
124- ol_config_path = Path (__file__ ).parent / "conf" / "openlibrary.yml"
125- ol_config = os .environ .get ("OL_CONFIG" , str (ol_config_path ))
126- try :
127- # We still call this even though we don't use it because of the side effects
128- legacy_wsgi = _load_legacy_wsgi (ol_config ) # noqa: F841
126+ ol_config_path = Path (__file__ ).parent / "conf" / "openlibrary.yml"
127+ ol_config = os .environ .get ("OL_CONFIG" , str (ol_config_path ))
128+ try :
129+ # We still call this even though we don't use it because of the side effects
130+ legacy_wsgi = _load_legacy_wsgi (ol_config ) # noqa: F841
129131
130- global sentry
131- if sentry is not None :
132- return
133- sentry = init_sentry (getattr (infogami .config , 'sentry' , {}))
134- set_tag ("fastapi" , True )
132+ global sentry
133+ if sentry is not None :
134+ return
135+ sentry = init_sentry (getattr (infogami .config , 'sentry' , {}))
136+ set_tag ("fastapi" , True )
135137
136- except Exception :
137- logger .exception ("Failed to initialize legacy WSGI app" )
138- raise
138+ except Exception :
139+ logger .exception ("Failed to initialize legacy WSGI app" )
140+ raise
139141
140142 app = FastAPI (title = "OpenLibrary ASGI" , version = "0.0.1" )
141143
@@ -154,6 +156,13 @@ def create_app() -> FastAPI:
154156
155157 setup_i18n (app )
156158
159+ @app .middleware ("http" )
160+ async def add_fastapi_header (request : Request , call_next ):
161+ """Middleware to add a header indicating the response came from FastAPI."""
162+ response = await call_next (request )
163+ response .headers ["X-Served-By" ] = "FastAPI"
164+ return response
165+
157166 # --- Fast routes (mounted within this app) ---
158167 @app .get ("/health" )
159168 def health () -> dict [str , str ]:
0 commit comments