pg_orrery/search/scripts/update_geoip.sh
Ryan Malloy d22d451c69 Add observer location awareness to chat widget
Progressive enhancement chain: GeoIP auto-detect -> browser GPS ->
manual entry -> works without any location. When set, the observer
coordinates are injected into chat requests so the LLM can answer
"Where is Jupiter?" with actual azimuth/elevation from the user's
location instead of placeholder coordinates.

Backend:
- GeoIP service (MaxMind GeoLite2-City) with lazy init, private IP
  filtering, and IPv4-mapped IPv6 unwrapping
- GET /api/geolocate endpoint (sync to avoid blocking event loop on
  mmap I/O, rightmost X-Forwarded-For for Caddy trust chain)
- ObserverContext model on both chat endpoints with shared
  _observer_prefix() helper that sanitizes label against prompt
  injection

Frontend:
- Location bar between header and messages with pin icon, GPS button,
  edit/clear controls, and inline manual entry parser (accepts
  "40.7N 74.0W", decimal lat/lon, pg_orrery observer format)
- GeoIP auto-detect on first visit, localStorage persistence
- Observer coordinates sent with every chat request

Infrastructure:
- api-data volume for GeoIP database, Caddy handle_4 for /api/geolocate
- update_geoip.sh using MaxMind Basic auth (key stays out of ps/proc)
2026-03-01 23:34:14 -07:00

43 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Download MaxMind GeoLite2-City database.
# Requires MAXMIND_ACCOUNT_ID and MAXMIND_LICENSE_KEY environment variables (free tier).
# Usage: MAXMIND_ACCOUNT_ID=xxx MAXMIND_LICENSE_KEY=xxx bash scripts/update_geoip.sh [output_dir]
set -euo pipefail
OUTPUT_DIR="${1:-/data/geoip}"
DB_NAME="GeoLite2-City"
if [ -z "${MAXMIND_LICENSE_KEY:-}" ]; then
echo "Error: MAXMIND_LICENSE_KEY not set" >&2
echo "Get a free license key at https://www.maxmind.com/en/geolite2/signup" >&2
exit 1
fi
if [ -z "${MAXMIND_ACCOUNT_ID:-}" ]; then
echo "Error: MAXMIND_ACCOUNT_ID not set" >&2
exit 1
fi
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
echo "Downloading ${DB_NAME}..."
# Use HTTP Basic auth to keep the license key out of the URL/process list
curl -fsSL \
-u "${MAXMIND_ACCOUNT_ID}:${MAXMIND_LICENSE_KEY}" \
"https://download.maxmind.com/geoip/databases/${DB_NAME}/download?suffix=tar.gz" \
-o "$TMPDIR/${DB_NAME}.tar.gz"
echo "Extracting..."
tar -xzf "$TMPDIR/${DB_NAME}.tar.gz" -C "$TMPDIR"
MMDB=$(find "$TMPDIR" -name "${DB_NAME}.mmdb" -type f | head -1)
if [ -z "$MMDB" ]; then
echo "Error: ${DB_NAME}.mmdb not found in archive" >&2
exit 1
fi
mkdir -p "$OUTPUT_DIR"
cp "$MMDB" "$OUTPUT_DIR/${DB_NAME}.mmdb"
echo "Installed: $OUTPUT_DIR/${DB_NAME}.mmdb ($(du -h "$OUTPUT_DIR/${DB_NAME}.mmdb" | cut -f1))"