mtfosbot/module/config/config.go

93 lines
1.8 KiB
Go
Raw Permalink Normal View History

2018-08-22 08:57:02 +00:00
package config
import (
"errors"
"io/ioutil"
"os"
"path"
"git.trj.tw/golang/mtfosbot/module/utils"
yaml "gopkg.in/yaml.v2"
)
// Config -
type Config struct {
2018-10-01 16:11:32 +00:00
Port int `yaml:"port"`
URL string `yaml:"url"`
SelfKey string `yaml:"self_key"`
ImageRoot string `yaml:"image_root"`
LogImageRoot string `yaml:"log_image_root"`
Line struct {
2018-08-22 09:20:12 +00:00
Secret string `yaml:"secret"`
Access string `yaml:"access"`
2018-08-22 08:57:02 +00:00
} `yaml:"line"`
2018-08-22 09:20:12 +00:00
Twitch struct {
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
SubSecret string `yaml:"sub_secret"`
ChatHost string `yaml:"chat_host"`
BotOauth string `yaml:"bot_oauth"`
BotUser string `yaml:"bot_user"`
2018-08-22 09:20:12 +00:00
} `yaml:"twitch"`
Google struct {
APIKey string `yaml:"api_key"`
} `yaml:"google"`
Database struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
User string `yaml:"user"`
Pass string `yaml:"pass"`
DB string `yaml:"dbname"`
} `yaml:"database"`
Redis struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
} `yaml:"redis"`
2019-05-28 03:17:22 +00:00
Elasticsearch struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Host string `yaml:"host"`
Index string `yaml:"index"`
2018-10-05 08:36:16 +00:00
} `yaml:"elasticsearch"`
2018-08-22 08:57:02 +00:00
}
var conf *Config
// LoadConfig -
func LoadConfig(p ...string) error {
var fp string
if len(p) > 0 && len(p[0]) > 0 {
fp = p[0]
} else {
wd, err := os.Getwd()
if err != nil {
return err
}
fp = path.Join(wd, "config.yml")
}
fp = utils.ParsePath(fp)
exists := utils.CheckExists(fp, false)
if !exists {
return errors.New("config file not exists")
}
data, err := ioutil.ReadFile(fp)
if err != nil {
return err
}
conf = &Config{}
err = yaml.Unmarshal(data, conf)
if err != nil {
return err
}
return nil
}
2018-08-22 09:20:12 +00:00
// GetConf -
func GetConf() *Config {
return conf
}