Explorar el Código

增加了 配置管理功能

pixel hace 5 años
padre
commit
30d321fbfd

+ 32 - 0
QMPlusServer/cmd/linux.go

@@ -0,0 +1,32 @@
+package cmd
+
+import (
+	"fmt"
+	"gin-vue-admin/config"
+	"github.com/fvbock/endless"
+	"github.com/gin-gonic/gin"
+	"syscall"
+	"time"
+)
+
+func RunLinuxServer(Router *gin.Engine) {
+	endless.DefaultReadTimeOut = 10 * time.Second
+	endless.DefaultWriteTimeOut = 10 * time.Second
+	endless.DefaultMaxHeaderBytes = 1 << 20
+	endPoint := fmt.Sprintf(":%d", config.GinVueAdminconfig.System.Addr)
+
+	server := endless.NewServer(endPoint, Router)
+	server.BeforeBegin = func(add string) {
+		fmt.Printf(`欢迎使用 Gin-Vue-Admin
+	作者:奇淼 And Spike666
+	微信:shouzi_1994
+	默认自动化文档地址:http://127.0.0.1%s/swagger/index.html
+	默认前端文件运行地址:http://127.0.0.1:8080
+	Actual pid is %d
+`, fmt.Sprintf(":%d", config.GinVueAdminconfig.System.Addr), syscall.Getpid())
+	}
+	err := server.ListenAndServe()
+	if err != nil {
+		fmt.Printf("Server err: %v", err)
+	}
+}

+ 27 - 0
QMPlusServer/cmd/windows.go

@@ -0,0 +1,27 @@
+package cmd
+
+import (
+	"fmt"
+	"gin-vue-admin/config"
+	"github.com/gin-gonic/gin"
+	"net/http"
+	"time"
+)
+
+func RunWindowsServer(Router *gin.Engine) {
+	s := &http.Server{
+		Addr:           fmt.Sprintf(":%d", config.GinVueAdminconfig.System.Addr),
+		Handler:        Router,
+		ReadTimeout:    10 * time.Second,
+		WriteTimeout:   10 * time.Second,
+		MaxHeaderBytes: 1 << 20,
+	}
+	time.Sleep(10 * time.Microsecond)
+	fmt.Printf(`欢迎使用 Gin-Vue-Admin
+	作者:奇淼 And Spike666
+	微信:shouzi_1994
+	默认自动化文档地址:http://127.0.0.1%s/swagger/index.html
+	默认前端文件运行地址:http://127.0.0.1:8080
+`, s.Addr)
+	_ = s.ListenAndServe()
+}

+ 23 - 22
QMPlusServer/config/config.go

@@ -7,43 +7,44 @@ import (
 )
 
 type Config struct {
-	MysqlAdmin   MysqlAdmin `json:"mysqlAdmin"`
-	Qiniu        Qiniu      `json:"qiniu"`
-	CasbinConfig CasbinConfig  `json:"casbinConfig"`
-	RedisAdmin   RedisAdmin  `json:"redisAdmin"`
-	System       System  `json:"system"`
-	JWT          JWT  `json:"jwt"`
+	MysqlAdmin   MysqlAdmin   `json:"mysqlAdmin"`
+	Qiniu        Qiniu        `json:"qiniu"`
+	CasbinConfig CasbinConfig `json:"casbinConfig"`
+	RedisAdmin   RedisAdmin   `json:"redisAdmin"`
+	System       System       `json:"system"`
+	JWT          JWT          `json:"jwt"`
 }
 
