-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
54 lines (42 loc) · 970 Bytes
/
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
package config
import (
"fmt"
"strings"
"time"
"github.com/spf13/viper"
)
type Config struct {
HTTPClient HTTPClient
HTTPServer HTTPServer
SQLite SQLite
}
type HTTPClient struct {
Timeout time.Duration
}
type HTTPServer struct {
Addr string
ReadTimeout time.Duration
WriteTimeout time.Duration
}
type SQLite struct {
Path string
}
// Load loads the configuration from the config file.
func Load() (Config, error) {
var cfg Config
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
viper.AddConfigPath("../")
viper.AddConfigPath("../../")
viper.SetEnvPrefix("MICROCART")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
return cfg, fmt.Errorf("failed to read config file: %w", err)
}
if err := viper.Unmarshal(&cfg); err != nil {
return cfg, fmt.Errorf("failed to unmarshal config: %w", err)
}
return cfg, nil
}