InitConfig initialize the config with configFile
(configFile string, confType ...interface{})
| 195 | |
| 196 | // InitConfig initialize the config with configFile |
| 197 | func 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) |