MCPcopy Create free account
hub / github.com/devfeel/dotweb / InitConfig

Function InitConfig

config/configs.go:197–261  ·  view source on GitHub ↗

InitConfig initialize the config with configFile

(configFile string, confType ...interface{})

Source from the content-addressed store, hash-verified

195
196// InitConfig initialize the config with configFile
197func InitConfig(configFile string, confType ...interface{}) (config *Config, err error) {
198
199 // Validity check
200 // 1. Try read as absolute path
201 // 2. Try the current working directory
202 // 3. Try $PWD/config
203 // fixed for issue #15 config file path
204 realFile := configFile
205 if !file.Exist(realFile) {
206 realFile = file.GetCurrentDirectory() + "/" + configFile
207 if !file.Exist(realFile) {
208 realFile = file.GetCurrentDirectory() + "/config/" + configFile
209 if !file.Exist(realFile) {
210 return nil, errors.New("no exists config file => " + configFile)
211 }
212 }
213 }
214
215 cType := ConfigType_XML
216 if len(confType) > 0 && confType[0] == ConfigType_JSON {
217 cType = ConfigType_JSON
218 }
219 if len(confType) > 0 && confType[0] == ConfigType_Yaml {
220 cType = ConfigType_Yaml
221 }
222
223 if cType == ConfigType_XML {
224 config, err = initConfig(realFile, cType, UnmarshalXML)
225 } else if cType == ConfigType_Yaml {
226 config, err = initConfig(realFile, cType, UnmarshalYaml)
227 } else {
228 config, err = initConfig(realFile, cType, UnmarshalJSON)
229 }
230
231 if err != nil {
232 return config, err
233 }
234
235 if config.App == nil {
236 config.App = NewAppNode()
237 }
238
239 if config.Server == nil {
240 config.Server = NewServerNode()
241 }
242
243 if config.Session == nil {
244 config.Session = NewSessionNode()
245 }
246
247 tmpConfigSetMap := core.NewConcurrenceMap()
248 for _, v := range config.ConfigSetNodes {
249 tmpConfigSetMap.Set(v.Key, v.Value)
250 }
251 config.ConfigSet = tmpConfigSetMap
252
253 // deal config default value
254 dealConfigDefaultSet(config)

Callers 6

visitMethod · 0.92
newConfigDotWebFunction · 0.92
mainFunction · 0.92
TestInitConfigFunction · 0.85
TestInitConfigWithXmlFunction · 0.85
MustInitConfigFunction · 0.85

Calls 9

SetMethod · 0.95
ExistFunction · 0.92
GetCurrentDirectoryFunction · 0.92
NewConcurrenceMapFunction · 0.92
initConfigFunction · 0.85
NewAppNodeFunction · 0.85
NewServerNodeFunction · 0.85
NewSessionNodeFunction · 0.85
dealConfigDefaultSetFunction · 0.85

Tested by 3

newConfigDotWebFunction · 0.74
TestInitConfigFunction · 0.68
TestInitConfigWithXmlFunction · 0.68