Compare commits
4 Commits
38416f103f
...
a95dfaae2b
Author | SHA1 | Date | |
---|---|---|---|
a95dfaae2b | |||
4eb906f6d4 | |||
878a2b6bbf | |||
d090e90ee2 |
33
app/main.py
33
app/main.py
@ -15,11 +15,13 @@ app = FastAPI()
|
|||||||
GEOLITE2_ASN_DB = '/usr/local/share/GeoIP/GeoLite2-ASN.mmdb'
|
GEOLITE2_ASN_DB = '/usr/local/share/GeoIP/GeoLite2-ASN.mmdb'
|
||||||
GEOLITE2_CITY_DB = '/usr/local/share/GeoIP/GeoLite2-City.mmdb'
|
GEOLITE2_CITY_DB = '/usr/local/share/GeoIP/GeoLite2-City.mmdb'
|
||||||
|
|
||||||
|
|
||||||
class IPAddressParam(BaseModel):
|
class IPAddressParam(BaseModel):
|
||||||
'''
|
'''
|
||||||
Payload entry as used in POST
|
Payload entry as used in POST
|
||||||
'''
|
'''
|
||||||
ip: Union[IPv6Address,IPv4Address]
|
ip: Union[IPv6Address, IPv4Address]
|
||||||
|
|
||||||
|
|
||||||
class Locality(BaseModel):
|
class Locality(BaseModel):
|
||||||
'''
|
'''
|
||||||
@ -30,20 +32,24 @@ class Locality(BaseModel):
|
|||||||
continent: Optional[str] = None
|
continent: Optional[str] = None
|
||||||
is_eu: bool = False
|
is_eu: bool = False
|
||||||
|
|
||||||
|
|
||||||
class GeoLocation(BaseModel):
|
class GeoLocation(BaseModel):
|
||||||
'''
|
'''
|
||||||
Geolocation data model
|
Geolocation data model
|
||||||
'''
|
'''
|
||||||
ip: Optional[Union[IPv6Address,IPv4Address]] = None
|
ip: Optional[Union[IPv6Address, IPv4Address]] = None
|
||||||
asn: Optional[int] = None
|
asn: Optional[int] = None
|
||||||
asn_org: Optional[str] = None
|
asn_org: Optional[str] = None
|
||||||
network: Optional[Union[IPv6Network,IPv4Network]] = None
|
network: Optional[Union[IPv6Network, IPv4Network]] = None
|
||||||
locality: Locality = Locality()
|
locality: Locality = Locality()
|
||||||
|
|
||||||
|
|
||||||
@app.post("/")
|
@app.post("/")
|
||||||
async def root_post(ipaddresses: Annotated[list[IPAddressParam],
|
async def root_post(ipaddresses: Annotated[
|
||||||
Body(title="The IPAddresses to geolocate")]
|
list[IPAddressParam],
|
||||||
) -> list[GeoLocation]:
|
Body(title="The IPAddresses to geolocate")],
|
||||||
|
response: Response
|
||||||
|
) -> list[GeoLocation]:
|
||||||
'''
|
'''
|
||||||
Return GeoLocation item(s) for a list of IPAddressParam objects
|
Return GeoLocation item(s) for a list of IPAddressParam objects
|
||||||
'''
|
'''
|
||||||
@ -72,26 +78,31 @@ async def root_post(ipaddresses: Annotated[list[IPAddressParam],
|
|||||||
ip=ipaddress.ip
|
ip=ipaddress.ip
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
if geolocations:
|
||||||
|
response.headers['Cache-Control'] = 'private, max-age=604800'
|
||||||
return geolocations
|
return geolocations
|
||||||
|
|
||||||
|
|
||||||
@app.get("/{ipaddress}")
|
@app.get("/{ipaddress}")
|
||||||
async def root_get(ipaddress: Annotated[Union[IPv4Address,IPv6Address],
|
async def root_get(ipaddress: Annotated[
|
||||||
Path(title="The IPAddress to geolocate")],
|
Union[IPv4Address, IPv6Address],
|
||||||
|
Path(title="The IPAddress to geolocate")],
|
||||||
response: Response
|
response: Response
|
||||||
) -> GeoLocation:
|
) -> GeoLocation:
|
||||||
'''
|
'''
|
||||||
Look up geolocation for ip in path parameter
|
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:
|
if locations:
|
||||||
|
response.headers['Cache-Control'] = 'private, max-age=604800'
|
||||||
return locations.pop()
|
return locations.pop()
|
||||||
response.status_code = status.HTTP_404_NOT_FOUND
|
response.status_code = status.HTTP_404_NOT_FOUND
|
||||||
return GeoLocation()
|
return GeoLocation()
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
def root_redirect(req: Request) -> RedirectResponse:
|
def root_redirect(req: Request) -> RedirectResponse:
|
||||||
'''
|
'''
|
||||||
Redirect empty request using REMOTE_ADDR
|
Redirect empty request using REMOTE_ADDR
|
||||||
'''
|
'''
|
||||||
return RedirectResponse(url= str(req.url) + str(req.client.host))
|
return RedirectResponse(url=str(req.url) + str(req.client.host))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user