From 1fa64feefc62f8d0dd6dbe929c879c495f197a5d Mon Sep 17 00:00:00 2001 From: Ruben van Staveren Date: Mon, 25 Dec 2023 14:16:45 +0100 Subject: [PATCH] Redirect empty GET with request to client address --- app/main.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 6322685..8feb465 100644 --- a/app/main.py +++ b/app/main.py @@ -6,7 +6,8 @@ from typing import Annotated, Optional, Union import geoip2.database from geoip2.errors import AddressNotFoundError -from fastapi import FastAPI, Path, Body, Response, status +from fastapi import FastAPI, Path, Body, Request, Response, status +from fastapi.responses import RedirectResponse from pydantic import BaseModel app = FastAPI() @@ -87,3 +88,10 @@ async def root_get(ipaddress: Annotated[Union[IPv4Address,IPv6Address], return locations.pop() response.status_code = status.HTTP_404_NOT_FOUND return GeoLocation() + +@app.get("/") +def root_redirect(req: Request) -> RedirectResponse: + ''' + Redirect empty request using REMOTE_ADDR + ''' + return RedirectResponse(url= str(req.url) + str(req.client.host))