-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
51 lines (41 loc) · 1.1 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
package config
import (
"log/slog"
"path"
"runtime"
"github.com/spf13/viper"
)
type Config struct {
Http Http `mapstructure:"http"`
Memphis Memphis `mapstructure:"memphis"`
}
type Http struct {
Port int `mapstructure:"port"`
Mode string `mapstructure:"mode"` // "debug" or "release"
}
type Memphis struct {
Host string `mapstructure:"host"`
UserName string `mapstructure:"user_name"`
Password string `mapstructure:"password"`
AccessMessageStationName string `mapstructure:"access_message_station_name"`
ProducerName string `mapstructure:"producer_name"`
}
func NewConfig() (*Config, error) {
_, b, _, _ := runtime.Caller(0)
configDirPath := path.Join(path.Dir(b))
conf := Config{}
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AddConfigPath(configDirPath)
err := viper.ReadInConfig()
if err != nil {
slog.Error("Read config file.", "err", err)
return nil, err
}
err = viper.Unmarshal(&conf)
if err != nil {
slog.Error("Unmarshal config file.", "err", err)
return nil, err
}
return &conf, nil
}