generated from hjess/PythonTemplateProject
Lets test
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 32s
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 32s
This commit is contained in:
22
app.py
22
app.py
@@ -3,31 +3,14 @@ from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
import json
|
||||
import os
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import HTMLResponse
|
||||
from contextlib import asynccontextmanager
|
||||
from markdown_render import render_markdown_with_jinja
|
||||
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import RedirectResponse
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
|
||||
class ConditionalHTTPSRedirectMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
# Extract the host or remote address
|
||||
host = request.headers.get("host", "")
|
||||
client_ip = request.client.host # Get the client's IP address
|
||||
|
||||
# Log for debugging
|
||||
print(f"Client IP: {client_ip}, Host: {host}")
|
||||
|
||||
# Check condition: Redirect only if not on 127.0.0.1
|
||||
if client_ip != "127.0.0.1" and request.url.scheme == "http":
|
||||
https_url = request.url.replace(scheme="https")
|
||||
return RedirectResponse(url=https_url)
|
||||
|
||||
# Continue processing other requests
|
||||
response = await call_next(request)
|
||||
return response
|
||||
|
||||
|
||||
# Context manager for app lifespan
|
||||
@@ -45,8 +28,7 @@ app.mount("/data", StaticFiles(directory="data"), name="data")
|
||||
|
||||
# Mount static files
|
||||
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||
#app.add_middleware( HTTPSRedirectMiddleware )
|
||||
app.add_middleware(ConditionalHTTPSRedirectMiddleware)
|
||||
app.add_middleware( HTTPSRedirectMiddleware )
|
||||
|
||||
# Templates directory
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
Reference in New Issue
Block a user