diff options
| -rw-r--r-- | iss/geo.py | 24 | ||||
| -rw-r--r-- | iss/server.py | 16 | ||||
| -rw-r--r-- | iss/templates/index.html | 1 | ||||
| -rw-r--r-- | iss/tests/test_geo.py | 26 | ||||
| -rw-r--r-- | iss/tests/test_geoip.py | 19 |
5 files changed, 58 insertions, 28 deletions
diff --git a/iss/geo.py b/iss/geo.py new file mode 100644 index 0000000..55c8f02 --- /dev/null +++ b/iss/geo.py @@ -0,0 +1,24 @@ +from collections import namedtuple +import geoip2.database +import geoip2.errors + +from .config import GEOIP_CITY_PATH + +DEFAULT_LOCATION = { + "latitude": 34.7641, + "longitude": 32.0669, + "time_zone": "Asia/Jerusalem", +} + +default_location = namedtuple("Location", DEFAULT_LOCATION.keys())( + *DEFAULT_LOCATION.values() +) + + +def get_location(ip): + with geoip2.database.Reader(GEOIP_CITY_PATH) as reader: + try: + res = reader.city(ip) + return res.location + except geoip2.errors.AddressNotFoundError: + return default_location diff --git a/iss/server.py b/iss/server.py index e1bbfe4..e6e59b7 100644 --- a/iss/server.py +++ b/iss/server.py @@ -1,8 +1,9 @@ -from fastapi import FastAPI, Request, Response, Header +from fastapi import FastAPI, Request, Header from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from typing import Optional +from .geo import get_location from .predictions import Predictions app = FastAPI() @@ -13,10 +14,12 @@ templates = Jinja2Templates(directory="iss/templates") @app.get("/") -async def home(request: Request): - preds = Predictions(34.7641, 32.0669, altitude=0, days=5).get_grouped_predictions() +async def home(request: Request, cf_connecting_ip: Optional[str] = Header(None)): + client_ip = cf_connecting_ip or request.client.host + location = get_location(client_ip) + # preds = Predictions(lat, lng, tz=tz, altitude=0, days=5).get_grouped_predictions() return templates.TemplateResponse( - "index.html", {"request": request, "predictions": preds} + "index.html", {"request": request, "location": location} ) @@ -31,8 +34,3 @@ async def passes(request: Request, lat: float, lng: float): "location": {"lat": lat, "lng": lng}, }, ) - - -@app.get("/ip") -async def ip(request: Request, cf_connecting_ip: Optional[str] = Header(None)): - return Response(cf_connecting_ip or request.client.host) diff --git a/iss/templates/index.html b/iss/templates/index.html index e48212a..20634e0 100644 --- a/iss/templates/index.html +++ b/iss/templates/index.html @@ -1,4 +1,5 @@ {% extends "base.html" %} {% block content %} +<a href="/passes/{{ location.latitude }}/{{ location.longitude }}" class="link light-blue dim">{{ location.latitude }}, {{ location.longitude }}, {{ location.time_zone }}</a> {% endblock %} diff --git a/iss/tests/test_geo.py b/iss/tests/test_geo.py new file mode 100644 index 0000000..c63c2d6 --- /dev/null +++ b/iss/tests/test_geo.py @@ -0,0 +1,26 @@ +import pytest + +from ..config import GEOIP_CITY_PATH +from ..geo import get_location + +geoip_required = pytest.mark.skipif( + not GEOIP_CITY_PATH, reason="Path to GeoLite2 city database must be provided" +) + + +@geoip_required +def test_geo(): + ip = "2.52.2.52" + location = get_location(ip) + assert location.latitude == 31.5 + assert location.longitude == 34.75 + assert location.time_zone == "Asia/Jerusalem" + + +@geoip_required +def test_geo_localhost(): + ip = "127.0.0.1" + location = get_location(ip) + assert location.latitude == 34.7641 + assert location.longitude == 32.0669 + assert location.time_zone == "Asia/Jerusalem" diff --git a/iss/tests/test_geoip.py b/iss/tests/test_geoip.py deleted file mode 100644 index aa3f37d..0000000 --- a/iss/tests/test_geoip.py +++ /dev/null @@ -1,19 +0,0 @@ -import pytest -import geoip2.database - -from ..config import GEOIP_CITY_PATH - -geoip_required = pytest.mark.skipif( - not GEOIP_CITY_PATH, reason="Path to GeoLite2 city database must be provided" -) - - -@geoip_required -def test_geo(): - ip = "2.52.2.52" - with geoip2.database.Reader(GEOIP_CITY_PATH) as reader: - res = reader.city(ip) - location = res.location - assert location.latitude == 31.5 - assert location.longitude == 34.75 - assert location.time_zone == "Asia/Jerusalem" |
