From 5a3a1e694bb64e0d2a852791617029bbdace12f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Thu, 23 Nov 2023 21:42:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Rsync=20=E6=8F=92=E4=BB=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=85=8D=E7=BD=AE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/plugins/rsync_controller.go | 46 +++++++++++++ .../requests/plugins/rsync/update_config.go | 32 +++++++++ docs/docs.go | 67 +++++++++++++++++++ docs/swagger.json | 67 +++++++++++++++++++ docs/swagger.yaml | 41 ++++++++++++ routes/plugin.go | 2 + scripts/rsync/install.sh | 2 +- 7 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 app/http/requests/plugins/rsync/update_config.go diff --git a/app/http/controllers/plugins/rsync_controller.go b/app/http/controllers/plugins/rsync_controller.go index f2d7375a..5831c489 100644 --- a/app/http/controllers/plugins/rsync_controller.go +++ b/app/http/controllers/plugins/rsync_controller.go @@ -346,3 +346,49 @@ secrets file = /etc/rsyncd.secrets return controllers.Success(ctx, nil) } + +// GetConfig +// +// @Summary 获取配置 +// @Description 获取 Rsync 配置 +// @Tags 插件-Rsync +// @Produce json +// @Security BearerToken +// @Success 200 {object} controllers.SuccessResponse +// @Router /plugins/rsync/config [get] +func (r *RsyncController) GetConfig(ctx http.Context) http.Response { + config, err := tools.Read("/etc/rsyncd.conf") + if err != nil { + return controllers.Error(ctx, http.StatusInternalServerError, err.Error()) + } + + return controllers.Success(ctx, config) +} + +// UpdateConfig +// +// @Summary 更新配置 +// @Description 更新 Rsync 配置 +// @Tags 插件-Rsync +// @Produce json +// @Security BearerToken +// @Param data body requests.UpdateConfig true "request" +// @Success 200 {object} controllers.SuccessResponse +// @Router /plugins/rsync/config [post] +func (r *RsyncController) UpdateConfig(ctx http.Context) http.Response { + var updateRequest requests.UpdateConfig + sanitize := controllers.Sanitize(ctx, &updateRequest) + if sanitize != nil { + return sanitize + } + + if err := tools.Write("/etc/rsyncd.conf", updateRequest.Config, 0644); err != nil { + return controllers.Error(ctx, http.StatusInternalServerError, err.Error()) + } + + if err := tools.ServiceRestart("rsyncd"); err != nil { + return controllers.Error(ctx, http.StatusInternalServerError, err.Error()) + } + + return controllers.Success(ctx, nil) +} diff --git a/app/http/requests/plugins/rsync/update_config.go b/app/http/requests/plugins/rsync/update_config.go new file mode 100644 index 00000000..f954af57 --- /dev/null +++ b/app/http/requests/plugins/rsync/update_config.go @@ -0,0 +1,32 @@ +package requests + +import ( + "github.com/goravel/framework/contracts/http" + "github.com/goravel/framework/contracts/validation" +) + +type UpdateConfig struct { + Config string `form:"config" json:"config"` +} + +func (r *UpdateConfig) Authorize(ctx http.Context) error { + return nil +} + +func (r *UpdateConfig) Rules(ctx http.Context) map[string]string { + return map[string]string{ + "config": "required|string", + } +} + +func (r *UpdateConfig) Messages(ctx http.Context) map[string]string { + return map[string]string{} +} + +func (r *UpdateConfig) Attributes(ctx http.Context) map[string]string { + return map[string]string{} +} + +func (r *UpdateConfig) PrepareForValidation(ctx http.Context, data validation.Data) error { + return nil +} diff --git a/docs/docs.go b/docs/docs.go index bbe15403..da1eecb4 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1661,6 +1661,65 @@ const docTemplate = `{ } } }, + "/plugins/rsync/config": { + "get": { + "security": [ + { + "BearerToken": [] + } + ], + "description": "获取 Rsync 配置", + "produces": [ + "application/json" + ], + "tags": [ + "插件-Rsync" + ], + "summary": "获取配置", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + }, + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "description": "更新 Rsync 配置", + "produces": [ + "application/json" + ], + "tags": [ + "插件-Rsync" + ], + "summary": "更新配置", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/requests.UpdateConfig" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, "/plugins/rsync/modules": { "get": { "security": [ @@ -2455,6 +2514,14 @@ const docTemplate = `{ } } }, + "requests.UpdateConfig": { + "type": "object", + "properties": { + "config": { + "type": "string" + } + } + }, "requests.UserStore": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 938c8dd9..ecfc576e 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -1654,6 +1654,65 @@ } } }, + "/plugins/rsync/config": { + "get": { + "security": [ + { + "BearerToken": [] + } + ], + "description": "获取 Rsync 配置", + "produces": [ + "application/json" + ], + "tags": [ + "插件-Rsync" + ], + "summary": "获取配置", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + }, + "post": { + "security": [ + { + "BearerToken": [] + } + ], + "description": "更新 Rsync 配置", + "produces": [ + "application/json" + ], + "tags": [ + "插件-Rsync" + ], + "summary": "更新配置", + "parameters": [ + { + "description": "request", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/requests.UpdateConfig" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controllers.SuccessResponse" + } + } + } + } + }, "/plugins/rsync/modules": { "get": { "security": [ @@ -2448,6 +2507,14 @@ } } }, + "requests.UpdateConfig": { + "type": "object", + "properties": { + "config": { + "type": "string" + } + } + }, "requests.UserStore": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 9358c60b..2417ae78 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -350,6 +350,11 @@ definitions: waf_mode: type: string type: object + requests.UpdateConfig: + properties: + config: + type: string + type: object requests.UserStore: properties: ca: @@ -1473,6 +1478,42 @@ paths: summary: 更新备注 tags: - 网站管理 + /plugins/rsync/config: + get: + description: 获取 Rsync 配置 + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controllers.SuccessResponse' + security: + - BearerToken: [] + summary: 获取配置 + tags: + - 插件-Rsync + post: + description: 更新 Rsync 配置 + parameters: + - description: request + in: body + name: data + required: true + schema: + $ref: '#/definitions/requests.UpdateConfig' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controllers.SuccessResponse' + security: + - BearerToken: [] + summary: 更新配置 + tags: + - 插件-Rsync /plugins/rsync/modules: get: description: 列出所有 Rsync 模块 diff --git a/routes/plugin.go b/routes/plugin.go index e0d0944a..bed104d0 100644 --- a/routes/plugin.go +++ b/routes/plugin.go @@ -292,6 +292,8 @@ func Plugin() { route.Post("modules", rsyncController.Create) route.Post("modules/{name}", rsyncController.Update) route.Delete("modules/{name}", rsyncController.Destroy) + route.Get("config", rsyncController.GetConfig) + route.Post("config", rsyncController.UpdateConfig) }) r.Prefix("toolbox").Group(func(route route.Router) { toolboxController := plugins.NewToolBoxController() diff --git a/scripts/rsync/install.sh b/scripts/rsync/install.sh index 2369d7a1..7f10f90a 100644 --- a/scripts/rsync/install.sh +++ b/scripts/rsync/install.sh @@ -50,7 +50,7 @@ log file = /var/log/rsyncd.log EOF touch /etc/rsyncd.secrets -chmod 600 /etc/rsyncd.conf +chmod 644 /etc/rsyncd.conf chmod 600 /etc/rsyncd.secrets # 写入服务文件