Port of wttr.in
Find a file
Emil Lerch 6e829eea30
All checks were successful
Generic zig build / build (push) Successful in 1m17s
Generic zig build / deploy (push) Successful in 13s
handle client ip without forwarding headers
2026-01-05 16:13:59 -08:00
.forgejo/workflows reference environment variable with actions syntax 2026-01-05 14:02:27 -08:00
build use git version in metno identifier 2026-01-05 13:34:37 -08:00
docker add ca-certificates for inclusion in docker image 2026-01-05 14:07:24 -08:00
scripts remove left over dead shell script 2026-01-05 10:06:10 -08:00
src handle client ip without forwarding headers 2026-01-05 16:13:59 -08:00
.gitignore remove GeoLite2 Db from gitignore - now stored in cache dir 2026-01-05 12:28:39 -08:00
.mise.toml clean up .gitignore/main.zig 2025-12-18 14:57:52 -08:00
.pre-commit-config.yaml reorder pre commit config to go fastest -> slowest 2025-12-19 14:38:33 -08:00
.travis.yml travis: pyflake only bin and lib 2019-12-29 17:50:14 +01:00
API_ENDPOINTS.md move zig to base, python/go out to legacy 2025-12-18 12:24:21 -08:00
ARCHITECTURE.md move zig to base, python/go out to legacy 2025-12-18 12:24:21 -08:00
build.zig use git version in metno identifier 2026-01-05 13:34:37 -08:00
build.zig.zon correctly pin openflights to specific commit 2026-01-05 13:56:39 -08:00
CACHE_CONFIGURATION.md ip2location 2026-01-05 00:39:24 -08:00
DATA_FLOW.md move zig to base, python/go out to legacy 2025-12-18 12:24:21 -08:00
IMPERIAL_UNITS.md add forecast data, feels like fields (leaking test) 2026-01-02 14:03:20 -08:00
LICENSE Initial commit 2015-12-26 23:03:43 +02:00
README.md ip2location 2026-01-05 00:39:24 -08:00
REWRITE_STRATEGY.md move zig to base, python/go out to legacy 2025-12-18 12:24:21 -08:00
TARGET_ARCHITECTURE.md timezone handling for metno 2026-01-04 22:47:12 -08:00

wttr.in Zig Rewrite Documentation

This directory contains comprehensive documentation for rewriting wttr.in in Zig.

Quick Start

# Minimal setup (uses defaults)
./wttr

# With IP2Location fallback (optional)
IP2LOCATION_API_KEY=your_key_here ./wttr

# Custom cache location
WTTR_CACHE_DIR=/var/cache/wttr ./wttr

See CACHE_CONFIGURATION.md for detailed configuration options.

External Services & API Keys

Required Services (No API Key)

  • Met.no Weather API - Primary weather data provider
    • Free, open API from Norwegian Meteorological Institute
    • No registration required
    • Rate limit: Be respectful, use caching (built-in)

Optional Services

  • IP2Location.io - Fallback IP geolocation
    • API Key Required: Sign up at https://www.ip2location.io/
    • Free tier: 30,000 requests/month
    • Only used when MaxMind GeoIP database lookup fails
    • Set via IP2LOCATION_API_KEY environment variable

Database Files (Auto-Downloaded)

  • MaxMind GeoLite2 City - IP geolocation database
    • Free database, automatically downloaded if missing
    • No API key required
    • Stored in ~/.cache/wttr/GeoLite2-City.mmdb by default

Current Implementation Status

Implemented Features

  • HTTP server with routing and middleware
  • Rate limiting
  • Caching system
  • GeoIP database integration (libmaxminddb)
  • Location resolver with multiple input types
  • Weather provider (Met.no) with timezone-aware forecasts; core data structures use zeit.Time/zeit.Date for type-safe date/time handling
  • Output formats: ANSI, line (1-4), JSON (j1), v2 data-rich, custom (%)
  • Query parameter parsing
  • Static help pages (/:help, /:translation)
  • Error handling (404/500 status codes)
  • Configuration from environment variables
  • Imperial units auto-detection: Automatically uses imperial units (°F, mph) for US IP addresses and lang=us, with explicit ?u and ?m overrides
  • IP2Location fallback: Optional fallback geolocation service with persistent cache

