From 0985c1fb6ffef0a145ef81fe39ef47449b9c5a6e Mon Sep 17 00:00:00 2001 From: Granty1 Date: Sun, 5 Apr 2020 20:35:52 +0800 Subject: [PATCH] Migrating configuration files from json to yaml --- server/config.json | 43 --------------------- server/config.yaml | 51 +++++++++++++++++++++++++ server/config/config.go | 84 ++++++++++++++++++++--------------------- server/core/config.go | 9 +++-- 4 files changed, 96 insertions(+), 91 deletions(-) delete mode 100644 server/config.json create mode 100644 server/config.yaml diff --git a/server/config.json b/server/config.json deleted file mode 100644 index 23e8a178..00000000 --- a/server/config.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "casbin": { - "modelPath": "./resource/rbac_model.conf" - }, - "jwt": { - "signingKey": "qmPlus" - }, - "mysql": { - "username": "root", - "password": "Aa@6447985", - "path": "127.0.0.1:3306", - "dbname": "qmPlus", - "config": "charset=utf8\u0026parseTime=True\u0026loc=Local", - "maxIdleConns": 10, - "maxOpenConns": 100, - "logMode": true - }, - "qiniu": { - "accessKey": "25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ", - "secretKey": "pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY" - }, - "redis": { - "addr": "127.0.0.1:6379", - "password": "", - "db": 0 - }, - "system": { - "useMultipoint": false, - "env": "develop", - "addr": 8888 - }, - "captcha": { - "keyLong": 6, - "imgWidth": 120, - "imgHeight": 40 - }, - "log": { - "prefix": "[GIN-VUE-ADMIN]", - "logFile": false, - "stdout": "DEBUG", - "file": "WARNING" - } -} \ No newline at end of file diff --git a/server/config.yaml b/server/config.yaml new file mode 100644 index 00000000..085982e1 --- /dev/null +++ b/server/config.yaml @@ -0,0 +1,51 @@ +# Gin-Vue-Admin Global Configuration + +# casbin configuration +casbin: + model-path: './resource/rbac_model.conf' + +# jwt configuration +jwt: + signing-key: 'qmPlus' + +# mysql connect configuration +mysql: + username: root + #Aa@6447985 + password: 'YINGL19980716.' + path: '127.0.0.1:3306' + db-name: 'qmPlus' + config: 'charset=utf8&parseTime=True&loc=Local' + max-idle-conns: 10 + max-open-conns: 10 + log-mode: true + +# oss configuration +qiniu: + access-key: '25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ' + secret-key: 'pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY' + +# redis configuration +redis: + addr: '127.0.0.1:6379' + passwprd: '' + db: 0 + +# system configuration +system: + use-multipoint: false + env: 'develop' + addr: 8888 + +# captcha configuration +captcha: + key-long: 6 + img-width: 120 + img-height: 40 + +# logger configuration +log: + prefix: '[GIN-VUE-ADMIN]' + log-file: true + stdout: 'DEBUG' + file: 'WARNING' diff --git a/server/config/config.go b/server/config/config.go index 17ecb2e7..2137cff1 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -1,64 +1,60 @@ package config type Server struct { - Mysql Mysql `json:"mysql"` - Qiniu Qiniu `json:"qiniu"` - Casbin Casbin `json:"casbin"` - Redis Redis `json:"redis"` - System System `json:"system"` - JWT JWT `json:"jwt"` - Captcha Captcha `json:"captcha"` - Log Log `json:"log"` + Mysql `mapstructure:"mysql"` + Qiniu `mapstructure:"qiniu"` + Casbin `mapstructure:"casbin"` + Redis `mapstructure:"redis"` + System `mapstructure:"system"` + JWT `mapstructure:"jwt"` + Captcha `mapstructure:"captcha"` + Log `mapstructure:"log"` } -type System struct { // 系统配置 - UseMultipoint bool `json:"useMultipoint"` - Env string `json:"env"` - Addr int `json:"addr"` +type System struct { + UseMultipoint bool `mapstructure:"use-multipoint"` + Env string `mapstructure:"env"` + Addr int `mapstructure:"addr"` } -type JWT struct { // jwt签名 - SigningKey string `json:"signingKey"` +type JWT struct { + SigningKey string `mapstructure:"signing-key"` } -type Casbin struct { //casbin配置 - ModelPath string `json:"modelPath"` // casbin model地址配置 +type Casbin struct { + ModelPath string `mapstructure:"model-path"` } -type Mysql struct { // mysql admin 数据库配置 - Username string `json:"username"` - Password string `json:"password"` - Path string `json:"path"` - Dbname string `json:"dbname"` - Config string `json:"config"` - MaxIdleConns int `json:"maxIdleConns"` - MaxOpenConns int `json:"maxOpenConns"` - LogMode bool `json:"maxOpenConns"` +type Mysql struct { + Username string `mapstructure:"username"` + Password string `mapstructure:"password"` + Path string `mapstructure:"path"` + Dbname string `mapstructure:"db-name"` + Config string `mapstructure:"config"` + MaxIdleConns int `mapstructure:"max-idle-conns"` + MaxOpenConns int `mapstructure:"max-open-conns"` + LogMode bool `mapstructure:"log-mode"` } -type Redis struct { // Redis admin 数据库配置 - Addr string `json:"addr"` - Password string `json:"password"` - DB int `json:"db"` +type Redis struct { + Addr string `mapstructure:"addr"` + Password string `mapstructure:"password"` + DB int `mapstructure:"db"` } -type Qiniu struct { // 七牛 密钥配置 - AccessKey string `json:"accessKey"` - SecretKey string `json:"secretKey"` +type Qiniu struct { + AccessKey string `mapstructure:"access-key"` + SecretKey string `mapstructure:"secret-key"` } -type Captcha struct { // 验证码配置 - KeyLong int `json:"keyLong"` - ImgWidth int `json:"imgWidth"` - ImgHeight int `json:"imgHeight"` +type Captcha struct { + KeyLong int `mapstructure:"key-long"` + ImgWidth int `mapstructure:"img-width"` + ImgHeight int `mapstructure:"img-height"` } type Log struct { - // log 打印的前缀 - Prefix string `json:"prefix"` - // 是否显示打印log的文件具体路径 - LogFile bool `json:"logFile"` - // 在控制台打印log的级别, "" 默认不打印 - Stdout string `json:"stdout"` - // 在文件中打印log的级别 "" 默认不打印 - File string `json:"file"` + Prefix string `mapstructure:"prefix"` + LogFile bool `mapstructure:"log-file"` + Stdout string `mapstructure:"stdout"` + File string `mapstructure:"file"` } diff --git a/server/core/config.go b/server/core/config.go index a79dd9f1..0ff0108b 100644 --- a/server/core/config.go +++ b/server/core/config.go @@ -7,16 +7,17 @@ import ( "github.com/spf13/viper" ) +const defaultConfigFile = "config.yaml" + func init() { v := viper.New() - v.SetConfigName("config") // 设置配置文件名 (不带后缀) - v.AddConfigPath("./") // 第一个搜索路径 - v.SetConfigType("json") - err := v.ReadInConfig() // 搜索路径,并读取配置数据 + v.SetConfigFile(defaultConfigFile) + err := v.ReadInConfig() if err != nil { panic(fmt.Errorf("Fatal error config file: %s \n", err)) } v.WatchConfig() + v.OnConfigChange(func(e fsnotify.Event) { fmt.Println("config file changed:", e.Name) if err := v.Unmarshal(&global.GVA_CONFIG); err != nil { -- GitLab