added middelware
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 33s

This commit is contained in:
2024-12-10 21:42:33 +01:00
parent a8aa012c2a
commit 1faff0fbc2

21
app.py
View File

@@ -7,14 +7,31 @@ import os
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware
from fastapi.middleware.trustedhost import TrustedHostMiddleware
from fastapi import FastAPI
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
class ProxyHeadersMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
# Fix protocol and host based on headers sent by NGINX
proto = request.headers.get("X-Forwarded-Proto", "http")
host = request.headers.get("X-Forwarded-Host")
if proto:
request.scope["scheme"] = proto
if host:
request.headers["host"] = host
response = await call_next(request)
return response
app = FastAPI() app = FastAPI()
# Mount static files # Mount static files
app.mount("/static", StaticFiles(directory="static"), name="static") app.mount("/static", StaticFiles(directory="static"), name="static")
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["*"]) app.add_middleware(ProxyHeadersMiddleware, allowed_hosts=["*"])
# Templates directory # Templates directory