Skip to content

Commit 6323688

Browse files
pixelmaxQmpiexlMax(奇淼Gor-cQINsring
authored
发布dev2.8.2版本 (#2025)
* refactor(server): 重构服务器启动和重载逻辑 将服务器启动和重载逻辑进行重构,提取初始化系统为单独函数,优化代码结构。删除冗余的服务器初始化文件,统一使用新的 `server_run.go` 实现优雅关闭和重载功能。同时,将“重启服务”改为“重载服务”以更准确地描述功能。 * refactor: 重构系统事件处理、JWT和Casbin相关逻辑 - 将系统重载逻辑提取到独立的`system_events.go`文件中,并引入全局事件管理器 - 将JWT相关操作从`service`层移动到`utils`层,减少服务层依赖 - 将Casbin实例管理逻辑提取到`utils`层,统一管理Casbin实例的初始化和获取 - 删除冗余的`CreateSysOperationRecord`方法,优化操作记录中间件逻辑 * refactor(server): 重构服务初始化和关闭逻辑 将 `RunServer` 函数重命名为 `initServer`,并调整其调用方式以简化代码。同时,在系统初始化时添加 `SetupHandlers` 函数以注册全局处理函数,提升代码可维护性。 * fix: 修复自动化代码enum查询条件的bug * fix: 修复组合模式下,顶部菜单重复bug * refactor: 修改名称 RunWindowsServer ==> RunServer * 新增mcp * feat: 支持mcp服务 * feat:调整mcp结构,增加客户端和测试用例 * feat:更换mcp基础包和结构 * feat:提交客户端工具测试用例 * feat: 增加自动创建 mcp Tool模板 功能 * fix: 增加默认值属性 * feat: 调整初始化menu的逻辑 * feat: 调整初始config.yaml * feat: 增加全局GVA_MCP_SERVER属性,方便灵活化开发。 * feat: 优化自动化mcp逻辑和成功展示 * feat: 优化mcp tool nickname工具 * feat: 发布2.8.2 Beta版本 --------- Co-authored-by: piexlMax(奇淼 <qimiaojiangjizhao@gmail.com> Co-authored-by: Gor-c <creup@outlook.com> Co-authored-by: QIN xiansheng <sjjlnaps@163.com>
1 parent 7a7c811 commit 6323688

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1124
-276
lines changed

server/api/v1/system/auto_code_mcp.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package system
2+
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/global"
5+
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
6+
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
// Create
11+
// @Tags mcp
12+
// @Summary 自动McpTool
13+
// @Security ApiKeyAuth
14+
// @accept application/json
15+
// @Produce application/json
16+
// @Param data body request.AutoMcpTool true "创建自动代码"
17+
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
18+
// @Router /autoCode/mcp [post]
19+
func (a *AutoCodeTemplateApi) MCP(c *gin.Context) {
20+
var info request.AutoMcpTool
21+
err := c.ShouldBindJSON(&info)
22+
if err != nil {
23+
response.FailWithMessage(err.Error(), c)
24+
return
25+
}
26+
27+
toolFilePath, err := autoCodeTemplateService.CreateMcp(c.Request.Context(), info)
28+
if err != nil {
29+
response.FailWithMessage("创建失败", c)
30+
global.GVA_LOG.Error(err.Error())
31+
return
32+
}
33+
response.OkWithMessage("创建成功,MCP Tool路径:"+toolFilePath, c)
34+
}

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/config.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,10 @@ cors:
274274
allow-headers: content-type
275275
allow-methods: GET, POST
276276
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
277-
allow-credentials: true # 布尔值
277+
allow-credentials: true # 布尔值
278+
mcp:
279+
name: GVA_MCP
280+
version: v1.0.0
281+
sse_path: /sse
282+
message_path: /message
283+
url_prefix: ''

server/config/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ type Server struct {
3434

3535
// 跨域配置
3636
Cors CORS `mapstructure:"cors" json:"cors" yaml:"cors"`
37+
38+
// MCP配置
39+
MCP MCP `mapstructure:"mcp" json:"mcp" yaml:"mcp"`
3740
}

server/config/mcp.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package config
2+
3+
type MCP struct {
4+
Name string `mapstructure:"name" json:"name" yaml:"name"` // MCP名称
5+
Version string `mapstructure:"version" json:"version" yaml:"version"` // MCP版本
6+
SSEPath string `mapstructure:"sse_path" json:"sse_path" yaml:"sse_path"` // SSE路径
7+
MessagePath string `mapstructure:"message_path" json:"message_path" yaml:"message_path"` // 消息路径
8+
UrlPrefix string `mapstructure:"url_prefix" json:"url_prefix" yaml:"url_prefix"` // URL前缀
9+
}

server/core/server.go

+7-11
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,23 +32,22 @@ 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
44-
当前版本:v2.8.1
38+
当前版本:v2.8.2
4539
加群方式:微信号:shouzi_1994 QQ群:470239250
4640
项目地址:https://github.com/flipped-aurora/gin-vue-admin
4741
插件市场:https://plugin.gin-vue-admin.com
4842
GVA讨论社区:https://support.qq.com/products/371961
4943
默认自动化文档地址:http://127.0.0.1%s/swagger/index.html
44+
默认MCP SSE地址:http://127.0.0.1%s%s
45+
默认MCP Message地址:http://127.0.0.1%s%s
5046
默认前端文件运行地址:http://127.0.0.1:8080
5147
--------------------------------------版权声明--------------------------------------
5248
** 版权所有方:flipped-aurora开源团队 **
5349
** 版权持有公司:北京翻转极光科技有限责任公司 **
5450
** 剔除授权标识需购买商用授权:https://gin-vue-admin.com/empower/index.html **
55-
`, address)
56-
global.GVA_LOG.Error(s.ListenAndServe().Error())
51+
`, address, address, global.GVA_CONFIG.MCP.SSEPath, address, global.GVA_CONFIG.MCP.MessagePath)
52+
initServer(address, Router, 10*time.Minute, 10*time.Minute)
5753
}

server/core/server_other.go

-19
This file was deleted.

server/core/server_run.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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("关闭WEB服务...")
49+
50+
// 设置5秒的超时时间
51+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
52+
53+
defer cancel()
54+
55+
if err := srv.Shutdown(ctx); err != nil {
56+
zap.L().Fatal("WEB服务关闭异常", zap.Error(err))
57+
}
58+
59+
zap.L().Info("WEB服务已关闭")
60+
}

