aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/config/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'backend/internal/config/config.go')
-rw-r--r--backend/internal/config/config.go114
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
+}