feat: openresty 增加清理反代缓存的功能 (#3471)

Refs https://github.com/1Panel-dev/1Panel/issues/2288
This commit is contained in:
zhengkunwang 2023-12-28 17:51:39 +08:00 committed by GitHub
parent d05d7bbcc2
commit 8d3fd42197
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 156 additions and 2 deletions

View file

@ -101,3 +101,18 @@ func (b *BaseApi) UpdateNginxFile(c *gin.Context) {
} }
helper.SuccessWithData(c, nil) 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)
}

View file

@ -2,6 +2,7 @@ package service
import ( import (
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/utils/compose"
"io" "io"
"net/http" "net/http"
"os" "os"
@ -26,6 +27,7 @@ type INginxService interface {
UpdateConfigByScope(req request.NginxConfigUpdate) error UpdateConfigByScope(req request.NginxConfigUpdate) error
GetStatus() (response.NginxStatus, error) GetStatus() (response.NginxStatus, error)
UpdateConfigFile(req request.NginxConfigFileUpdate) error UpdateConfigFile(req request.NginxConfigFileUpdate) error
ClearProxyCache() error
} }
func NewINginxService() INginxService { func NewINginxService() INginxService {
@ -121,3 +123,22 @@ func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error
} }
return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName) 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
}

View file

@ -20,5 +20,6 @@ func (a *NginxRouter) InitRouter(Router *gin.RouterGroup) {
groupRouter.POST("/update", baseApi.UpdateNginxConfigByScope) groupRouter.POST("/update", baseApi.UpdateNginxConfigByScope)
groupRouter.GET("/status", baseApi.GetNginxStatus) groupRouter.GET("/status", baseApi.GetNginxStatus)
groupRouter.POST("/file", baseApi.UpdateNginxFile) groupRouter.POST("/file", baseApi.UpdateNginxFile)
groupRouter.POST("/clear", baseApi.ClearNginxProxyCache)
} }
} }

View file

@ -89,6 +89,10 @@ func (f FileOp) Delete(dst string) error {
return os.RemoveAll(dst) 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 { func (f FileOp) RmRf(dst string) error {
return cmd.ExecCmd(fmt.Sprintf("rm -rf %s", dst)) return cmd.ExecCmd(fmt.Sprintf("rm -rf %s", dst))
} }

View file

@ -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 package docs
import "github.com/swaggo/swag" 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": { "/openResty/file": {
"post": { "post": {
"security": [ "security": [
@ -16204,6 +16230,9 @@ const docTemplate = `{
"state" "state"
], ],
"properties": { "properties": {
"excludeAppStore": {
"type": "boolean"
},
"filters": { "filters": {
"type": "string" "type": "string"
}, },
@ -16699,6 +16728,7 @@ const docTemplate = `{
"enum": [ "enum": [
"self", "self",
"select", "select",
"import",
"import-paste", "import-paste",
"import-local" "import-local"
] ]

View file

@ -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": { "/openResty/file": {
"post": { "post": {
"security": [ "security": [
@ -16197,6 +16223,9 @@
"state" "state"
], ],
"properties": { "properties": {
"excludeAppStore": {
"type": "boolean"
},
"filters": { "filters": {
"type": "string" "type": "string"
}, },
@ -16692,6 +16721,7 @@
"enum": [ "enum": [
"self", "self",
"select", "select",
"import",
"import-paste", "import-paste",
"import-local" "import-local"
] ]

View file

@ -1867,6 +1867,8 @@ definitions:
type: object type: object
dto.PageContainer: dto.PageContainer:
properties: properties:
excludeAppStore:
type: boolean
filters: filters:
type: string type: string
name: name:
@ -2200,6 +2202,7 @@ definitions:
enum: enum:
- self - self
- select - select
- import
- import-paste - import-paste
- import-local - import-local
type: string type: string
@ -9916,6 +9919,23 @@ paths:
summary: Load OpenResty conf summary: Load OpenResty conf
tags: tags:
- OpenResty - 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: /openResty/file:
post: post:
consumes: consumes:

View file

@ -21,3 +21,7 @@ export const GetNginxStatus = () => {
export const UpdateNginxConfigFile = (req: Nginx.NginxFileUpdate) => { export const UpdateNginxConfigFile = (req: Nginx.NginxFileUpdate) => {
return http.post<any>(`/openresty/file`, req); return http.post<any>(`/openresty/file`, req);
}; };
export const ClearNginxCache = () => {
return http.post<any>(`/openresty/clear`);
};

View file

@ -50,6 +50,17 @@
> >
{{ $t('commons.button.set') }} {{ $t('commons.button.set') }}
</el-button> </el-button>
<el-divider v-if="data.app === 'OpenResty'" direction="vertical" />
<el-button
type="primary"
@click="clear"
link
:disabled="
data.status === 'Installing' || (data.status !== 'Running' && data.app === 'OpenResty')
"
>
{{ $t('nginx.clearProxyCache') }}
</el-button>
</span> </span>
<span class="warn" v-if="key === 'openresty' && (httpPort != 80 || httpsPort != 443)"> <span class="warn" v-if="key === 'openresty' && (httpPort != 80 || httpsPort != 443)">
@ -88,6 +99,7 @@ import Status from '@/components/status/index.vue';
import { ElMessageBox } from 'element-plus'; import { ElMessageBox } from 'element-plus';
import i18n from '@/lang'; import i18n from '@/lang';
import { MsgSuccess } from '@/utils/message'; import { MsgSuccess } from '@/utils/message';
import { ClearNginxCache } from '@/api/modules/nginx';
const props = defineProps({ const props = defineProps({
appKey: { 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) => { const onOperate = async (operation: string) => {
em('update:maskShow', false); em('update:maskShow', false);
operateReq.operate = operation; operateReq.operate = operation;

View file

@ -1858,6 +1858,9 @@ const message = {
nginxConfig: 'Settings', nginxConfig: 'Settings',
configResource: 'Configuration', configResource: 'Configuration',
saveAndReload: 'Save and Reload', 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: { ssl: {
create: 'Apply Certificate', create: 'Apply Certificate',

View file

@ -1744,6 +1744,8 @@ const message = {
nginxConfig: '設置', nginxConfig: '設置',
configResource: '配置修改', configResource: '配置修改',
saveAndReload: '保存並重載', saveAndReload: '保存並重載',
clearProxyCache: '清除反代快取',
clearProxyCacheWarn: '清除反代快取會影響所有配置快取的網站並且需要重新啟動 OpenResty 是否繼續 ',
}, },
ssl: { ssl: {
create: '申請證書', create: '申請證書',

View file

@ -1744,6 +1744,8 @@ const message = {
nginxConfig: '设置', nginxConfig: '设置',
configResource: '配置修改', configResource: '配置修改',
saveAndReload: '保存并重载', saveAndReload: '保存并重载',
clearProxyCache: '清除反代缓存',
clearProxyCacheWarn: '清除反代缓存会影响所有配置缓存的网站并且需要重启 OpenResty 是否继续',
}, },
ssl: { ssl: {
create: '申请证书', create: '申请证书',