From 562ce19385c32f166842721c0b53da591541bc34 Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Fri, 13 Sep 2024 14:45:50 -0700 Subject: [PATCH] Add middleware to fill in client host --- backend/src/appointment/main.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/backend/src/appointment/main.py b/backend/src/appointment/main.py index 510b3da7..d790d222 100644 --- a/backend/src/appointment/main.py +++ b/backend/src/appointment/main.py @@ -143,6 +143,18 @@ async def lifespan(app: FastAPI): # init app app = FastAPI(openapi_url=openapi_url, lifespan=lifespan) + @app.middleware("http") + async def apply_x_forwarded_headers_to_client(request: Request, call_next): + ip_list = request.headers.get('x-forwarded-for') + port = request.headers.get('x-forwarded-port') + if ip_list: + client_ip = ip_list.split(',')[0] + request.client.host = client_ip + if port: + request.client.port = port + response = await call_next(request) + return response + app.add_middleware(RawContextMiddleware, plugins=(L10n(),)) # strip html tags from input requests