42 lines
717 B
Go
42 lines
717 B
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
// Config 配置
|
|
type Config struct {
|
|
Database struct {
|
|
URI string `yaml:"uri"` // "user:password@/dbname"
|
|
} `yaml:"database"`
|
|
}
|
|
|
|
// Load 加载yaml/yml配置
|
|
func (conifg *Config) Load() {
|
|
configfile := "./config.yaml"
|
|
if _, err := os.Stat(configfile); os.IsNotExist(err) {
|
|
configfile = "./config.yml"
|
|
if _, err := os.Stat(configfile); os.IsNotExist(err) {
|
|
panic(errors.New("config.yaml or config.yml is not exists"))
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|