75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
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 {
|
|
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"`
|
|
}
|
|
|
|
// Config main struct
|
|
type Config struct {
|
|
Port int `yaml:"port" env:"PORT"`
|
|
Google Google `yaml:"google"`
|
|
Storage Storage `yaml:"storage"`
|
|
}
|
|
|
|
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 }
|