// Package config loads Updraft's runtime settings from environment variables // and its feed definitions from a TOML file. package config import ( "errors" "fmt" ) // Env holds the settings Updraft reads from environment variables. type Env struct { // Hostname is the public host name Updraft is served from (behind a reverse proxy). Hostname string // ServiceDID identifies this feed generator service. ServiceDID string // PublisherDID is the account whose repo holds the app.bsky.feed.generator records. PublisherDID string // ListenAddr is the address the HTTP server binds to. ListenAddr string // DBPath is the path to the SQLite database file. DBPath string // FeedsFile is the path to the feeds.toml file. FeedsFile string // JetstreamHost is the Jetstream instance to consume. JetstreamHost string } // LoadEnv builds an Env using getenv (normally os.Getenv) to look up each variable. func LoadEnv(getenv func(string) string) (Env, error) { lookup := func(name, fallback string) string { if value := getenv(name); value != "" { return value } return fallback } env := Env{ Hostname: getenv("UPDRAFT_HOSTNAME"), PublisherDID: getenv("UPDRAFT_PUBLISHER_DID"), ListenAddr: lookup("UPDRAFT_LISTEN_ADDR", "127.0.0.1:3000"), DBPath: lookup("UPDRAFT_DB_PATH", "updraft.db"), FeedsFile: lookup("UPDRAFT_FEEDS_FILE", "feeds.toml"), JetstreamHost: lookup("UPDRAFT_JETSTREAM_HOST", "jetstream.us-east.bsky.network"), } var errs []error if env.Hostname == "" { errs = append(errs, errors.New("UPDRAFT_HOSTNAME is required")) } if env.PublisherDID == "" { errs = append(errs, errors.New("UPDRAFT_PUBLISHER_DID is required")) } if err := errors.Join(errs...); err != nil { return Env{}, err } env.ServiceDID = lookup("UPDRAFT_SERVICE_DID", fmt.Sprintf("did:web:%s", env.Hostname)) return env, nil }