-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.go
152 lines (141 loc) · 3.47 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package config
import (
"flag"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"log"
"os"
"reflect"
"strings"
)
var Config *MainConfig
var Changed bool
type UsersConfig struct {
TargetId string
Name string
DownloadDir string
NeedDownload bool
TransBiliId string
ExtraConfig map[string]interface{}
}
type ModuleConfig struct {
//EnableProxy bool
//Proxy string
Name string
Enable bool
Users []UsersConfig
DownloadProvider string
ExtraConfig map[string]interface{}
}
type MainConfig struct {
CriticalCheckSec int
NormalCheckSec int
LogFile string
LogFileSize int
LogLevel string
RLogLevel string
DownloadQuality string
DownloadDir string
UploadDir string
Module []ModuleConfig
ExpressPort string
EnableTS2MP4 bool
ExtraConfig map[string]interface{}
}
var v *viper.Viper
func InitConfig() {
log.Print("Init config!")
initConfig()
log.Print("Load config!")
_, _ = ReloadConfig()
//fmt.Println(Config)
}
func initConfig() {
/*viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.AddConfigPath("..")
viper.AddConfigPath("../..")
viper.SetConfigType("json")*/
v = viper.NewWithOptions(viper.KeyDelimiter("::::"))
v.SetConfigFile(viper.ConfigFileUsed())
v.WatchConfig()
err := v.ReadInConfig()
if err != nil {
fmt.Printf("config file error: %s\n", err)
os.Exit(1)
}
Changed = true
v.OnConfigChange(func(in fsnotify.Event) {
Changed = true
})
}
func ReloadConfig() (bool, error) {
if !Changed {
return false, nil
}
Changed = false
err := v.ReadInConfig()
if err != nil {
return true, err
}
config := &MainConfig{}
mdMap := make(map[string]*mapstructure.Metadata, 10)
mdMap[""] = &mapstructure.Metadata{}
err = v.Unmarshal(config, func(c *mapstructure.DecoderConfig) {
c.DecodeHook = mapstructure.ComposeDecodeHookFunc(
func(inType reflect.Type, outType reflect.Type, input interface{}) (interface{}, error) {
if inType.Kind() == reflect.Map && outType.Kind() == reflect.Struct { // we'll decoding a struct
fieldsMap := make(map[string]reflect.StructField, 10)
for i := 0; i < outType.NumField(); i++ {
fieldsMap[strings.ToLower(outType.Field(i).Name)] = outType.Field(i)
}
inputMap := input.(map[string]interface{})
extraConfig := make(map[string]interface{}, 5)
inputMap["ExtraConfig"] = extraConfig
for key := range inputMap {
_, ok := fieldsMap[strings.ToLower(key)]
if !ok {
extraConfig[key] = inputMap[key]
}
}
}
return input, nil
},
c.DecodeHook)
})
if err != nil {
fmt.Printf("Struct config error: %s", err)
}
Config = config
UpdateLogLevel()
return true, nil
}
func LevelStrParse(levelStr string) (level logrus.Level) {
level = logrus.InfoLevel
if levelStr == "debug" {
level = logrus.DebugLevel
} else if levelStr == "info" {
level = logrus.InfoLevel
} else if levelStr == "warn" {
level = logrus.WarnLevel
} else if levelStr == "error" {
level = logrus.ErrorLevel
}
return level
}
func UpdateLogLevel() {
if ConsoleHook != nil {
level := LevelStrParse(Config.LogLevel)
ConsoleHook.LogLevel = level
logrus.Printf("Set logrus console level to %s", level)
}
}
func PrepareConfig() {
confPath := flag.String("config", "config.json", "config.json location")
flag.Parse()
viper.SetConfigFile(*confPath)
InitConfig()
}