Monorepo for Tangled tangled.org
1package config 2 3import ( 4 "context" 5 "fmt" 6 "net" 7 "os" 8 "path" 9 10 "github.com/bluesky-social/indigo/atproto/syntax" 11 "github.com/sethvargo/go-envconfig" 12 "gopkg.in/yaml.v3" 13) 14 15type Config struct { 16 Dev bool `yaml:"dev"` 17 HostName string `yaml:"hostname"` 18 OwnerDid syntax.DID `yaml:"owner_did"` 19 ListenHost string `yaml:"listen_host"` 20 ListenPort string `yaml:"listen_port"` 21 DataDir string `yaml:"data_dir"` 22 RepoDir string `yaml:"repo_dir"` 23 PlcUrl string `yaml:"plc_url"` 24 JetstreamEndpoint string `yaml:"jetstream_endpoint"` 25 AppviewEndpoint string `yaml:"appview_endpoint"` 26 GitUserName string `yaml:"git_user_name"` 27 GitUserEmail string `yaml:"git_user_email"` 28 OAuth OAuthConfig 29} 30 31type OAuthConfig struct { 32 CookieSecret string `env:"KNOT2_COOKIE_SECRET, default=00000000000000000000000000000000"` 33 ClientSecret string `env:"KNOT2_OAUTH_CLIENT_SECRET"` 34 ClientKid string `env:"KNOT2_OAUTH_CLIENT_KID"` 35} 36 37func (c *Config) Uri() string { 38 // TODO: make port configurable 39 if c.Dev { 40 return "http://127.0.0.1:6444" 41 } 42 return "https://" + c.HostName 43} 44 45func (c *Config) ListenAddr() string { 46 return net.JoinHostPort(c.ListenHost, c.ListenPort) 47} 48 49func (c *Config) DbPath() string { 50 return path.Join(c.DataDir, "knot.db") 51} 52 53func (c *Config) GitMotdFilePath() string { 54 return path.Join(c.DataDir, "motd") 55} 56 57func (c *Config) Validate() error { 58 if c.HostName == "" { 59 return fmt.Errorf("knot hostname cannot be empty") 60 } 61 if c.OwnerDid == "" { 62 return fmt.Errorf("knot owner did cannot be empty") 63 } 64 return nil 65} 66 67func Load(ctx context.Context, path string) (Config, error) { 68 // NOTE: yaml.v3 package doesn't support "default" struct tag 69 cfg := Config{ 70 Dev: true, 71 ListenHost: "0.0.0.0", 72 ListenPort: "5555", 73 DataDir: "/home/git", 74 RepoDir: "/home/git", 75 PlcUrl: "https://plc.directory", 76 JetstreamEndpoint: "wss://jetstream1.us-west.bsky.network/subscribe", 77 AppviewEndpoint: "https://tangled.org", 78 GitUserName: "Tangled", 79 GitUserEmail: "noreply@tangled.org", 80 } 81 // load config from env vars 82 err := envconfig.Process(ctx, &cfg.OAuth) 83 if err != nil { 84 return cfg, err 85 } 86 87 // load config from toml config file 88 bytes, err := os.ReadFile(path) 89 if err != nil { 90 return cfg, err 91 } 92 if err := yaml.Unmarshal(bytes, &cfg); err != nil { 93 return cfg, err 94 } 95 96 // validate the config 97 if err = cfg.Validate(); err != nil { 98 return cfg, err 99 } 100 return cfg, nil 101}