aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/config/config.go
blob: c37588b0940bc5278d95994c9d63ef6d7a65c3b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
}