-
Notifications
You must be signed in to change notification settings - Fork 0
/
viper.go
140 lines (125 loc) · 4.04 KB
/
viper.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
137
138
139
140
package goconfig
import (
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/spf13/viper"
)
// ViperConfig wraps config and implements ConfigurationLoader.
type ViperConfig struct {
*Config
}
// NewViperConfig will setup and return viper configuration that
// implements goconfig.ConfigurationLoader.
func NewViperConfig(appname string) *ViperConfig {
viper.SetConfigName("config")
viper.SetConfigType("ini")
viper.AddConfigPath(fmt.Sprintf("/etc/%s/", appname))
viper.AddConfigPath(fmt.Sprintf("$HOME/.%s", appname))
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
if errors.Is(err, viper.ConfigFileNotFoundError{}) {
// Config file not found
viper.Set("debug", false)
viper.Set("port", 1323)
} else {
log.Fatalf("Fatal error config file: %s", err)
}
}
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
return &ViperConfig{
Config: &Config{
httpClients: map[string]HTTPClientConfig{},
},
}
}
// WithServer will setup the web server configuration if required.
func (c *ViperConfig) WithServer() ConfigurationLoader {
c.Server = &Server{
Port: viper.GetString(EnvServerPort),
Hostname: viper.GetString(EnvServerHost),
TLSEnabled: viper.GetBool(EnvServerTLSEnabled),
TLSCertPath: viper.GetString(EnvServerTLSCert),
PProfEnabled: viper.GetBool(EnvServerPprofEnabled),
}
return c
}
// WithEnvironment sets up the deployment configuration if required.
func (c *ViperConfig) WithEnvironment(appName string) ConfigurationLoader {
viper.SetDefault(EnvEnvironment, "dev")
viper.SetDefault(EnvRegion, "test")
viper.SetDefault(EnvCommit, "test")
viper.SetDefault(EnvVersion, "test")
viper.SetDefault(EnvBuildDate, time.Now().UTC())
c.Deployment = &Deployment{
Environment: viper.GetString(EnvEnvironment),
Region: viper.GetString(EnvRegion),
Version: viper.GetString(EnvVersion),
Commit: viper.GetString(EnvCommit),
BuildDate: viper.GetTime(EnvBuildDate),
AppName: appName,
}
return c
}
// WithLog sets up logger config from environment variables.
func (c *ViperConfig) WithLog() ConfigurationLoader {
c.Logging = &Logging{Level: viper.GetString(EnvLogLevel)}
return c
}
// WithDb sets up and returns database configuration.
func (c *ViperConfig) WithDb() ConfigurationLoader {
c.Db = &Db{
Type: DbType(viper.GetString(EnvDb)),
Dsn: viper.GetString(EnvDbDsn),
SchemaPath: viper.GetString(EnvDbSchema),
Migrate: viper.GetBool(EnvDbMigrate),
}
return c
}
// WithRedis will include redis config.
func (c *ViperConfig) WithRedis() ConfigurationLoader {
if !viper.IsSet(EnvRedisDb) {
viper.SetDefault(EnvRedisDb, 0)
}
c.Redis = &Redis{
Address: viper.GetString(EnvRedisAddress),
Password: viper.GetString(EnvRedisPassword),
Db: viper.GetUint(EnvRedisDb),
}
return c
}
// WithHTTPClient will setup a custom http client referenced by name.
func (c *ViperConfig) WithHTTPClient(name string) ConfigurationLoader {
c.httpClients[name] = HTTPClientConfig{
Host: viper.GetString(fmt.Sprintf(EnvHTTPClientHost, name)),
Port: viper.GetString(fmt.Sprintf(EnvHTTPClientPort, name)),
TLSEnabled: viper.GetBool(fmt.Sprintf(EnvHTTPClientTLSEnabled, name)),
TLSCert: viper.GetBool(fmt.Sprintf(EnvHTTPClientTLSCert, name)),
Timeout: time.Second * time.Duration(viper.GetInt(fmt.Sprintf(EnvHTTPClientTimeout, name))),
}
return c
}
// WithSwagger will setup and return swagger configuration.
func (c *ViperConfig) WithSwagger() ConfigurationLoader {
c.Swagger = &Swagger{
Host: viper.GetString(EnvSwaggerHost),
Enabled: viper.GetBool(EnvSwaggerEnabled),
}
return c
}
// WithInstrumentation will read instrumentation environment vars.
func (c *ViperConfig) WithInstrumentation() ConfigurationLoader {
c.Instrumentation = &Instrumentation{
MetricsEnabled: viper.GetBool(EnvMetricsEnabled),
TracingEnabled: viper.GetBool(EnvTracingEnabled),
}
return c
}
// Load will finish setup and return configuration. This should
// always be the last call.
func (c *ViperConfig) Load() *Config {
return c.Config
}