Compare commits
2 Commits
d090e90ee2
...
4eb906f6d4
Author | SHA1 | Date | |
---|---|---|---|
4eb906f6d4 | |||
878a2b6bbf |
13
app/main.py
13
app/main.py
@ -15,12 +15,14 @@ app = FastAPI()
|
||||
GEOLITE2_ASN_DB = '/usr/local/share/GeoIP/GeoLite2-ASN.mmdb'
|
||||
GEOLITE2_CITY_DB = '/usr/local/share/GeoIP/GeoLite2-City.mmdb'
|
||||
|
||||
|
||||
class IPAddressParam(BaseModel):
|
||||
'''
|
||||
Payload entry as used in POST
|
||||
'''
|
||||
ip: Union[IPv6Address, IPv4Address]
|
||||
|
||||
|
||||
class Locality(BaseModel):
|
||||
'''
|
||||
Locality data
|
||||
@ -30,6 +32,7 @@ class Locality(BaseModel):
|
||||
continent: Optional[str] = None
|
||||
is_eu: bool = False
|
||||
|
||||
|
||||
class GeoLocation(BaseModel):
|
||||
'''
|
||||
Geolocation data model
|
||||
@ -40,8 +43,10 @@ class GeoLocation(BaseModel):
|
||||
network: Optional[Union[IPv6Network, IPv4Network]] = None
|
||||
locality: Locality = Locality()
|
||||
|
||||
|
||||
@app.post("/")
|
||||
async def root_post(ipaddresses: Annotated[list[IPAddressParam],
|
||||
async def root_post(ipaddresses: Annotated[
|
||||
list[IPAddressParam],
|
||||
Body(title="The IPAddresses to geolocate")],
|
||||
response: Response
|
||||
) -> list[GeoLocation]:
|
||||
@ -79,20 +84,22 @@ async def root_post(ipaddresses: Annotated[list[IPAddressParam],
|
||||
|
||||
|
||||
@app.get("/{ipaddress}")
|
||||
async def root_get(ipaddress: Annotated[Union[IPv4Address,IPv6Address],
|
||||
async def root_get(ipaddress: Annotated[
|
||||
Union[IPv4Address, IPv6Address],
|
||||
Path(title="The IPAddress to geolocate")],
|
||||
response: Response
|
||||
) -> GeoLocation:
|
||||
'''
|
||||
Look up geolocation for ip in path parameter
|
||||
'''
|
||||
locations = await root_post([IPAddressParam(ip=ipaddress)])
|
||||
locations = await root_post([IPAddressParam(ip=ipaddress)], response)
|
||||
if locations:
|
||||
response.headers['Cache-Control'] = 'private, max-age=604800'
|
||||
return locations.pop()
|
||||
response.status_code = status.HTTP_404_NOT_FOUND
|
||||
return GeoLocation()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def root_redirect(req: Request) -> RedirectResponse:
|
||||
'''
|
||||
|
Loading…
x
Reference in New Issue
Block a user