gorm wire
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
package config
|
||||
|
||||
type App struct {
|
||||
Host string `mapstructure:"host" json:"host" yaml:"host"` // 服务地址
|
||||
Port int `mapstructure:"port" json:"port" yaml:"port"` // 服务端口
|
||||
Prod bool `mapstructure:"prod" json:"prod" yaml:"prod"` // 是否正式
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package config
|
||||
|
||||
type Applet struct {
|
||||
AppID string `mapstructure:"app_id" json:"app_id" yaml:"app_id"` // appid
|
||||
AppSecret string `mapstructure:"app_secret" json:"app_secret" yaml:"app_secret"` // secret
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package config
|
||||
|
||||
type Captcha struct {
|
||||
OpenCaptcha int `mapstructure:"open_captcha" json:"open_captcha" yaml:"open_captcha"` // 是否开启防爆次数
|
||||
OpenCaptchaTimeout string `mapstructure:"open_captcha_timeout" json:"open_captcha_timeout" yaml:"open_captcha_timeout"` // 缓存超时时间
|
||||
ImgWidth int `mapstructure:"img_width" json:"img_width" yaml:"img_width"` // 验证码图片宽度
|
||||
ImgHeight int `mapstructure:"img_height" json:"img_height" yaml:"img_height"` // 验证码图片高度
|
||||
KeyLong int `mapstructure:"key_long" json:"key_long" yaml:"key_long"` // 验证码长度
|
||||
}
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package config
|
||||
|
||||
type Cors struct {
|
||||
Host string `mapstructure:"host" json:"host" yaml:"host"`
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package config
|
||||
|
||||
type DB struct {
|
||||
Driver string `mapstructure:"driver" json:"driver" yaml:"driver"` // 数据库类型
|
||||
Host string `mapstructure:"host" json:"host" yaml:"host"` // 数据库地址
|
||||
Port int `mapstructure:"port" json:"port" yaml:"port"` // 数据库端口
|
||||
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户
|
||||
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
|
||||
DBName string `mapstructure:"db_name" json:"db_name" yaml:"db_name"` // 数据库名称
|
||||
MaxOpenConns int `mapstructure:"max_open_conns" json:"max_open_conns" yaml:"max_open_conns"` // 数据库名称
|
||||
MaxIdleConns int `mapstructure:"max_idle_conns" json:"max_idle_conns" yaml:"max_idle_conns"` // 数据库名称
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type JWT struct {
|
||||
SigningKey string `mapstructure:"signing_key" json:"signing_key" yaml:"signing_key"` // jwt签名
|
||||
ExpiresTime time.Duration `mapstructure:"expires_time" json:"expires_time" yaml:"expires_time"` // 过期时间
|
||||
RefreshTime time.Duration `mapstructure:"refresh_time" json:"refresh_time" yaml:"refresh_time"` // 刷新过期时间
|
||||
Issuer string `mapstructure:"issuer" json:"issuer" yaml:"issuer"` // 签发者
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package config
|
||||
|
||||
type Redis struct {
|
||||
Host string `mapstructure:"host" json:"host" yaml:"host"` // redis地址
|
||||
Port int `mapstructure:"port" json:"port" yaml:"port"` // redis端口
|
||||
Password string `mapstructure:"password" json:"password" yaml:"password"` // redis密码
|
||||
DB int `mapstructure:"db" json:"db_name" yaml:"db"` // redis数据库
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package config
|
||||
|
||||
type Smb struct {
|
||||
Host string `mapstructure:"host" json:"host" yaml:"host"`
|
||||
Name string `mapstructure:"name" json:"name" yaml:"name"`
|
||||
Pass string `mapstructure:"pass" json:"pass" yaml:"pass"`
|
||||
Mount string `mapstructure:"mount" json:"mount" yaml:"mount"`
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package config
|
||||
|
||||
type AliyunUpload struct {
|
||||
Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint"`
|
||||
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"`
|
||||
AccessKeyID string `mapstructure:"access_key_id" json:"access_key_id" yaml:"access_key_id"`
|
||||
AccessKeySecret string `mapstructure:"access_key_secret" json:"access_key_secret" yaml:"access_key_secret"`
|
||||
}
|
||||
|
||||
type TencentUpload struct {
|
||||
Region string `mapstructure:"region" json:"region" yaml:"region"`
|
||||
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"`
|
||||
AccessKeyID string `mapstructure:"access_key_id" json:"access_key_id" yaml:"access_key_id"`
|
||||
AccessKeySecret string `mapstructure:"access_key_secret" json:"access_key_secret" yaml:"access_key_secret"`
|
||||
AllowImageMaxSize int64 `mapstructure:"allow_image_max_size" json:"allow_image_max_size" yaml:"allow_image_max_size"`
|
||||
AllowImageExtension string `mapstructure:"allow_image_extension" json:"allow_image_extension" yaml:"allow_image_extension"`
|
||||
AllowFileMaxSize int64 `mapstructure:"allow_file_max_size" json:"allow_file_max_size" yaml:"allow_file_max_size"`
|
||||
}
|
||||
Reference in New Issue
Block a user