Skip to content

Commit 5d304f9

Browse files
committed
Merge branch 'dev-282' into dev-mcp
2 parents d2fb2d6 + 3a84455 commit 5d304f9

25 files changed

+275
-219
lines changed

server/api/v1/system/sys_operation_record.go

-25
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,6 @@ import (
1313

1414
type OperationRecordApi struct{}
1515

16-
// CreateSysOperationRecord
17-
// @Tags SysOperationRecord
18-
// @Summary 创建SysOperationRecord
19-
// @Security ApiKeyAuth
20-
// @accept application/json
21-
// @Produce application/json
22-
// @Param data body system.SysOperationRecord true "创建SysOperationRecord"
23-
// @Success 200 {object} response.Response{msg=string} "创建SysOperationRecord"
24-
// @Router /sysOperationRecord/createSysOperationRecord [post]
25-
func (s *OperationRecordApi) CreateSysOperationRecord(c *gin.Context) {
26-
var sysOperationRecord system.SysOperationRecord
27-
err := c.ShouldBindJSON(&sysOperationRecord)
28-
if err != nil {
29-
response.FailWithMessage(err.Error(), c)
30-
return
31-
}
32-
err = operationRecordService.CreateSysOperationRecord(sysOperationRecord)
33-
if err != nil {
34-
global.GVA_LOG.Error("创建失败!", zap.Error(err))
35-
response.FailWithMessage("创建失败", c)
36-
return
37-
}
38-
response.OkWithMessage("创建成功", c)
39-
}
40-
4116
// DeleteSysOperationRecord
4217
// @Tags SysOperationRecord
4318
// @Summary 删除SysOperationRecord

server/api/v1/system/sys_system.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,20 @@ func (s *SystemApi) SetSystemConfig(c *gin.Context) {
5555

5656
// ReloadSystem
5757
// @Tags System
58-
// @Summary 重启系统
58+
// @Summary 重载系统
5959
// @Security ApiKeyAuth
6060
// @Produce application/json
61-
// @Success 200 {object} response.Response{msg=string} "重启系统"
61+
// @Success 200 {object} response.Response{msg=string} "重载系统"
6262
// @Router /system/reloadSystem [post]
6363
func (s *SystemApi) ReloadSystem(c *gin.Context) {
64-
err := utils.Reload()
64+
// 触发系统重载事件
65+
err := utils.GlobalSystemEvents.TriggerReload()
6566
if err != nil {
66-
global.GVA_LOG.Error("重启系统失败!", zap.Error(err))
67-
response.FailWithMessage("重启系统失败", c)
67+
global.GVA_LOG.Error("重载系统失败!", zap.Error(err))
68+
response.FailWithMessage("重载系统失败:"+err.Error(), c)
6869
return
6970
}
70-
response.OkWithMessage("重启系统成功", c)
71+
response.OkWithMessage("重载系统成功", c)
7172
}
7273

7374
// GetServerInfo

server/api/v1/system/sys_user.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (b *BaseApi) TokenNext(c *gin.Context, user system.SysUser) {
9393
}
9494

9595
if jwtStr, err := jwtService.GetRedisJWT(user.Username); err == redis.Nil {
96-
if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
96+
if err := utils.SetRedisJWT(token, user.Username); err != nil {
9797
global.GVA_LOG.Error("设置登录状态失败!", zap.Error(err))
9898
response.FailWithMessage("设置登录状态失败", c)
9999
return
@@ -114,7 +114,7 @@ func (b *BaseApi) TokenNext(c *gin.Context, user system.SysUser) {
114114
response.FailWithMessage("jwt作废失败", c)
115115
return
116116
}
117-
if err := jwtService.SetRedisJWT(token, user.GetUsername()); err != nil {
117+
if err := utils.SetRedisJWT(token, user.GetUsername()); err != nil {
118118
response.FailWithMessage("设置登录状态失败", c)
119119
return
120120
}

server/core/server.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ import (
66
"github.com/flipped-aurora/gin-vue-admin/server/initialize"
77
"github.com/flipped-aurora/gin-vue-admin/server/service/system"
88
"go.uber.org/zap"
9+
"time"
910
)
1011

11-
type server interface {
12-
ListenAndServe() error
13-
}
14-
15-
func RunWindowsServer() {
12+
func RunServer() {
1613
if global.GVA_CONFIG.System.UseRedis {
1714
// 初始化redis服务
1815
initialize.Redis()
@@ -35,9 +32,6 @@ func RunWindowsServer() {
3532
Router := initialize.Routers()
3633

3734
address := fmt.Sprintf(":%d", global.GVA_CONFIG.System.Addr)
38-
s := initServer(address, Router)
39-
40-
global.GVA_LOG.Info("server run success on ", zap.String("address", address))
4135

4236
fmt.Printf(`
4337
欢迎使用 gin-vue-admin
@@ -53,5 +47,6 @@ func RunWindowsServer() {
5347
** 版权持有公司:北京翻转极光科技有限责任公司 **
5448
** 剔除授权标识需购买商用授权:https://gin-vue-admin.com/empower/index.html **
5549
`, address)
56-
global.GVA_LOG.Error(s.ListenAndServe().Error())
50+
51+
initServer(address, Router, 10*time.Minute, 10*time.Minute)
5752
}

server/core/server_other.go

-19
This file was deleted.

server/core/server_run.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package core
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
"time"
11+
12+
"github.com/gin-gonic/gin"
13+
"go.uber.org/zap"
14+
)
15+
16+
type server interface {
17+
ListenAndServe() error
18+
Shutdown(context.Context) error
19+
}
20+
21+
// initServer 启动服务并实现优雅关闭
22+
func initServer(address string, router *gin.Engine, readTimeout, writeTimeout time.Duration) {
23+
// 创建服务
24+
srv := &http.Server{
25+
Addr: address,
26+
Handler: router,
27+
ReadTimeout: readTimeout,
28+
WriteTimeout: writeTimeout,
29+
MaxHeaderBytes: 1 << 20,
30+
}
31+
32+
// 在goroutine中启动服务
33+
go func() {
34+
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
35+
fmt.Printf("listen: %s\n", err)
36+
zap.L().Error("server启动失败", zap.Error(err))
37+
os.Exit(1)
38+
}
39+
}()
40+
41+
// 等待中断信号以优雅地关闭服务器
42+
quit := make(chan os.Signal, 1)
43+
// kill (无参数) 默认发送 syscall.SIGTERM
44+
// kill -2 发送 syscall.SIGINT
45+
// kill -9 发送 syscall.SIGKILL,但是无法被捕获,所以不需要添加
46+
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
47+
<-quit
48+
zap.L().Info("关闭服务器...")
49+
50+
// 设置5秒的超时时间
51+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
52+
defer cancel()
53+
54+
if err := srv.Shutdown(ctx); err != nil {
55+
zap.L().Fatal("服务器关闭异常", zap.Error(err))
56+
}
57+
58+
zap.L().Info("服务器已优雅关闭")
59+
}

server/core/server_win.go

-21
This file was deleted.

server/initialize/init.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// 假设这是初始化逻辑的一部分
2+
3+
package initialize
4+
5+
import (
6+
"github.com/flipped-aurora/gin-vue-admin/server/utils"
7+
)
8+
9+
// 初始化全局函数
10+
func SetupHandlers() {
11+
// 注册系统重载处理函数
12+
utils.GlobalSystemEvents.RegisterReloadHandler(func() error {
13+
return Reload()
14+
})
15+
}

server/initialize/reload.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package initialize
2+
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/global"
5+
"go.uber.org/zap"
6+
)
7+
8+
// Reload 优雅地重新加载系统配置
9+
func Reload() error {
10+
global.GVA_LOG.Info("正在重新加载系统配置...")
11+
12+
// 重新加载配置文件
13+
if err := global.GVA_VP.ReadInConfig(); err != nil {
14+
global.GVA_LOG.Error("重新读取配置文件失败!", zap.Error(err))
15+
return err
16+
}
17+
18+
// 重新初始化数据库连接
19+
if global.GVA_DB != nil {
20+
db, _ := global.GVA_DB.DB()
21+
err := db.Close()
22+
if err != nil {
23+
global.GVA_LOG.Error("关闭原数据库连接失败!", zap.Error(err))
24+
return err
25+
}
26+
}
27+
28+
// 重新建立数据库连接
29+
global.GVA_DB = Gorm()
30+
31+
// 重新初始化其他配置
32+
OtherInit()
33+
DBList()
34+
35+
if global.GVA_DB != nil {
36+
// 确保数据库表结构是最新的
37+
RegisterTables()
38+
}
39+
40+
// 重新初始化定时任务
41+
Timer()
42+
43+
global.GVA_LOG.Info("系统配置重新加载完成")
44+
return nil
45+
}

server/main.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,25 @@ import (
2828
// @name x-token
2929
// @BasePath /
3030
func main() {
31+
// 初始化系统
32+
initializeSystem()
33+
34+
// 运行服务器
35+
core.RunServer()
36+
}
37+
38+
// initializeSystem 初始化系统所有组件
39+
// 提取为单独函数以便于系统重载时调用
40+
func initializeSystem() {
3141
global.GVA_VP = core.Viper() // 初始化Viper
3242
initialize.OtherInit()
3343
global.GVA_LOG = core.Zap() // 初始化zap日志库
3444
zap.ReplaceGlobals(global.GVA_LOG)
3545
global.GVA_DB = initialize.Gorm() // gorm连接数据库
3646
initialize.Timer()
3747
initialize.DBList()
48+
initialize.SetupHandlers() // 注册全局函数
3849
if global.GVA_DB != nil {
3950
initialize.RegisterTables() // 初始化表
40-
// 程序结束前关闭数据库链接
41-
db, _ := global.GVA_DB.DB()
42-
defer db.Close()
4351
}
44-
core.RunWindowsServer()
4552
}

server/middleware/casbin_rbac.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ import (
66

77
"github.com/flipped-aurora/gin-vue-admin/server/global"
88
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
9-
"github.com/flipped-aurora/gin-vue-admin/server/service"
109
"github.com/flipped-aurora/gin-vue-admin/server/utils"
1110
"github.com/gin-gonic/gin"
1211
)
1312

14-
var casbinService = service.ServiceGroupApp.SystemServiceGroup.CasbinService
15-
1613
// CasbinHandler 拦截器
1714
func CasbinHandler() gin.HandlerFunc {
1815
return func(c *gin.Context) {
@@ -24,7 +21,7 @@ func CasbinHandler() gin.HandlerFunc {
2421
act := c.Request.Method
2522
// 获取用户的角色
2623
sub := strconv.Itoa(int(waitUse.AuthorityId))
27-
e := casbinService.Casbin() // 判断策略中是否存在
24+
e := utils.GetCasbin() // 判断策略中是否存在
2825
success, _ := e.Enforce(sub, obj, act)
2926
if !success {
3027
response.FailWithDetailed(gin.H{}, "权限不足", c)

server/middleware/email.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ import (
1111

1212
"github.com/flipped-aurora/gin-vue-admin/server/global"
1313
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
14-
"github.com/flipped-aurora/gin-vue-admin/server/service"
1514
"github.com/gin-gonic/gin"
1615
"go.uber.org/zap"
1716
)
1817

19-
var userService = service.ServiceGroupApp.SystemServiceGroup.UserService
20-
2118
func ErrorToEmail() gin.HandlerFunc {
2219
return func(c *gin.Context) {
2320
var username string
@@ -26,11 +23,12 @@ func ErrorToEmail() gin.HandlerFunc {
2623
username = claims.Username
2724
} else {
2825
id, _ := strconv.Atoi(c.Request.Header.Get("x-user-id"))
29-
user, err := userService.FindUserById(id)
26+
var u system.SysUser
27+
err := global.GVA_DB.Where("id = ?", id).First(&u).Error
3028
if err != nil {
3129
username = "Unknown"
3230
}
33-
username = user.Username
31+
username = u.Username
3432
}
3533
body, _ := io.ReadAll(c.Request.Body)
3634
// 再重新写回请求体body中,ioutil.ReadAll会清空c.Request.Body中的数据

0 commit comments

Comments
 (0)