mirror of
https://github.com/hanzoai/world-zap.git
synced 2026-10-30 14:55:43 +00:00
Real-time Hanzo World feeds over ZAP binary protocol (9-byte header +
JSON payload) fronted by WebSockets at wss://zap.world.hanzo.ai/zap.
IAM bearer auth via Authorization header or ?token= query param.
Validator caches /oauth/userinfo for 5 minutes.
Topics (10): world.events.{all,conflicts,earthquakes,fires},
world.markets.{quotes,crypto}, world.ships.ais, world.aviation.opensky,
world.news.live, world.weather.alerts. Minimum plan enforced per topic.
Tools: subscribe, unsubscribe, list_topics. Admin-only publish for
internal service accounts.
Ingest: streams events from worldmonitor GET /v1/world/events?stream=1,
fans out via bounded-channel hub with drop-on-overflow semantics.
Per-user token bucket rate limits: free 30/min, pro 3000/min, team
15000/min, enterprise 60000/min.
Tests: all packages pass -race (proto, auth, hub, ratelimit, ingest,
handlers). Multi-arch image build wired to hanzoai/.github docker-build
workflow.
70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Config is the runtime configuration.
|
|
type Config struct {
|
|
Addr string
|
|
IAMEndpoint string
|
|
BackendBase string
|
|
ServiceToken string
|
|
AdminOrgs []string
|
|
OtelEndpoint string
|
|
ServiceName string
|
|
LogLevel string
|
|
AuthTTL time.Duration
|
|
}
|
|
|
|
func loadConfig() Config {
|
|
return Config{
|
|
Addr: envOr("ADDR", ":9999"),
|
|
IAMEndpoint: envOr("IAM_ENDPOINT", "https://hanzo.id"),
|
|
BackendBase: envOr("WORLD_BACKEND", "http://world.hanzo.svc"),
|
|
ServiceToken: os.Getenv("WORLD_SERVICE_TOKEN"),
|
|
AdminOrgs: splitCSV(envOr("ADMIN_ORGS", "hanzo")),
|
|
OtelEndpoint: os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"),
|
|
ServiceName: envOr("OTEL_SERVICE_NAME", "world-zap"),
|
|
LogLevel: envOr("LOG_LEVEL", "info"),
|
|
AuthTTL: parseDurationOr("AUTH_CACHE_TTL", 5*time.Minute),
|
|
}
|
|
}
|
|
|
|
func envOr(k, def string) string {
|
|
if v := os.Getenv(k); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|
|
|
|
func parseDurationOr(k string, def time.Duration) time.Duration {
|
|
v := os.Getenv(k)
|
|
if v == "" {
|
|
return def
|
|
}
|
|
if n, err := strconv.Atoi(v); err == nil {
|
|
return time.Duration(n) * time.Second
|
|
}
|
|
d, err := time.ParseDuration(v)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return d
|
|
}
|
|
|
|
func splitCSV(s string) []string {
|
|
parts := strings.Split(s, ",")
|
|
out := parts[:0]
|
|
for _, p := range parts {
|
|
p = strings.TrimSpace(p)
|
|
if p != "" {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|