Compare commits
No commits in common. "4eb906f6d48a535efdb8d37c7778febab00d4e54" and "d090e90ee20c1a53028c92963b496d865e498b19" have entirely different histories.
4eb906f6d4
...
d090e90ee2
13
app/main.py
13
app/main.py
@ -15,14 +15,12 @@ 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):
|
||||||
'''
|
'''
|
||||||
Locality data
|
Locality data
|
||||||
@ -32,7 +30,6 @@ 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
|
||||||
@ -43,10 +40,8 @@ class GeoLocation(BaseModel):
|
|||||||
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[
|
async def root_post(ipaddresses: Annotated[list[IPAddressParam],
|
||||||
list[IPAddressParam],
|
|
||||||
Body(title="The IPAddresses to geolocate")],
|
Body(title="The IPAddresses to geolocate")],
|
||||||
response: Response
|
response: Response
|
||||||
) -> list[GeoLocation]:
|
) -> list[GeoLocation]:
|
||||||
@ -84,22 +79,20 @@ async def root_post(ipaddresses: Annotated[
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/{ipaddress}")
|
@app.get("/{ipaddress}")
|
||||||
async def root_get(ipaddress: Annotated[
|
async def root_get(ipaddress: Annotated[Union[IPv4Address,IPv6Address],
|
||||||
Union[IPv4Address, IPv6Address],
|
|
||||||
Path(title="The IPAddress to geolocate")],
|
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)], response)
|
locations = await root_post([IPAddressParam(ip=ipaddress)])
|
||||||
if locations:
|
if locations:
|
||||||
response.headers['Cache-Control'] = 'private, max-age=604800'
|
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:
|
||||||
'''
|
'''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user