-type System struct {    // 系统配置
+type System struct { // 系统配置
 	UseMultipoint bool   `json:"useMultipoint"`
-	Env           string  `json:"env"`
+	Env           string `json:"env"`
+	Addr          int    `json:"addr"`
 }
 
-type JWT struct {   // jwt签名
-	SigningKey string  `json:"signingKey"`
+type JWT struct { // jwt签名
+	SigningKey string `json:"signingKey"`
 }
 
-type CasbinConfig struct {  //casbin配置
-	ModelPath string  `json:"modelPath"` // casbin model地址配置
+type CasbinConfig struct { //casbin配置
+	ModelPath string `json:"modelPath"` // casbin model地址配置
 }
 
 type MysqlAdmin struct { // mysql admin 数据库配置
-	Username string    `json:"username"`
-	Password string    `json:"password"`
-	Path     string    `json:"path"`
-	Dbname   string    `json:"dbname"`
-	Config   string    `json:"config"`
+	Username string `json:"username"`
+	Password string `json:"password"`
+	Path     string `json:"path"`
+	Dbname   string `json:"dbname"`
+	Config   string `json:"config"`
 }
 
 type RedisAdmin struct { // Redis admin 数据库配置
-	Addr     string     `json:"addr"`
-	Password string     `json:"password"`
-	DB       int        `json:"db"`
+	Addr     string `json:"addr"`
+	Password string `json:"password"`
+	DB       int    `json:"db"`
 }
 type Qiniu struct { // 七牛 密钥配置
-	AccessKey string    `json:"accessKey"`
-	SecretKey string    `json:"secretKey"`
+	AccessKey string `json:"accessKey"`
+	SecretKey string `json:"secretKey"`
 }
 
 var GinVueAdminconfig Config

+ 18 - 0
QMPlusServer/controller/api/sys_system.go

@@ -39,3 +39,21 @@ func SetSystemConfig(c *gin.Context) {
 		servers.ReportFormat(c, true, "设置成功", gin.H{})
 	}
 }
+
+// @Tags system
+// @Summary 设置配置文件内容
+// @Security ApiKeyAuth
+// @Produce  application/json
+// @Param data body sysModel.System true
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
+// @Router /system/ReloadSystem [post]
+func ReloadSystem(c *gin.Context) {
+	var sys sysModel.System
+	_ = c.ShouldBind(&sys)
+	err := sys.SetSystemConfig()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("设置失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "设置成功", gin.H{})
+	}
+}

+ 1 - 0
QMPlusServer/go.mod

@@ -9,6 +9,7 @@ require (
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
 	github.com/fsnotify/fsnotify v1.4.7
+	github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6
 	github.com/gin-gonic/gin v1.4.0
 	github.com/go-redis/redis v6.15.6+incompatible
 	github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect

+ 13 - 19
QMPlusServer/main.go

@@ -1,15 +1,14 @@
 package main
 
 import (
-	"fmt"
+	"gin-vue-admin/cmd"
 	"gin-vue-admin/config"
 	"gin-vue-admin/init/initRedis"
 	"gin-vue-admin/init/initRouter"
 	"gin-vue-admin/init/qmlog"
 	"gin-vue-admin/init/qmsql"
 	"gin-vue-admin/init/registTable"
-	"net/http"
-	"time"
+	"runtime"
 )
 
 // @title Swagger Example API
@@ -21,29 +20,24 @@ import (
 // @BasePath /
 
 func main() {
+
 	qmlog.InitLog()                                            // 初始化日志
 	db := qmsql.InitMysql(config.GinVueAdminconfig.MysqlAdmin) // 链接初始化数据库
 	if config.GinVueAdminconfig.System.UseMultipoint {
 		_ = initRedis.InitRedis() // 初始化redis服务
 	}
-	registTable.RegistTable(db)       //注册数据库表
+	registTable.RegistTable(db)       // 注册数据库表
 	defer qmsql.DEFAULTDB.Close()     // 程序结束前关闭数据库链接
-	Router := initRouter.InitRouter() //注册路由
+	Router := initRouter.InitRouter() // 注册路由
 	qmlog.QMLog.Info("服务器开启")         // 日志测试代码
 	//Router.RunTLS(":443","ssl.pem", "ssl.key")  // https支持 需要添加中间件
-	s := &http.Server{
-		Addr:           ":8888",
-		Handler:        Router,
-		ReadTimeout:    10 * time.Second,
-		WriteTimeout:   10 * time.Second,
-		MaxHeaderBytes: 1 << 20,
+	sysType := runtime.GOOS
+	if sysType == "linux" {
+		// LINUX系统
+		cmd.RunLinuxServer(Router)
+	}
+	if sysType == "windows" {
+		// WIN系统
+		cmd.RunWindowsServer(Router)
 	}
-	time.Sleep(10 * time.Microsecond)
-	fmt.Printf(`欢迎使用 Gin-Vue-Admin
-作者:奇淼 And Spike666
-微信:shouzi_1994
-默认自动化文档地址:http://127.0.0.1%s/swagger/index.html
-默认前端文件运行地址:http://127.0.0.1:8080
-`, s.Addr)
-	_ = s.ListenAndServe()
 }

+ 2 - 1
QMPlusServer/static/config/config.json

@@ -23,6 +23,7 @@
   },
   "system": {
     "useMultipoint": false,
-    "env": "develop"
+    "env": "develop",
+    "addr": 8888
   }
 }

+ 7 - 4
QMPlusVuePage/src/view/superAdmin/system/system.vue

@@ -8,6 +8,9 @@
       <el-form-item label="环境值">
         <el-input v-model="config.system.env"></el-input>
       </el-form-item>
+      <el-form-item label="端口值">
+        <el-input v-model="config.system.addr"></el-input>
+      </el-form-item>
       <h2>jwt签名</h2>
       <el-form-item label="jwt签名">
         <el-input v-model="config.jwt.signingKey"></el-input>
@@ -74,7 +77,7 @@ export default {
     }
   },
   async created() {
-      await this.initForm()
+    await this.initForm()
   },
   methods: {
     async initForm() {
@@ -88,10 +91,10 @@ export default {
       const res = await setSystemConfig({ config: this.config })
       if (res.success) {
         this.$message({
-          type:"success",
-          message:"配置文件设置成功"
+          type: 'success',
+          message: '配置文件设置成功'
         })
-          await this.initForm()
+        await this.initForm()
       }
     }
   }