-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_config_handler.go
186 lines (154 loc) · 4.52 KB
/
project_config_handler.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package setting
import (
"fmt"
"mocknet/logger"
"mocknet/utils"
)
type Setting map[string]interface{}
type projectConfig struct {
ProxyHost string `json:"proxy_host"` //请求代理的host
ProxyScheme string `json:"proxy_scheme"` //请求代理的host
Address string `json:"address"` //服务端启动的address
MockApiPath []string `json:"mock_api_path"` //加载 mock api 信息的地址
StaticFilePath string `json:"file_path"` //视频文件地址
FileWatcher bool `json:"file_watcher"` //是否开启文件更新刷新server
FileWatcherConfig map[string]string `json:"file_watcher_config"` //文件变化通知配置信息
}
func loadProjectConfig() {
config := projectConfig{}
err := utils.LoadFileJson("config.json", &config)
if err != nil {
panic(logger.FormatPanicString(err, fmt.Sprintf("config.json parse is fail")))
}
setProxyHost(config.ProxyHost)
setProxySchema(config.ProxyScheme)
setStartAddress(config.Address)
setLocalApiInfoPath(config.MockApiPath)
setStaticFilePath(config.StaticFilePath)
setFileWatcherOpen(config.FileWatcher)
validExt, ok := config.FileWatcherConfig["valid_ext"]
if ok {
setFileWatcherValidExt(validExt)
}
noReloadExt, ok := config.FileWatcherConfig["no_reload_ext"]
if ok {
setFileWatcherNoReloadExt(noReloadExt)
}
ignoredFolder, ok := config.FileWatcherConfig["ignored_folder"]
if ok {
setFileWatcherIgnoredFolder(ignoredFolder)
}
}
func (setting Setting) setKVOrDefault(k string, v interface{}) {
switch v.(type) {
case string:
value, ok := v.(string)
if ok && len(value) != 0 {
setting[k] = value
}
case []string:
value, ok := v.([]string)
if ok && len(value) != 0 {
setting[k] = value
}
default:
setting[k] = v
}
}
func (setting Setting) getString(k string, defaultValue string) string {
v := setting[k]
str, ok := v.(string)
if ok {
return str
} else {
return defaultValue
}
}
// 项目设置的模版
var projectSetting Setting = map[string]interface{}{
"proxy_host": "",
"proxy_schema": "https",
"address": ":8080",
"local_api_info_path": []string{"."},
"static_path": ".",
"file_watcher_open": false,
"file_watcher_valid_ext": ".json",
"file_watcher_no_reload_ext": ".tpl, .tmpl, .html",
"file_watcher_ignored_folder": "",
}
func GetProxyHost() string {
return projectSetting.getString("proxy_host", "")
}
func setProxyHost(v string) {
projectSetting.setKVOrDefault("proxy_host", v)
}
func GetProxySchema() string {
return projectSetting.getString("proxy_schema", "https")
}
func setProxySchema(v string) {
projectSetting.setKVOrDefault("proxy_schema", v)
}
func GetStartAddress() string {
return projectSetting.getString("address", ":8080")
}
func setStartAddress(v string) {
projectSetting.setKVOrDefault("address", v)
}
func GetLocalApiInfoPath() []string {
v := projectSetting["local_api_info_path"]
localApiInfoPath, ok := v.([]string)
if ok {
return localApiInfoPath
} else {
return []string{"."}
}
}
func setLocalApiInfoPath(v []string) {
projectSetting.setKVOrDefault("local_api_info_path", v)
}
func GetStaticFilePath() string {
return projectSetting.getString("static_path", ".")
}
func setStaticFilePath(v string) {
projectSetting.setKVOrDefault("static_path", v)
}
func setFileWatcherOpen(isOpen bool) {
projectSetting["file_watcher_open"] = isOpen
}
func IsFileWatcherOpen() bool {
info := projectSetting["file_watcher_open"]
isOpen, ok := info.(bool)
if ok {
return isOpen
} else {
return false
}
}
func setFileWatcherValidExt(v string) {
projectSetting.setKVOrDefault("file_watcher_valid_ext", v)
}
func GetFileWatcherValidExt() string {
return projectSetting.getString("file_watcher_valid_ext", ".json")
}
func setFileWatcherNoReloadExt(v string) {
projectSetting.setKVOrDefault("file_watcher_no_reload_ext", v)
}
func GetFileWatcherNoReloadExt() string {
return projectSetting.getString("file_watcher_no_reload_ext", ".tpl, .tmpl, .html")
}
func setFileWatcherIgnoredFolder(v string) {
projectSetting.setKVOrDefault("file_watcher_ignored_folder", v)
}
func GetFileWatcherIgnoredFolder() string {
return projectSetting.getString("file_watcher_ignored_folder", "")
}
func CheckProxyInfo() bool {
return len(GetProxyHost()) != 0 &&
len(GetProxySchema()) != 0
}
func LoadProjectConfig() {
loadProjectConfig()
}
func LoadApiInfo() {
loadApiInfo(GetLocalApiInfoPath())
}