feat: add API prefix for prefixes in url
This commit is contained in:
@@ -10,8 +10,31 @@ from fastapi import FastAPI, Query
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
|
||||
|
||||
app = FastAPI(title="Signature Validation API")
|
||||
|
||||
# Optional path prefix support: when behind a reverse-proxy that uses a
|
||||
# URL prefix (eg. `https://api.example.tld/library/...`) set `API_PREFIX` to
|
||||
# that prefix (example: `/library`) so incoming requests are rewritten to the
|
||||
# application root. This keeps route definitions unchanged while supporting
|
||||
# both proxied and direct deployments.
|
||||
_api_prefix_raw = os.getenv("API_PREFIX", "").strip()
|
||||
api_prefix = ""
|
||||
if _api_prefix_raw:
|
||||
if not _api_prefix_raw.startswith("/"):
|
||||
_api_prefix_raw = "/" + _api_prefix_raw
|
||||
api_prefix = _api_prefix_raw.rstrip("/")
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def _strip_api_prefix(request, call_next):
|
||||
if api_prefix and request.url.path.startswith(api_prefix):
|
||||
new_path = request.url.path[len(api_prefix) :]
|
||||
request.scope["path"] = new_path or "/"
|
||||
request.scope["root_path"] = api_prefix
|
||||
return await call_next(request)
|
||||
|
||||
# Allow PHP application to call this API
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
|
||||
Reference in New Issue
Block a user