-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
81 lines (65 loc) · 1.77 KB
/
config.go
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
package internal
import (
"fmt"
"reflect"
"sync"
goenv "github.com/Netflix/go-env"
)
// Config struct holds the configuration settings.
// Annotations for environment variables should be added here.
// Example:
//
// type Config struct {
// DatabaseURL string `env:"DATABASE_URL"`
// }
type Config struct {
OpenAIAPIKey string `env:"OPENAI_API_KEY"`
}
// config is a singleton instance of Config
var config *Config
// once is used to ensure the config is initialized only once
var once sync.Once
// GetConfig initializes and returns the singleton instance of Config.
// It unmarshal environment variables into the Config struct and validates it.
func GetConfig() (*Config, error) {
once.Do(func() {
config = &Config{}
// Unmarshal environment variables into the config struct
_, err := goenv.UnmarshalFromEnviron(config)
if err != nil {
// Log and handle the error if unmarshalling fails
Logf("Failed to unmarshal config").
AddError(err).
Error()
return
}
})
// Validate the config after unmarshalling
err := ValidateConfig(config)
return config, err
}
// ValidateConfig checks if the config fields are properly set.
// It ensures that all fields are non-empty.
func ValidateConfig(config *Config) error {
if config == nil {
return fmt.Errorf("config is nil")
}
v := reflect.ValueOf(config)
// Check if config is a pointer and dereference it
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
// Ensure that config is a struct
if v.Kind() != reflect.Struct {
return fmt.Errorf("config is not a struct")
}
// Iterate over all fields of the struct
m := v.NumField()
for i := 0; i < m; i++ {
// Validate that no field is empty
if v.Field(i).String() == "" {
return fmt.Errorf("%s must not be empty", v.Type().Field(i).Name)
}
}
return nil
}