config.go raw
1 package main
2
3 import (
4 "os"
5 "strings"
6 "time"
7
8 "go-simpler.org/env"
9 "next.orly.dev/pkg/lol/chk"
10 "next.orly.dev/pkg/lol/log"
11 )
12
13 // Config holds the distributed sync service configuration.
14 type Config struct {
15 // Listen is the gRPC server listen address
16 Listen string `env:"ORLY_SYNC_DISTRIBUTED_LISTEN" default:"127.0.0.1:50061" usage:"gRPC server listen address"`
17
18 // LogLevel is the logging level
19 LogLevel string `env:"ORLY_SYNC_DISTRIBUTED_LOG_LEVEL" default:"info" usage:"log level (trace, debug, info, warn, error)"`
20
21 // Database configuration
22 DBType string `env:"ORLY_SYNC_DISTRIBUTED_DB_TYPE" default:"grpc" usage:"database type: grpc or badger"`
23 GRPCDBServer string `env:"ORLY_SYNC_DISTRIBUTED_DB_SERVER" default:"127.0.0.1:50051" usage:"gRPC database server address"`
24 DataDir string `env:"ORLY_DATA_DIR" usage:"database data directory (for badger mode)"`
25
26 // Sync configuration
27 NodeID string `env:"ORLY_SYNC_DISTRIBUTED_NODE_ID" usage:"node identity (relay pubkey)"`
28 RelayURL string `env:"ORLY_SYNC_DISTRIBUTED_RELAY_URL" usage:"this relay's URL"`
29 PeersRaw string `env:"ORLY_SYNC_DISTRIBUTED_PEERS" usage:"comma-separated peer relay URLs"`
30 SyncInterval time.Duration `env:"ORLY_SYNC_DISTRIBUTED_INTERVAL" default:"5s" usage:"sync poll interval"`
31
32 // NIP-11 cache configuration
33 NIP11CacheTTL time.Duration `env:"ORLY_SYNC_DISTRIBUTED_NIP11_TTL" default:"30m" usage:"NIP-11 cache TTL"`
34
35 // Parsed peers
36 Peers []string
37 }
38
39 // loadConfig loads configuration from environment variables.
40 func loadConfig() *Config {
41 cfg := &Config{}
42 if err := env.Load(cfg, nil); chk.E(err) {
43 log.E.F("failed to load config: %v", err)
44 os.Exit(1)
45 }
46
47 // Parse peers from comma-separated string
48 if cfg.PeersRaw != "" {
49 cfg.Peers = strings.Split(cfg.PeersRaw, ",")
50 for i, p := range cfg.Peers {
51 cfg.Peers[i] = strings.TrimSpace(p)
52 }
53 }
54
55 return cfg
56 }
57