This commit is contained in:
henry
2021-11-02 09:43:19 +08:00
parent 570bb3c772
commit 4734344985
78 changed files with 4798 additions and 0 deletions

42
lib/config.go Normal file
View File

@ -0,0 +1,42 @@
package lib
import (
"encoding/json"
"io/ioutil"
"path"
"gopkg.in/yaml.v2"
)
type callback func(interface{})
var loaders = map[string]func([]byte, interface{}) error{
".json": LoadConfigFormJsonBytes,
".yaml": LoadConfigFromYamlBytes,
}
func LoadConfigFormJsonBytes(content []byte, obj interface{}) error {
return json.Unmarshal(content, obj)
}
func LoadConfigFromYamlBytes(content []byte, obj interface{}) error {
return yaml.Unmarshal(content, obj)
}
func LoadConfig(file string, v interface{}, callback ...callback) {
content, err := ioutil.ReadFile(file)
if err != nil {
panic("Load Config Error " + err.Error())
}
loader, ok := loaders[path.Ext(file)]
if !ok {
panic("Unknown File Type" + path.Ext(file))
}
if err = loader(content, v); err == nil {
for _, _callback := range callback {
_callback(v)
}
}
}