Discover books, shows, and movies at your level. Track your progress by filling your Shelf with what you find, and share with other language learners. *No dusting required. shlf.space
at master 62 lines 1.4 kB view raw
1package config 2 3import ( 4 "context" 5 "fmt" 6 "net/url" 7 8 "github.com/sethvargo/go-envconfig" 9) 10 11type CoreConfig struct { 12 CookieSecret string `env:"COOKIE_SECRET, default=00000000000000000000000000000000"` 13 Dev bool `env:"DEV, default=false"` 14 ListenAddr string `env:"PORT, default=0.0.0.0:8080"` 15 Host string `env:"HOST, default=https://shlf.space"` 16} 17 18type OAuthConfig struct { 19 ClientSecret string `env:"CLIENT_SECRET"` 20 ClientKid string `env:"CLIENT_KID"` 21} 22 23type RedisConfig struct { 24 Addr string `env:"ADDR, default=localhost:6379"` 25 Password string `env:"PASS"` 26 DB int `env:"DB, default=0"` 27} 28 29func (cfg RedisConfig) ToURL() string { 30 u := &url.URL{ 31 Scheme: "redis", 32 Host: cfg.Addr, 33 Path: fmt.Sprintf("/%d", cfg.DB), 34 } 35 36 if cfg.Password != "" { 37 u.User = url.UserPassword("", cfg.Password) 38 } 39 40 return u.String() 41} 42 43type JetstreamConfig struct { 44 Endpoint string `env:"ENDPOINT, default=wss://jetstream1.us-east.bsky.network/subscribe"` 45} 46 47type Config struct { 48 Core CoreConfig `env:",prefix=SHLF_"` 49 Jetstream JetstreamConfig `env:",prefix=SHLF_JETSTREAM_"` 50 OAuth OAuthConfig `env:",prefix=SHLF_OAUTH_"` 51 Redis RedisConfig `env:",prefix=SHLF_REDIS_"` 52} 53 54func LoadConfig(ctx context.Context) (*Config, error) { 55 var cfg Config 56 err := envconfig.Process(ctx, &cfg) 57 if err != nil { 58 return nil, err 59 } 60 61 return &cfg, nil 62}