server/core/server_win.go

-21
This file was deleted.

server/docs/docs.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/global/global.go

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package global
22

33
import (
44
"fmt"
5+
"github.com/mark3labs/mcp-go/server"
56
"sync"
67

78
"github.com/gin-gonic/gin"
@@ -35,6 +36,7 @@ var (
3536
GVA_Concurrency_Control = &singleflight.Group{}
3637
GVA_ROUTERS gin.RoutesInfo
3738
GVA_ACTIVE_DBNAME *string
39+
GVA_MCP_SERVER *server.MCPServer
3840
BlackCache local_cache.Cache
3941
lock sync.RWMutex
4042
)

server/go.mod

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
module github.com/flipped-aurora/gin-vue-admin/server
22

3-
go 1.22.2
3+
go 1.23
4+
5+
toolchain go1.23.9
46

57
require (
8+
github.com/ThinkInAIXYZ/go-mcp v0.2.2
69
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
710
github.com/aws/aws-sdk-go v1.55.6
811
github.com/casbin/casbin/v2 v2.103.0
912
github.com/casbin/gorm-adapter/v3 v3.32.0
1013
github.com/fsnotify/fsnotify v1.8.0
11-
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6
1214
github.com/gin-gonic/gin v1.10.0
1315
github.com/glebarez/sqlite v1.11.0
1416
github.com/go-sql-driver/mysql v1.8.1
@@ -55,7 +57,7 @@ require (
5557
filippo.io/edwards25519 v1.1.0 // indirect
5658
github.com/BurntSushi/toml v1.4.0 // indirect
5759
github.com/KyleBanks/depth v1.2.1 // indirect
58-
github.com/STARRY-S/zip v0.1.0 // indirect
60+
github.com/STARRY-S/zip v0.2.1 // indirect
5961
github.com/alex-ant/gomath v0.0.0-20160516115720-89013a210a82 // indirect
6062
github.com/andybalholm/brotli v1.1.1 // indirect
6163
github.com/bmatcuk/doublestar/v4 v4.8.0 // indirect
@@ -111,17 +113,20 @@ require (
111113
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 // indirect
112114
github.com/magiconair/properties v1.8.9 // indirect
113115
github.com/mailru/easyjson v0.9.0 // indirect
116+
github.com/mark3labs/mcp-go v0.26.0 // indirect
114117
github.com/mattn/go-isatty v0.0.20 // indirect
115118
github.com/microsoft/go-mssqldb v1.8.0 // indirect
116119
github.com/minio/md5-simd v1.1.2 // indirect
120+
github.com/minio/minlz v1.0.0 // indirect
117121
github.com/mitchellh/mapstructure v1.5.0 // indirect
118122
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
119123
github.com/modern-go/reflect2 v1.0.2 // indirect
120124
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
121125
github.com/montanaflynn/stats v0.7.1 // indirect
122126
github.com/mozillazg/go-httpheader v0.4.0 // indirect
123127
github.com/ncruces/go-strftime v0.1.9 // indirect
124-
github.com/nwaples/rardecode/v2 v2.0.1 // indirect
128+
github.com/nwaples/rardecode/v2 v2.1.0 // indirect
129+
github.com/orcaman/concurrent-map/v2 v2.0.1 // indirect
125130
github.com/otiai10/mint v1.6.3 // indirect
126131
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
127132
github.com/pierrec/lz4/v4 v4.1.22 // indirect
@@ -141,6 +146,9 @@ require (
141146
github.com/spf13/pflag v1.0.5 // indirect
142147
github.com/subosito/gotenv v1.6.0 // indirect
143148
github.com/therootcompany/xz v1.0.1 // indirect
149+
github.com/tidwall/gjson v1.18.0 // indirect
150+
github.com/tidwall/match v1.1.1 // indirect
151+
github.com/tidwall/pretty v1.2.0 // indirect
144152
github.com/tklauser/go-sysconf v0.3.14 // indirect
145153
github.com/tklauser/numcpus v0.9.0 // indirect
146154
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
@@ -152,6 +160,7 @@ require (
152160
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
153161
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6 // indirect
154162
github.com/xuri/nfp v0.0.0-20250111060730-82a408b9aa71 // indirect
163+
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
155164
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
156165
github.com/yusufpapurcu/wmi v1.2.4 // indirect
157166
go.uber.org/multierr v1.11.0 // indirect

0 commit comments

Comments
 (0)