intimate/config.go
2020-07-31 18:04:10 +08:00

60 lines
1.1 KiB
Go

package intimate
import (
"errors"
"io/ioutil"
"log"
"os"
"gopkg.in/yaml.v2"
)
// InitConfig 初始化配置加载 config.yaml(yml)
var InitConfig *Config
func init() {
InitConfig = &Config{}
InitConfig.Load()
// storeOpenrec = NewStore()
log.SetFlags(log.Llongfile | log.Ltime)
}
// Config 配置
type Config struct {
Database struct {
SourceURI string `yaml:"source_uri"` // "user:password@/dbname"
ExtractorURI string `yaml:"extractor_uri"`
} `yaml:"database"`
}
// Load 加载yaml/yml配置
func (conifg *Config) Load() {
var configfile string
configlist := []string{"./config.yaml", "./config.yml", "../../config.yml", "../../config.yaml", "../../../config.yml", "../../../config.yaml"}
for _, configfile = range configlist {
if _, err := os.Stat(configfile); err == nil {
log.Println("find config: ", configfile)
break
}
}
if len(configfile) <= 4 {
log.Panic(errors.New("can't find config.yaml/config.yml"))
}
f, err := os.Open(configfile)
if err != nil {
panic(err)
}
data, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(data, conifg)
if err != nil {
panic(err)
}
}