Skip to content

Commit f42e3a2

Browse files
committed
新增api重启系统(支持非windows)
修改 - server/api/v1/sys_system.go - server/router/sys_system.go
1 parent 625ff31 commit f42e3a2

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

server/api/v1/sys_system.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import (
55
"gin-vue-admin/model"
66
"gin-vue-admin/model/response"
77
"gin-vue-admin/service"
8+
"os"
9+
"os/exec"
10+
"runtime"
11+
"strconv"
12+
813
"github.com/gin-gonic/gin"
914
"go.uber.org/zap"
1015
)
@@ -42,7 +47,6 @@ func SetSystemConfig(c *gin.Context) {
4247
}
4348
}
4449

45-
// 本方法开发中 开发者windows系统 缺少linux系统所需的包 因此搁置
4650
// @Tags System
4751
// @Summary 重启系统
4852
// @Security ApiKeyAuth
@@ -51,14 +55,20 @@ func SetSystemConfig(c *gin.Context) {
5155
// @Success 200 {string} string "{"success":true,"data":{},"msg":"重启系统成功"}"
5256
// @Router /system/ReloadSystem [post]
5357
func ReloadSystem(c *gin.Context) {
54-
var sys model.System
55-
_ = c.ShouldBindJSON(&sys)
56-
if err := service.SetSystemConfig(sys); err != nil {
58+
if runtime.GOOS == "windows" {
59+
response.FailWithMessage("系统不支持", c)
60+
return
61+
}
62+
pid := os.Getpid()
63+
cmd := exec.Command("kill", "-1", strconv.Itoa(pid))
64+
err := cmd.Run()
65+
if err != nil {
5766
global.GVA_LOG.Error("重启系统失败!", zap.Any("err", err))
5867
response.FailWithMessage("重启系统失败", c)
59-
} else {
60-
response.OkWithMessage("重启系统成功", c)
68+
return
6169
}
70+
response.OkWithMessage("重启系统成功", c)
71+
return
6272
}
6373

6474
// @Tags System

server/router/sys_system.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ func InitSystemRouter(Router *gin.RouterGroup) {
1212
SystemRouter.POST("getSystemConfig", v1.GetSystemConfig) // 获取配置文件内容
1313
SystemRouter.POST("setSystemConfig", v1.SetSystemConfig) // 设置配置文件内容
1414
SystemRouter.POST("getServerInfo", v1.GetServerInfo) // 获取服务器信息
15+
SystemRouter.POST("reloadSystem", v1.ReloadSystem) // 获取服务器信息
1516
}
1617
}

0 commit comments

Comments
 (0)