-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
50 lines (41 loc) · 884 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
package main
import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
type Config struct {
DB DBConfig
WX WXConfig
}
type WXConfig struct {
URL string
AppId string
AppSecret string
}
type DBConfig struct {
Path string
}
func GetConfig() (*Config, error) {
viper.SetConfigName("config")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
log.Warn().Err(err).Msg("Failed to read configuration from disk")
return nil,fmt.Errorf("fatal error reading config file: %w", err)
}
wxUrl := viper.GetString("weixin.url")
wxAppId := viper.GetString("weixin.app_id")
wxAppKey := viper.GetString("weixin.app_secret")
dbPath := viper.GetString("db.path")
config := Config{
DB: DBConfig{
Path: dbPath,
},
WX: WXConfig{
URL: wxUrl,
AppId: wxAppId,
AppSecret: wxAppKey,
},
}
return &config, nil
}