add backend tpl
This commit is contained in:
82
backend_v1/conf/config.go
Normal file
82
backend_v1/conf/config.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var C *Config
|
||||
|
||||
// WechatAppID = "wx45745a8c51091ae0"
|
||||
// WechatAppSecret = "2ab33bc79d9b47efa4abef19d66e1977"
|
||||
// WechatToken = "W8Xhw5TivYBgY"
|
||||
// WechatAesKey = "F6AqCxAV4W1eCrY6llJ2zapphKK49CQN3RgtPDrjhnI"
|
||||
type Config struct {
|
||||
Debug bool `mapstructure:"debug"`
|
||||
Port uint `mapstructure:"port"`
|
||||
Database Database `mapstructure:"database"`
|
||||
Wechat Wechat `mapstructure:"wechat"`
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
Database string `mapstructure:"database"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
Host string `mapstructure:"host"`
|
||||
Port uint `mapstructure:"port"`
|
||||
SslMode string `mapstructure:"ssl_mode"`
|
||||
TimeZone string `mapstructure:"time_zone"`
|
||||
}
|
||||
|
||||
func (m Database) DSN() string {
|
||||
tpl := "host=%s port=%d user=%s password=%s dbname=%s sslmode=%s TimeZone=%s"
|
||||
if m.Username == "" {
|
||||
m.Username = "postgres"
|
||||
}
|
||||
|
||||
if m.Port == 0 {
|
||||
m.Port = 5432
|
||||
}
|
||||
|
||||
if m.SslMode == "" {
|
||||
m.SslMode = "disable"
|
||||
}
|
||||
|
||||
if m.TimeZone == "" {
|
||||
m.TimeZone = "Asia/Shanghai"
|
||||
}
|
||||
|
||||
return fmt.Sprintf(tpl, m.Host, m.Port, m.Username, m.Password, m.Database, m.SslMode, m.TimeZone)
|
||||
}
|
||||
|
||||
type Wechat struct {
|
||||
AppID string `mapstructure:"app_id"`
|
||||
AppSecret string `mapstructure:"app_secret"`
|
||||
Token string `mapstructure:"token"`
|
||||
AesKey string `mapstructure:"aes_key"`
|
||||
}
|
||||
|
||||
func Load(file string) error {
|
||||
viper.SetConfigType("yaml")
|
||||
if file != "" {
|
||||
viper.SetConfigFile(file)
|
||||
} else {
|
||||
viper.SetConfigName("config")
|
||||
viper.AddConfigPath(".")
|
||||
}
|
||||
|
||||
log.Infof("load config file")
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return errors.Wrap(err, "read config file")
|
||||
}
|
||||
log.Infof("use config file: %s", viper.ConfigFileUsed())
|
||||
|
||||
if err := viper.Unmarshal(&C); err != nil {
|
||||
return errors.Wrap(err, "unmarshal config")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user