Route Prefixes
How prefixes work
When you need this
FastAPI example
import os
from fastapi import FastAPI
from starlette.requests import Request
app = FastAPI()
@app.middleware("http")
async def process_valohai_prefix(request: Request, call_next):
path = request.scope["path"]
# Check both header and environment variable
for prefix in (
request.headers.get("X-VH-Prefix"),
os.environ.get("VH_DEFAULT_PREFIX"),
):
if not prefix:
continue
if path.startswith(prefix):
# Tell FastAPI about the mount point
request.scope["root_path"] = prefix
# Strip the prefix for internal routing
request.scope["path"] = "/" + path[len(prefix) :].lstrip("/")
break
return await call_next(request)
@app.get("/predict")
def predict(name: str):
return {"predicted_first_letter": name[:1].lower()}Flask example
Alternative: Accept any prefix
Last updated
Was this helpful?
