Quellcode durchsuchen

Migrating configuration files from json to yaml

Granty1 vor 5 Jahren
Ursprung
Commit
0985c1fb6f
4 geänderte Dateien mit 96 neuen und 91 gelöschten Zeilen
  1. 0 43
      server/config.json
  2. 51 0
      server/config.yaml
  3. 40 44
      server/config/config.go
  4. 5 4
      server/core/config.go

+ 0 - 43
server/config.json

@@ -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"
-    }
-}

+ 51 - 0
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'

+ 40 - 44
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"`
 }

+ 5 - 4
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 {