intimate/config.go

60 lines
1.1 KiB
Go
Raw Normal View History

2020-07-07 10:39:24 +00:00
package intimate
import (
"errors"
"io/ioutil"
2020-07-10 04:05:33 +00:00
"log"
"os"
"gopkg.in/yaml.v2"
)
2020-07-07 10:39:24 +00:00
// InitConfig 初始化配置加载 config.yaml(yml)
var InitConfig *Config
func init() {
InitConfig = &Config{}
InitConfig.Load()
// storeOpenrec = NewStore()
2020-07-13 10:10:48 +00:00
log.SetFlags(log.Lshortfile | log.Ldate)
2020-07-07 10:39:24 +00:00
}
// Config 配置
type Config struct {
Database struct {
2020-07-10 04:05:33 +00:00
SourceURI string `yaml:"source_uri"` // "user:password@/dbname"
ExtractorURI string `yaml:"extractor_uri"`
} `yaml:"database"`
}
// Load 加载yaml/yml配置
func (conifg *Config) Load() {
2020-07-10 04:05:33 +00:00
var configfile string
2020-07-10 10:39:27 +00:00
configlist := []string{"./config.yaml", "./config.yml", "../../config.yml", "../../config.yaml", "../../../config.yml", "../../../config.yaml"}
2020-07-10 04:05:33 +00:00
for _, configfile = range configlist {
if _, err := os.Stat(configfile); err == nil {
log.Println("find config: ", configfile)
break
}
}
2020-07-10 04:05:33 +00:00
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)
}
}