-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
49 lines (41 loc) · 1.03 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
package config
import (
"strings"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
// NewDefaultConfig create a default configuration structure with
// the same values as the default configuration in config.yaml
func NewDefaultConfig() Config {
return Config{
Debug: false,
Server: Server{
HTTPListen: ":9000",
HTTPSListen: ":9001",
TLS: ServerTLS{},
},
DSN: "sqlite://data.db",
Log: Log{
Level: "warn",
Path: "logs/error.log",
},
}
}
// SetupConfig reads the configuration from the specified file
func SetupConfig(c *Config, file string) error {
// setup config file path
viper.SetConfigFile(file)
// setup env config search
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
// load config
if err := viper.ReadInConfig(); err != nil {
return errors.Errorf("failed to read configuration: %v", err)
}
// unmarshal config
err := viper.Unmarshal(c)
if err != nil {
return errors.Errorf("failed to unmarshal configuration: %s, err: %v", file, err)
}
return nil
}