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)
}
// @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 (
"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
}

View file

@ -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)
}
}

View file

@ -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))
}

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
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"
]

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": {
"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"
]

View file

@ -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:

View file

@ -21,3 +21,7 @@ export const GetNginxStatus = () => {
export const UpdateNginxConfigFile = (req: Nginx.NginxFileUpdate) => {
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') }}
</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 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 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;

View file

@ -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',

View file

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

View file

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