2020-07-07 10:39:24 +00:00
|
|
|
package intimate
|
2020-07-06 08:33:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
2020-07-10 04:05:33 +00:00
|
|
|
"log"
|
2020-07-06 08:33:35 +00:00
|
|
|
"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-06 08:33:35 +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"`
|
2020-07-06 08:33:35 +00:00
|
|
|
} `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-06 08:33:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 04:05:33 +00:00
|
|
|
if len(configfile) <= 4 {
|
|
|
|
log.Panic(errors.New("can't find config.yaml/config.yml"))
|
|
|
|
}
|
|
|
|
|
2020-07-06 08:33:35 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|