-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
125 lines (107 loc) · 3.68 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
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
115
116
117
118
119
120
121
122
123
124
125
package module_runner
import (
"context"
"fmt"
"github.com/rs/zerolog/log"
"github.com/sethvargo/go-envconfig"
"gopkg.in/yaml.v3"
"os"
"path"
"time"
)
/************************************************************************/
// RUNNER CONFIG
/************************************************************************/
type config struct {
BinBasePath string `yaml:"bin_base_path"`
Health *configHealth `yaml:"health"`
Log *configLog `yaml:"logging"`
Sparks []*configSpark `yaml:"sparks"`
Nats *configNats `yaml:"nats"`
IOServer *ioServer `yaml:"io_server"`
}
func defaultConfig() *config {
return &config{
BinBasePath: "./",
}
}
type configSpark struct {
Id string `yaml:"id"` // Id is unique hash to identify this combination of Name and Config
Name string `yaml:"name"` // Name of the binary to execute
NatsRequestSubject string `yaml:"nats_request_subject"`
NatsResponseSubject string `yaml:"nats_response_subject"`
NatsRequestStreamName string `yaml:"nats_request_stream_name"`
NatsResponseStreamName string `yaml:"nats_response_stream_name"`
NatsBucket string `yaml:"nats_bucket"`
RetryCount uint `yaml:"retry_count"`
RetryBackoff time.Duration `yaml:"retry_backoff"`
RetryBackoffMultiplier uint `yaml:"retry_backoff_multiplier"`
Timeout time.Duration `yaml:"timeout"`
Config string `yaml:"config"` // Config Deprecated: will be JSON string with config details
ConfigServer *configServer `yaml:"config_server"` // ConfigServer which is used to retrieve startup config
StartupTimeout *time.Duration `yaml:"startup_timeout"` // StartupTimeout amount of time to wait for spark to start before error
}
type configServer struct {
Url string `yaml:"url" json:"url,omitempty"`
ApiKey string `yaml:"api_key" json:"api_key,omitempty"`
}
type configHealth struct {
Enabled bool `env:"HEALTH_ENABLED" yaml:"enabled"`
Bind string `env:"SERVER_BIND" yaml:"bind"`
Port int `env:"HEALTH_PORT" yaml:"port"`
}
type configLog struct {
Level string `env:"LOG_LEVEL" yaml:"level"`
}
type configNats struct {
Address string `yaml:"address"`
}
type ioServer struct {
Url string `env:"IO_SERVER_URL" yaml:"url"`
ApiKey string `env:"IO_SERVER_API_KEY" yaml:"api_key"`
}
func (m *config) healthBindTo() string {
return fmt.Sprintf("%s:%d", m.Health.Bind, m.Health.Port)
}
func LoadModuleConfig(opts ...ModuleOption) (*config, error) {
config := defaultConfig()
if os.Getenv("MODULE_FILE_PATH") != "" {
b, err := os.ReadFile(os.Getenv("MODULE_FILE_PATH"))
if err != nil {
return nil, err
}
log.Info().Msgf("CONFIG %s", string(b))
if err := yaml.Unmarshal(b, &config); err != nil {
return nil, err
}
return config, nil
}
if os.Getenv("MODULE_SECRET") != "" {
if err := yaml.Unmarshal([]byte(os.Getenv("MODULE_SECRET")), &config); err != nil {
return nil, err
}
return config, nil
}
// Note: this is not run if env vars are used (above)
mo := moduleOpts{}
for _, opt := range opts {
opt(&mo)
}
config.BinBasePath = mo.binBasePath
// check for a yaml config
sparkPath := path.Join(mo.configBasePath, "module.yaml")
if _, err := os.Stat(sparkPath); err == nil {
b, err := os.ReadFile(sparkPath)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(b, &config); err != nil {
return nil, err
}
}
if err := envconfig.Process(context.Background(), config); err != nil {
fmt.Printf("error loading configuration: %s", err.Error())
return nil, err
}
return config, nil
}