Missing Features (To Be Implemented Later)

  1. Prometheus Metrics Format (format=p1)

    • Export weather data in Prometheus metrics format
    • See API_ENDPOINTS.md for format specification
  2. PNG Generation

    • Render weather reports as PNG images
    • Support transparency and custom styling
    • Requires image rendering library integration
  3. Multiple Locations Support

    • Handle colon-separated locations (e.g., London:Paris:Berlin)
    • Process and display weather for multiple cities in one request
  4. Language/Localization

    • Accept-Language header parsing
    • lang query parameter support
    • Translation of weather conditions and text (54 languages)
  5. Moon Phase Calculation

    • Real moon phase computation based on date
    • Moon phase emoji display
    • Moonday calculation
  6. Astronomical Times

    • Calculate dawn, sunrise, zenith, sunset, dusk times
    • Based on location coordinates and timezone
    • Display in custom format output

Documentation Files

CACHE_CONFIGURATION.md NEW

Complete cache and external services documentation:

  • External services (Met.no, IP2Location, MaxMind GeoLite2)
  • API key requirements
  • Cache locations and policies
  • Expiration and eviction strategies
  • Environment variables
  • Configuration examples

TARGET_ARCHITECTURE.md

Target architecture for Zig rewrite:

  • Single binary design
  • Simplified caching (one layer)
  • Pluggable weather provider interface
  • Rate limiting middleware
  • Module structure
  • Testing strategy
  • Performance targets

ARCHITECTURE.md

Current system architecture documentation:

  • Component breakdown (Go proxy, Python backend, static assets)
  • Request flow diagrams
  • API endpoints
  • External dependencies
  • Caching strategy
  • Configuration
  • Known issues

API_ENDPOINTS.md

Complete API reference:

  • All endpoints with examples
  • Query parameters
  • Output formats
  • Language support
  • HTTP headers
  • Rate limits
  • Usage examples

DATA_FLOW.md

Detailed request processing flow:

  • Step-by-step request handling
  • Location resolution process
  • Weather data fetching
  • Rendering pipeline
  • Caching mechanisms
  • Error handling
  • Performance optimizations

REWRITE_STRATEGY.md

Comprehensive rewrite plan:

  • Goals and non-goals
  • Phase-by-phase breakdown (10-11 weeks)
  • Module organization
  • Testing strategy
  • Risk mitigation
  • Success criteria
  • Alternative approaches

Quick Start

  1. Read ARCHITECTURE.md - Understand the current system
  2. Read DATA_FLOW.md - Understand how requests are processed
  3. Read REWRITE_STRATEGY.md - Understand the rewrite plan
  4. Refer to API_ENDPOINTS.md - When implementing specific endpoints

Current System Summary

Architecture: Hybrid Python/Go

  • Go proxy (port 8082): LRU caching, prefetching
  • Python backend (port 8002): Weather logic, rendering
  • External binaries: wego (Go), pyphoon (Python)
  • External services: Geolocator (port 8004)

Key Stats:

  • ~5000 lines of Python
  • ~400 lines of Go
  • 54 languages supported
  • 12,800 entry LRU cache
  • 1000-2000s cache TTL
  • Zero unit tests (only integration tests)

Main Challenges:

  1. ANSI weather rendering (currently done by wego)
  2. PNG image generation (PIL + pyte)
  3. GeoIP database parsing (MaxMind format)
  4. 54 language translations
  5. Multiple output formats (ANSI, HTML, PNG, JSON, Prometheus)

Option A: Incremental (Recommended)

  1. Replace Go proxy with Zig (2 weeks)
  2. Test and deploy
  3. Replace Python backend with Zig (8-9 weeks)
  4. Full cutover

Option B: Full Rewrite

  • All at once (10-11 weeks)
  • Higher risk, but cleaner result

Next Steps

  1. Review documentation
  2. Choose rewrite strategy
  3. Set up Zig project structure
  4. Begin implementation (start with HTTP server or Go proxy)

Questions?

See REWRITE_STRATEGY.md "Questions to Answer" section for key decisions needed.