-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfigs.go
136 lines (109 loc) · 3.08 KB
/
configs.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
126
127
128
129
130
131
132
133
134
135
136
package configs
import (
"fmt"
"os"
"github.com/mitchellh/mapstructure"
"github.com/nekomeowww/factorio-rcon-api/v2/internal/meta"
"github.com/nekomeowww/xo"
"github.com/samber/lo"
"github.com/spf13/viper"
)
type APIServer struct {
GrpcServerAddr string `json:"grpc_server_addr" yaml:"grpc_server_addr"`
HttpServerAddr string `json:"http_server_addr" yaml:"http_server_addr"`
}
type Tracing struct {
OtelCollectorHTTP bool `json:"otel_collector_http" yaml:"otel_collector_http"`
OtelStdoutEnabled bool `json:"otel_stdout_enabled" yaml:"otel_stdout_enabled"`
}
type Factorio struct {
RCONHost string `json:"rcon_host" yaml:"rcon_host"`
RCONPort string `json:"rcon_port" yaml:"rcon_port"`
RCONPassword string `json:"rcon_password" yaml:"rcon_password"`
}
type Config struct {
meta.Meta `json:"-" yaml:"-"`
Env string `json:"env" yaml:"env"`
Tracing Tracing `json:"tracing" yaml:"tracing"`
APIServer APIServer `json:"api_server" yaml:"api_server"`
Factorio Factorio `json:"factorio" yaml:"factorio"`
}
func defaultConfig() Config {
return Config{
Tracing: Tracing{
OtelCollectorHTTP: false,
OtelStdoutEnabled: false,
},
APIServer: APIServer{
GrpcServerAddr: ":24181",
HttpServerAddr: ":24180",
},
Factorio: Factorio{
RCONHost: "127.0.0.1",
RCONPort: "27015",
RCONPassword: "",
},
}
}
func NewConfig(namespace string, app string, configFilePath string, envFilePath string) func() (*Config, error) {
return func() (*Config, error) {
configPath := getConfigFilePath(configFilePath)
lo.Must0(viper.BindEnv("env"))
lo.Must0(viper.BindEnv("tracing.otel_collector_http"))
lo.Must0(viper.BindEnv("tracing.otel_stdout_enabled"))
lo.Must0(viper.BindEnv("api_server.grpc_server_bind"))
lo.Must0(viper.BindEnv("api_server.http_server_bind"))
lo.Must0(viper.BindEnv("factorio.rcon_host"))
lo.Must0(viper.BindEnv("factorio.rcon_port"))
lo.Must0(viper.BindEnv("factorio.rcon_password"))
err := loadEnvConfig(envFilePath)
if err != nil {
return nil, err
}
err = readConfig(configPath)
if err != nil {
return nil, err
}
config := defaultConfig()
err = viper.Unmarshal(&config, func(c *mapstructure.DecoderConfig) {
c.TagName = "yaml"
})
if err != nil {
return nil, err
}
// For debugging for users, otherwise it's hard to know what's wrong with the config
fmt.Println("read config:")
xo.PrintJSON(config)
fmt.Println("")
meta.Env = config.Env
if meta.Env == "" {
meta.Env = os.Getenv("ENV")
}
config.Meta.Env = config.Env
config.Meta.App = app
config.Meta.Namespace = namespace
return &config, nil
}
}
func NewTestConfig(envFilePath string) (*Config, error) {
configPath := tryToMatchConfigPathForUnitTest("")
if envFilePath != "" {
err := loadEnvConfig("")
if err != nil {
return nil, err
}
}
err := readConfig(configPath)
if err != nil {
return nil, err
}
config := defaultConfig()
config.Env = "test"
err = viper.Unmarshal(&config, func(c *mapstructure.DecoderConfig) {
c.TagName = "yaml"
})
if err != nil {
return nil, err
}
return &config, nil
}