Skip to content

Commit

Permalink
add middleware for v1 routing
Browse files Browse the repository at this point in the history
  • Loading branch information
silvioheinze committed Oct 11, 2024
1 parent f6b72d9 commit a7a673e
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion code/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import FastAPI
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from routers import city_router, station_router

Expand Down Expand Up @@ -35,6 +35,14 @@
allow_headers=["*"],
)

# Middleware to add /v1 prefix to all routes
@app.middleware("http")
async def add_version_prefix(request: Request, call_next):
if request.url.path.startswith("/v1"):
request.scope["path"] = request.url.path[3:] # Remove '/v1' from the path
response = await call_next(request)
return response

# Register routers
app.include_router(station_router, prefix="/station")
app.include_router(city_router, prefix="/city")

0 comments on commit a7a673e

Please sign in to comment.