2019-12-10 14:15:27 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"git.trj.tw/golang/utils"
|
|
|
|
"github.com/otakukaze/envconfig"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Google config
|
|
|
|
type Google struct {
|
2019-12-28 14:38:38 +00:00
|
|
|
APIKey string `yaml:"api_key" env:"GOOGLE_API_KEY"`
|
|
|
|
ClientID string `yaml:"client_id" env:"GOOGLE_CLIENT_ID"`
|
|
|
|
AuthURL string `yaml:"auth_url" env:"GOOGLE_AUTH_URL"`
|
|
|
|
TokenURL string `yaml:"token_url" env:"GOOGLE_TOKEN_URL"`
|
|
|
|
ClientSecret string `yaml:"client_secret" env:"GOOGLE_CLIENT_SECRET"`
|
|
|
|
RedirectURL string `yaml:"redirect_url" env:"GOOGLE_REDIRECT_URL"`
|
|
|
|
Scopes []string `yaml:"scopes" env:"GOOGLE_SCOPES"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storage struct
|
|
|
|
type Storage struct {
|
|
|
|
Path string `yaml:"path" env:"STORAGE_FILE_PATH"`
|
2019-12-10 14:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config main struct
|
|
|
|
type Config struct {
|
2019-12-28 14:38:38 +00:00
|
|
|
Port int `yaml:"port" env:"PORT"`
|
|
|
|
Google Google `yaml:"google"`
|
|
|
|
Storage Storage `yaml:"storage"`
|
2019-12-10 14:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var conf *Config
|
|
|
|
|
|
|
|
// Load config from file and env
|
|
|
|
func Load(p ...string) (err error) {
|
|
|
|
fp := ""
|
|
|
|
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)
|
|
|
|
if !utils.CheckExists(fp, false) {
|
|
|
|
return errors.New("config file not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
conf = &Config{}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(b, conf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
envconfig.Parse(conf)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get config struct
|
|
|
|
func Get() *Config { return conf }
|