diff --git a/api/main.py b/api/main.py index 1963d4f6..25ddfe55 100644 --- a/api/main.py +++ b/api/main.py @@ -49,6 +49,14 @@ user_scopes={"admin": "Superusers", "users": "Regular users"}) pubsub = None # pylint: disable=invalid-name +API_VERSIONS = [] + + +@app.on_event('startup') +async def init_api_versions(): + """Startup event handler to initialize list of API versions""" + global API_VERSIONS + API_VERSIONS = ['latest', 'v0'] @app.on_event('startup') @@ -685,6 +693,7 @@ async def put_regression(regression_id: str, regression: Regression, on_startup=[ pubsub_startup, create_indexes, + init_api_versions, ] ) @@ -701,3 +710,14 @@ async def put_regression(regression_id: str, regression: Regression, sub_app.app.add_exception_handler( errors.InvalidId, invalid_id_exception_handler ) + + +@app.middleware("http") +async def redirect_http_requests(request: Request, call_next): + """Redirect request with version prefix when no version is provided""" + path = request.scope['path'] + prefix = path.split('/')[1] + if prefix not in API_VERSIONS: + request.scope['path'] = '/latest' + path + response = await call_next(request) + return response