gorm wire
This commit is contained in:
@@ -3,91 +3,73 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var File *Config
|
||||
func New(path string) (*Config, error) {
|
||||
v := viper.New()
|
||||
v.AddConfigPath(filepath.Dir(path))
|
||||
v.SetConfigName(filepath.Base(path))
|
||||
v.SetConfigType(strings.TrimPrefix(filepath.Ext(path), "."))
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
const ConfigDefaultFile = "config.dev.yaml"
|
||||
v.WatchConfig()
|
||||
|
||||
var config Config
|
||||
v.OnConfigChange(func(e fsnotify.Event) {
|
||||
fmt.Println("config file changed:", e.Name)
|
||||
if err := v.Unmarshal(&config); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
})
|
||||
|
||||
if err := v.Unmarshal(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
App App `mapstructure:"app" json:"app" yaml:"app"`
|
||||
DB DB `mapstructure:"db" json:"db" yaml:"db"`
|
||||
Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
|
||||
Cors Cors `mapstructure:"cors" json:"cors" yaml:"cors"`
|
||||
JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
|
||||
AliyunUpload AliyunUpload `mapstructure:"aliyunupload" json:"aliyunupload" yaml:"aliyunupload"`
|
||||
TencentUpload TencentUpload `mapstructure:"tencentupload" json:"tencentupload" yaml:"tencentupload"`
|
||||
Captcha Captcha `mapstructure:"captcha" json:"captcha"`
|
||||
Applet Applet `mapstructure:"applet" json:"applet" yaml:"applet"`
|
||||
Smb Smb `mapstructure:"smb" json:"smb" yaml:"smb"`
|
||||
}
|
||||
|
||||
func New(path string) (*Config, error) {
|
||||
fp := "."
|
||||
fn := ConfigDefaultFile
|
||||
if len(path) > 0 {
|
||||
fp, fn = filepath.Split(path)
|
||||
if len(fp) == 0 {
|
||||
fp = "."
|
||||
}
|
||||
}
|
||||
|
||||
v := viper.New()
|
||||
v.AddConfigPath(fp)
|
||||
v.SetConfigName(fn)
|
||||
v.SetConfigType("yaml")
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v.WatchConfig()
|
||||
|
||||
var conf *Config
|
||||
v.OnConfigChange(func(e fsnotify.Event) {
|
||||
fmt.Println("config file changed:", e.Name)
|
||||
if err := v.Unmarshal(&conf); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
})
|
||||
|
||||
if err := v.Unmarshal(&conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
func Init(path string) error {
|
||||
fp := "."
|
||||
fn := ConfigDefaultFile
|
||||
if len(path) > 0 {
|
||||
fp, fn = filepath.Split(path)
|
||||
if len(fp) == 0 {
|
||||
fp = "."
|
||||
}
|
||||
}
|
||||
|
||||
v := viper.New()
|
||||
v.AddConfigPath(fp)
|
||||
v.SetConfigName(fn)
|
||||
v.SetConfigType("yaml")
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
v.WatchConfig()
|
||||
|
||||
v.OnConfigChange(func(e fsnotify.Event) {
|
||||
fmt.Println("config file changed:", e.Name)
|
||||
if err := v.Unmarshal(&File); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
})
|
||||
|
||||
if err := v.Unmarshal(&File); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
App struct {
|
||||
Host string `mapstructure:"host"` // 服务地址
|
||||
Port int `mapstructure:"port"` // 服务端口
|
||||
Prod bool `mapstructure:"prod"` // 是否正式
|
||||
} `mapstructure:"app"`
|
||||
DB struct {
|
||||
Driver string `mapstructure:"driver"` // 数据库类型
|
||||
Host string `mapstructure:"host"` // 数据库地址
|
||||
Port int `mapstructure:"port"` // 数据库端口
|
||||
Username string `mapstructure:"username"` // 数据库用户
|
||||
Password string `mapstructure:"password"` // 数据库密码
|
||||
DBName string `mapstructure:"db_name"` // 数据库名称
|
||||
MaxOpenConns int `mapstructure:"max_open_conns"` // 数据库名称
|
||||
MaxIdleConns int `mapstructure:"max_idle_conns"` // 数据库名称
|
||||
} `mapstructure:"db"`
|
||||
Redis struct {
|
||||
Host string `mapstructure:"host"` // redis地址
|
||||
Port int `mapstructure:"port"` // redis端口
|
||||
Password string `mapstructure:"password"` // redis密码
|
||||
DB int `mapstructure:"db"` // redis数据库
|
||||
} `mapstructure:"redis"`
|
||||
Cors struct {
|
||||
Host string `mapstructure:"host"`
|
||||
} `mapstructure:"cors"`
|
||||
JWT struct {
|
||||
SigningKey string `mapstructure:"signing_key"` // jwt签名
|
||||
ExpiresTime time.Duration `mapstructure:"expires_time"` // 过期时间
|
||||
RefreshTime time.Duration `mapstructure:"refresh_time"` // 刷新过期时间
|
||||
Issuer string `mapstructure:"issuer"` // 签发者
|
||||
} `mapstructure:"jwt"`
|
||||
Captcha struct {
|
||||
OpenCaptcha int `mapstructure:"open_captcha"` // 是否开启防爆次数
|
||||
ImgWidth int `mapstructure:"img_width"` // 验证码图片宽度
|
||||
ImgHeight int `mapstructure:"img_height"` // 验证码图片高度
|
||||
KeyLong int `mapstructure:"key_long"` // 验证码长度
|
||||
} `mapstructure:"captcha"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user