-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
76 lines (66 loc) · 1.63 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
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
package config
import (
"log"
"os"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
type config struct {
Apps []*AppConfig `mapstructure:"apps"`
}
type AppConfig struct {
ID string `mapstructure:"id"`
Token string `mapstructure:"token"`
DataType string `mapstructure:"data_type"`
EncryptionMethod string `mapstructure:"encryption_method"`
EncodingAESKey string `mapstructure:"encoding_aes_key"`
Callback string `mapstructure:"callback"`
}
var (
Config *config
)
func Init() {
viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")
configFilename := "./config.yaml"
if v := os.Getenv("WECHAT_GATEWAY_CONFIG"); v != "" {
configFilename = v
}
viper.SetConfigFile(configFilename)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Fatalf("Fatal error config file: %s\n", err)
}
viper.WatchConfig()
Config = new(config)
err = viper.Unmarshal(Config)
if err != nil { // Handle errors reading the config file
log.Fatalf("Fatal error config file: %s\n", err)
}
viper.OnConfigChange(func(in fsnotify.Event) {
err = viper.Unmarshal(Config)
if err != nil { // Handle errors reading the config file
log.Printf("Fatal error config file: %s\n", err)
}
})
}
func GetApps() []*AppConfig {
return Config.Apps
}
func GetApp(appID string) *AppConfig {
apps := GetApps()
for _, v := range apps {
if v.ID == appID {
return v
}
}
return nil
}
func ExistAppID(appID string) bool {
apps := GetApps()
for _, v := range apps {
if v.ID == appID {
return true
}
}
return false
}