diff options
author | 2024-08-11 13:15:50 -0700 | |
---|---|---|
committer | 2024-08-11 13:15:50 -0700 | |
commit | 6a3c21fb0b1c126849f2bbff494403bbe901448e (patch) | |
tree | 5d7805524357c2c8a9819c39d2051a4e3633a1d5 /backend/internal/config/config.go | |
parent | 29c6040a51616e9e4cf6c70ee16391b2a3b238c9 (diff) | |
parent | f34b92ded11b07f78575ac62c260a380c468e5ea (diff) | |
download | ibd-trader-6a3c21fb0b1c126849f2bbff494403bbe901448e.tar.gz ibd-trader-6a3c21fb0b1c126849f2bbff494403bbe901448e.tar.zst ibd-trader-6a3c21fb0b1c126849f2bbff494403bbe901448e.zip |
Merge remote-tracking branch 'backend/main'
Diffstat (limited to 'backend/internal/config/config.go')
-rw-r--r-- | backend/internal/config/config.go | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go new file mode 100644 index 0000000..c37588b --- /dev/null +++ b/backend/internal/config/config.go @@ -0,0 +1,114 @@ +package config + +import ( + "github.com/ansg191/ibd-trader-backend/internal/keys" + "github.com/ansg191/ibd-trader-backend/internal/leader/manager/ibd" + + "github.com/spf13/viper" +) + +type Config struct { + // Logging configuration + Log struct { + // Log level + Level string + // Add source info to log messages + AddSource bool + // Enable colorized output + Color bool + } + // Database configuration + DB struct { + // Database URL + URL string + } + // Redis configuration + Redis struct { + // Redis address + Addr string + // Redis password + Password string + } + // KMS configuration + KMS struct { + // GCP KMS configuration + GCP *keys.GCPKeyName + } + // Server configuration + Server struct { + // Server port + Port uint16 + } + // OAuth 2.0 configuration + Auth struct { + // OAuth 2.0 domain + Domain string + // OAuth 2.0 client ID + ClientID string + // OAuth 2.0 client secret + ClientSecret string + // OAuth 2.0 callback URL + CallbackURL string + } + // IBD configuration + IBD struct { + // Scraper API Key + APIKey string + // Proxy URL + ProxyURL string + // Scrape schedules. In cron format. + Schedules ibd.Schedules + } + // Analyzer configuration + Analyzer struct { + // Use OpenAI for analysis + OpenAI *struct { + // OpenAI API Key + APIKey string + } + } +} + +func New() (*Config, error) { + v := viper.New() + + v.SetDefault("log.level", "INFO") + v.SetDefault("log.addSource", false) + v.SetDefault("log.color", false) + v.SetDefault("server.port", 8000) + + v.SetConfigName("config") + v.AddConfigPath("/etc/ibd-trader/") + v.AddConfigPath("$HOME/.ibd-trader") + v.AddConfigPath(".") + err := v.ReadInConfig() + if err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + // Config file not found; ignore error + } else { + return nil, err + } + } + + v.MustBindEnv("db.url", "DATABASE_URL") + v.MustBindEnv("redis.addr", "REDIS_ADDR") + v.MustBindEnv("redis.password", "REDIS_PASSWORD") + v.MustBindEnv("log.level", "LOG_LEVEL") + v.MustBindEnv("server.port", "SERVER_PORT") + v.MustBindEnv("auth.domain", "AUTH_DOMAIN") + v.MustBindEnv("auth.clientID", "AUTH_CLIENT_ID") + v.MustBindEnv("auth.clientSecret", "AUTH_CLIENT_SECRET") + v.MustBindEnv("auth.callbackURL", "AUTH_CALLBACK_URL") + + config := new(Config) + err = v.Unmarshal(config) + if err != nil { + return nil, err + } + + return config, config.assert() +} + +func (c *Config) assert() error { + return nil +} |