From 6ba7d01875aa8bbd6e33a12b11d0dce3dfaa648f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Fri, 14 Jun 2024 10:33:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/http/controllers/file_controller.go | 1 - app/http/controllers/system_controller.go | 162 ++++++++++++ docs/docs.go | 285 +++++++++++++++++++++- docs/swagger.json | 285 +++++++++++++++++++++- docs/swagger.yaml | 172 ++++++++++++- routes/api.go | 11 + 6 files changed, 891 insertions(+), 25 deletions(-) create mode 100644 app/http/controllers/system_controller.go diff --git a/app/http/controllers/file_controller.go b/app/http/controllers/file_controller.go index 82653280..3af225c8 100644 --- a/app/http/controllers/file_controller.go +++ b/app/http/controllers/file_controller.go @@ -102,7 +102,6 @@ func (r *FileController) Content(ctx http.Context) http.Response { // @Produce json // @Security BearerToken // @Param data body requests.Save true "request" -// @Param content body string true "content" // @Success 200 {object} SuccessResponse // @Router /panel/file/save [post] func (r *FileController) Save(ctx http.Context) http.Response { diff --git a/app/http/controllers/system_controller.go b/app/http/controllers/system_controller.go new file mode 100644 index 00000000..ea0d7401 --- /dev/null +++ b/app/http/controllers/system_controller.go @@ -0,0 +1,162 @@ +package controllers + +import ( + "fmt" + + "github.com/goravel/framework/contracts/http" + + "github.com/TheTNB/panel/pkg/tools" +) + +type SystemController struct { +} + +func NewSystemController() *SystemController { + return &SystemController{} +} + +// ServiceStatus +// +// @Summary 服务状态 +// @Tags 系统 +// @Produce json +// @Security BearerToken +// @Param data query string true "request" +// @Success 200 {object} controllers.SuccessResponse +// @Router /panel/system/service/status [get] +func (r *SystemController) ServiceStatus(ctx http.Context) http.Response { + service := ctx.Request().Query("service") + status, err := tools.ServiceStatus(service) + if err != nil { + return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("获取 %s 服务运行状态失败", service)) + } + + return Success(ctx, status) +} + +// ServiceIsEnabled +// +// @Summary 是否启用服务 +// @Tags 系统 +// @Produce json +// @Security BearerToken +// @Param data query string true "request" +// @Success 200 {object} controllers.SuccessResponse +// @Router /panel/system/service/isEnabled [get] +func (r *SystemController) ServiceIsEnabled(ctx http.Context) http.Response { + service := ctx.Request().Query("service") + enabled, err := tools.ServiceIsEnabled(service) + if err != nil { + return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("获取 %s 服务启用状态失败", service)) + } + + return Success(ctx, enabled) +} + +// ServiceEnable +// +// @Summary 启用服务 +// @Tags 系统 +// @Produce json +// @Security BearerToken +// @Param data body string true "request" +// @Success 200 {object} controllers.SuccessResponse +// @Router /panel/system/service/enable [post] +func (r *SystemController) ServiceEnable(ctx http.Context) http.Response { + service := ctx.Request().Input("service") + if err := tools.ServiceEnable(service); err != nil { + return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("启用 %s 服务失败", service)) + } + + return Success(ctx, nil) +} + +// ServiceDisable +// +// @Summary 禁用服务 +// @Tags 系统 +// @Produce json +// @Security BearerToken +// @Param data body string true "request" +// @Success 200 {object} controllers.SuccessResponse +// @Router /panel/system/service/disable [post] +func (r *SystemController) ServiceDisable(ctx http.Context) http.Response { + service := ctx.Request().Input("service") + if err := tools.ServiceDisable(service); err != nil { + return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("禁用 %s 服务失败", service)) + } + + return Success(ctx, nil) +} + +// ServiceRestart +// +// @Summary 重启服务 +// @Tags 系统 +// @Produce json +// @Security BearerToken +// @Param data body string true "request" +// @Success 200 {object} controllers.SuccessResponse +// @Router /panel/system/service/restart [post] +func (r *SystemController) ServiceRestart(ctx http.Context) http.Response { + service := ctx.Request().Input("service") + if err := tools.ServiceRestart(service); err != nil { + return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("重启 %s 服务失败", service)) + } + + return Success(ctx, nil) +} + +// ServiceReload +// +// @Summary 重载服务 +// @Tags 系统 +// @Produce json +// @Security BearerToken +// @Param data body string true "request" +// @Success 200 {object} controllers.SuccessResponse +// @Router /panel/system/service/reload [post] +func (r *SystemController) ServiceReload(ctx http.Context) http.Response { + service := ctx.Request().Input("service") + if err := tools.ServiceReload(service); err != nil { + return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("重载 %s 服务失败", service)) + } + + return Success(ctx, nil) +} + +// ServiceStart +// +// @Summary 启动服务 +// @Tags 系统 +// @Produce json +// @Security BearerToken +// @Param data body string true "request" +// @Success 200 {object} controllers.SuccessResponse +// @Router /panel/system/service/start [post] +func (r *SystemController) ServiceStart(ctx http.Context) http.Response { + service := ctx.Request().Input("service") + if err := tools.ServiceStart(service); err != nil { + return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("启动 %s 服务失败", service)) + } + + return Success(ctx, nil) +} + +// ServiceStop +// +// @Summary 停止服务 +// @Tags 系统 +// @Produce json +// @Security BearerToken +// @Param data body string true "request" +// @Success 200 {object} controllers.SuccessResponse +// @Router /panel/system/service/stop [post] +func (r *SystemController) ServiceStop(ctx http.Context) http.Response { + service := ctx.Request().Input("service") + if err := tools.ServiceStop(service); err != nil { + return Error(ctx, http.StatusInternalServerError, fmt.Sprintf("停止 %s 服务失败", service)) + } + + return Success(ctx, nil) +} diff --git a/docs/docs.go b/docs/docs.go index 9b7b6dc2..5c6dbaa3 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -2505,15 +2505,6 @@ const docTemplate = `{ "schema": { "$ref": "#/definitions/requests.Save" } - }, - { - "description": "content", - "name": "content", - "in": "body", - "required": true, - "schema": { - "type": "string" - } } ], "responses": { @@ -2707,6 +2698,282 @@ const docTemplate = `{ } } }, + "/panel/system/service/disable": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "禁用服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/enable": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "启用服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/isEnabled": { + "get": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "是否启用服务", + "parameters": [ + { + "type": "string", + "description": "request", + "name": "data", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/reload": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "重载服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/restart": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "重启服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/start": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "启动服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/status": { + "get": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "服务状态", + "parameters": [ + { + "type": "string", + "description": "request", + "name": "data", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/stop": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "停止服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, "/panel/user/info": { "get": { "security": [ diff --git a/docs/swagger.json b/docs/swagger.json index dad152af..2479241f 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -2498,15 +2498,6 @@ "schema": { "$ref": "#/definitions/requests.Save" } - }, - { - "description": "content", - "name": "content", - "in": "body", - "required": true, - "schema": { - "type": "string" - } } ], "responses": { @@ -2700,6 +2691,282 @@ } } }, + "/panel/system/service/disable": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "禁用服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/enable": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "启用服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/isEnabled": { + "get": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "是否启用服务", + "parameters": [ + { + "type": "string", + "description": "request", + "name": "data", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/reload": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "重载服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/restart": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "重启服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/start": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "启动服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/status": { + "get": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "服务状态", + "parameters": [ + { + "type": "string", + "description": "request", + "name": "data", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, + "/panel/system/service/stop": { + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "系统" + ], + "summary": "停止服务", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, "/panel/user/info": { "get": { "security": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index a545a8fc..4eca7b6a 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -2180,12 +2180,6 @@ paths: required: true schema: $ref: '#/definitions/requests.Save' - - description: content - in: body - name: content - required: true - schema: - type: string produces: - application/json responses: @@ -2311,6 +2305,172 @@ paths: summary: 更新设置 tags: - 面板设置 + /panel/system/service/disable: + post: + parameters: + - description: request + in: body + name: data + required: true + schema: + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controllers.SuccessResponse' + security: + - BearerToken: [] + summary: 禁用服务 + tags: + - 系统 + /panel/system/service/enable: + post: + parameters: + - description: request + in: body + name: data + required: true + schema: + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controllers.SuccessResponse' + security: + - BearerToken: [] + summary: 启用服务 + tags: + - 系统 + /panel/system/service/isEnabled: + get: + parameters: + - description: request + in: query + name: data + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controllers.SuccessResponse' + security: + - BearerToken: [] + summary: 是否启用服务 + tags: + - 系统 + /panel/system/service/reload: + post: + parameters: + - description: request + in: body + name: data + required: true + schema: + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controllers.SuccessResponse' + security: + - BearerToken: [] + summary: 重载服务 + tags: + - 系统 + /panel/system/service/restart: + post: + parameters: + - description: request + in: body + name: data + required: true + schema: + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controllers.SuccessResponse' + security: + - BearerToken: [] + summary: 重启服务 + tags: + - 系统 + /panel/system/service/start: + post: + parameters: + - description: request + in: body + name: data + required: true + schema: + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controllers.SuccessResponse' + security: + - BearerToken: [] + summary: 启动服务 + tags: + - 系统 + /panel/system/service/status: + get: + parameters: + - description: request + in: query + name: data + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controllers.SuccessResponse' + security: + - BearerToken: [] + summary: 服务状态 + tags: + - 系统 + /panel/system/service/stop: + post: + parameters: + - description: request + in: body + name: data + required: true + schema: + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controllers.SuccessResponse' + security: + - BearerToken: [] + summary: 停止服务 + tags: + - 系统 /panel/user/info: get: description: 获取当前登录用户信息 diff --git a/routes/api.go b/routes/api.go index c2014f28..035a70be 100644 --- a/routes/api.go +++ b/routes/api.go @@ -199,6 +199,17 @@ func Api() { r.Get("list", settingController.List) r.Post("update", settingController.Update) }) + r.Prefix("system").Middleware(middleware.Jwt()).Group(func(r route.Router) { + controller := controllers.NewSystemController() + r.Get("service/status", controller.ServiceStatus) + r.Get("service/isEnabled", controller.ServiceIsEnabled) + r.Post("service/enable", controller.ServiceEnable) + r.Post("service/disable", controller.ServiceDisable) + r.Post("service/restart", controller.ServiceRestart) + r.Post("service/reload", controller.ServiceReload) + r.Post("service/start", controller.ServiceStart) + r.Post("service/stop", controller.ServiceStop) + }) }) // 文档