From 8d3fd421977a063c273a6b490cb6b2cbc19be843 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Thu, 28 Dec 2023 17:51:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20openresty=20=E5=A2=9E=E5=8A=A0=E6=B8=85?= =?UTF-8?q?=E7=90=86=E5=8F=8D=E4=BB=A3=E7=BC=93=E5=AD=98=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20(#3471)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs https://github.com/1Panel-dev/1Panel/issues/2288 --- backend/app/api/v1/nginx.go | 15 +++++++++ backend/app/service/nginx.go | 21 ++++++++++++ backend/router/ro_nginx.go | 1 + backend/utils/files/file_op.go | 4 +++ cmd/server/docs/docs.go | 34 ++++++++++++++++++-- cmd/server/docs/swagger.json | 30 +++++++++++++++++ cmd/server/docs/swagger.yaml | 20 ++++++++++++ frontend/src/api/modules/nginx.ts | 4 +++ frontend/src/components/app-status/index.vue | 22 +++++++++++++ frontend/src/lang/modules/en.ts | 3 ++ frontend/src/lang/modules/tw.ts | 2 ++ frontend/src/lang/modules/zh.ts | 2 ++ 12 files changed, 156 insertions(+), 2 deletions(-) diff --git a/backend/app/api/v1/nginx.go b/backend/app/api/v1/nginx.go index e314ed085..cce59d082 100644 --- a/backend/app/api/v1/nginx.go +++ b/backend/app/api/v1/nginx.go @@ -101,3 +101,18 @@ func (b *BaseApi) UpdateNginxFile(c *gin.Context) { } helper.SuccessWithData(c, nil) } + +// @Tags OpenResty +// @Summary Clear OpenResty proxy cache +// @Description 清理 OpenResty 代理缓存 +// @Success 200 +// @Security ApiKeyAuth +// @Router /openResty/clear [post] +// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清理 Openresty 代理缓存","formatEN":"Clear nginx proxy cache"} +func (b *BaseApi) ClearNginxProxyCache(c *gin.Context) { + if err := nginxService.ClearProxyCache(); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithOutData(c) +} diff --git a/backend/app/service/nginx.go b/backend/app/service/nginx.go index c012619f2..d6891482e 100644 --- a/backend/app/service/nginx.go +++ b/backend/app/service/nginx.go @@ -2,6 +2,7 @@ package service import ( "fmt" + "github.com/1Panel-dev/1Panel/backend/utils/compose" "io" "net/http" "os" @@ -26,6 +27,7 @@ type INginxService interface { UpdateConfigByScope(req request.NginxConfigUpdate) error GetStatus() (response.NginxStatus, error) UpdateConfigFile(req request.NginxConfigFileUpdate) error + ClearProxyCache() error } func NewINginxService() INginxService { @@ -121,3 +123,22 @@ func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error } return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName) } + +func (n NginxService) ClearProxyCache() error { + nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) + if err != nil { + return err + } + cacheDir := path.Join(nginxInstall.GetPath(), "www/common/proxy/proxy_cache_dir") + fileOp := files.NewFileOp() + if fileOp.Stat(cacheDir) { + if err = fileOp.CleanDir(cacheDir); err != nil { + return err + } + _, err = compose.Restart(nginxInstall.GetComposePath()) + if err != nil { + return err + } + } + return nil +} diff --git a/backend/router/ro_nginx.go b/backend/router/ro_nginx.go index 28ff43fee..b483c8995 100644 --- a/backend/router/ro_nginx.go +++ b/backend/router/ro_nginx.go @@ -20,5 +20,6 @@ func (a *NginxRouter) InitRouter(Router *gin.RouterGroup) { groupRouter.POST("/update", baseApi.UpdateNginxConfigByScope) groupRouter.GET("/status", baseApi.GetNginxStatus) groupRouter.POST("/file", baseApi.UpdateNginxFile) + groupRouter.POST("/clear", baseApi.ClearNginxProxyCache) } } diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 0a87d0af4..24a0910e5 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -89,6 +89,10 @@ func (f FileOp) Delete(dst string) error { return os.RemoveAll(dst) } +func (f FileOp) CleanDir(dst string) error { + return cmd.ExecCmd(fmt.Sprintf("rm -rf %s/*", dst)) +} + func (f FileOp) RmRf(dst string) error { return cmd.ExecCmd(fmt.Sprintf("rm -rf %s", dst)) } diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index d288e5828..3079d81a4 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -1,5 +1,5 @@ -// Code generated by swaggo/swag. DO NOT EDIT. - +// Package docs GENERATED BY SWAG; DO NOT EDIT +// This file was generated by swaggo/swag package docs import "github.com/swaggo/swag" @@ -8107,6 +8107,32 @@ const docTemplate = `{ } } }, + "/openResty/clear": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "清理 OpenResty 代理缓存", + "tags": [ + "OpenResty" + ], + "summary": "Clear OpenResty proxy cache", + "responses": { + "200": { + "description": "OK" + } + }, + "x-panel-log": { + "BeforeFunctions": [], + "bodyKeys": [], + "formatEN": "Clear nginx proxy cache", + "formatZH": "清理 Openresty 代理缓存", + "paramKeys": [] + } + } + }, "/openResty/file": { "post": { "security": [ @@ -16204,6 +16230,9 @@ const docTemplate = `{ "state" ], "properties": { + "excludeAppStore": { + "type": "boolean" + }, "filters": { "type": "string" }, @@ -16699,6 +16728,7 @@ const docTemplate = `{ "enum": [ "self", "select", + "import", "import-paste", "import-local" ] diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json index 5ef899b02..5c5996a61 100644 --- a/cmd/server/docs/swagger.json +++ b/cmd/server/docs/swagger.json @@ -8100,6 +8100,32 @@ } } }, + "/openResty/clear": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "清理 OpenResty 代理缓存", + "tags": [ + "OpenResty" + ], + "summary": "Clear OpenResty proxy cache", + "responses": { + "200": { + "description": "OK" + } + }, + "x-panel-log": { + "BeforeFunctions": [], + "bodyKeys": [], + "formatEN": "Clear nginx proxy cache", + "formatZH": "清理 Openresty 代理缓存", + "paramKeys": [] + } + } + }, "/openResty/file": { "post": { "security": [ @@ -16197,6 +16223,9 @@ "state" ], "properties": { + "excludeAppStore": { + "type": "boolean" + }, "filters": { "type": "string" }, @@ -16692,6 +16721,7 @@ "enum": [ "self", "select", + "import", "import-paste", "import-local" ] diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml index f0d01437b..41e80debf 100644 --- a/cmd/server/docs/swagger.yaml +++ b/cmd/server/docs/swagger.yaml @@ -1867,6 +1867,8 @@ definitions: type: object dto.PageContainer: properties: + excludeAppStore: + type: boolean filters: type: string name: @@ -2200,6 +2202,7 @@ definitions: enum: - self - select + - import - import-paste - import-local type: string @@ -9916,6 +9919,23 @@ paths: summary: Load OpenResty conf tags: - OpenResty + /openResty/clear: + post: + description: 清理 OpenResty 代理缓存 + responses: + "200": + description: OK + security: + - ApiKeyAuth: [] + summary: Clear OpenResty proxy cache + tags: + - OpenResty + x-panel-log: + BeforeFunctions: [] + bodyKeys: [] + formatEN: Clear nginx proxy cache + formatZH: 清理 Openresty 代理缓存 + paramKeys: [] /openResty/file: post: consumes: diff --git a/frontend/src/api/modules/nginx.ts b/frontend/src/api/modules/nginx.ts index 60f9c4807..c36cd6775 100644 --- a/frontend/src/api/modules/nginx.ts +++ b/frontend/src/api/modules/nginx.ts @@ -21,3 +21,7 @@ export const GetNginxStatus = () => { export const UpdateNginxConfigFile = (req: Nginx.NginxFileUpdate) => { return http.post(`/openresty/file`, req); }; + +export const ClearNginxCache = () => { + return http.post(`/openresty/clear`); +}; diff --git a/frontend/src/components/app-status/index.vue b/frontend/src/components/app-status/index.vue index 58c9ef431..a03e3e942 100644 --- a/frontend/src/components/app-status/index.vue +++ b/frontend/src/components/app-status/index.vue @@ -50,6 +50,17 @@ > {{ $t('commons.button.set') }} + + + {{ $t('nginx.clearProxyCache') }} + @@ -88,6 +99,7 @@ import Status from '@/components/status/index.vue'; import { ElMessageBox } from 'element-plus'; import i18n from '@/lang'; import { MsgSuccess } from '@/utils/message'; +import { ClearNginxCache } from '@/api/modules/nginx'; const props = defineProps({ appKey: { @@ -164,6 +176,16 @@ const onCheck = async () => { }); }; +const clear = () => { + ElMessageBox.confirm(i18n.global.t('nginx.clearProxyCacheWarn'), i18n.global.t('nginx.clearProxyCache'), { + confirmButtonText: i18n.global.t('commons.button.confirm'), + cancelButtonText: i18n.global.t('commons.button.cancel'), + }).then(async () => { + await ClearNginxCache(); + MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); + }); +}; + const onOperate = async (operation: string) => { em('update:maskShow', false); operateReq.operate = operation; diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 06ff69651..f267b160b 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -1858,6 +1858,9 @@ const message = { nginxConfig: 'Settings', configResource: 'Configuration', saveAndReload: 'Save and Reload', + clearProxyCache: 'Clear reverse proxy cache', + clearProxyCacheWarn: + 'Clearing the reverse proxy cache will affect all websites configured with cache and requires restarting OpenResty. Do you want to continue? ', }, ssl: { create: 'Apply Certificate', diff --git a/frontend/src/lang/modules/tw.ts b/frontend/src/lang/modules/tw.ts index 67fd13b95..83faf84e1 100644 --- a/frontend/src/lang/modules/tw.ts +++ b/frontend/src/lang/modules/tw.ts @@ -1744,6 +1744,8 @@ const message = { nginxConfig: '設置', configResource: '配置修改', saveAndReload: '保存並重載', + clearProxyCache: '清除反代快取', + clearProxyCacheWarn: '清除反代快取會影響所有配置快取的網站,並且需要重新啟動 OpenResty, 是否繼續? ', }, ssl: { create: '申請證書', diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index ea4f8808c..67f5d36b4 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -1744,6 +1744,8 @@ const message = { nginxConfig: '设置', configResource: '配置修改', saveAndReload: '保存并重载', + clearProxyCache: '清除反代缓存', + clearProxyCacheWarn: '清除反代缓存会影响所有配置缓存的网站,并且需要重启 OpenResty, 是否继续?', }, ssl: { create: '申请证书',