1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 00:09:16 +08:00

feat(website): Add Cache Clearing Functionality for Website Reverse P… (#7551)

This commit is contained in:
zhengkunwang 2024-12-24 17:24:47 +08:00 committed by GitHub
parent 542512fc58
commit b719383943
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 63 additions and 68 deletions

View File

@ -101,21 +101,6 @@ 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.InternalServer(c, err)
return
}
helper.SuccessWithOutData(c)
}
// @Tags OpenResty
// @Summary Build OpenResty
// @Description 构建 OpenResty

View File

@ -1082,3 +1082,21 @@ func (b *BaseApi) ListCustomRewrite(c *gin.Context) {
}
helper.SuccessWithData(c, res)
}
// @Tags Website
// @Summary Clear Website proxy cache
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/proxy/clear [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清理 Openresty 代理缓存","formatEN":"Clear nginx proxy cache"}
func (b *BaseApi) ClearProxyCache(c *gin.Context) {
var req request.NginxCommonReq
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := websiteService.ClearProxyCache(req); err != nil {
helper.InternalServer(c, err)
return
}
helper.SuccessWithOutData(c)
}

View File

@ -35,7 +35,6 @@ type INginxService interface {
UpdateConfigByScope(req request.NginxConfigUpdate) error
GetStatus() (response.NginxStatus, error)
UpdateConfigFile(req request.NginxConfigFileUpdate) error
ClearProxyCache() error
Build(req request.NginxBuildReq) error
GetModules() (*response.NginxBuildConfig, error)
@ -166,25 +165,6 @@ 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
}
func (n NginxService) Build(req request.NginxBuildReq) error {
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if err != nil {

View File

@ -91,6 +91,7 @@ type IWebsiteService interface {
UpdateProxyFile(req request.NginxProxyUpdate) (err error)
UpdateProxyCache(req request.NginxProxyCacheUpdate) (err error)
GetProxyCache(id uint) (res response.NginxProxyCache, err error)
ClearProxyCache(req request.NginxCommonReq) error
GetAntiLeech(id uint) (*response.NginxAntiLeechRes, error)
UpdateAntiLeech(req request.NginxAntiLeechUpdate) (err error)
@ -1822,6 +1823,28 @@ func (w WebsiteService) UpdateProxyFile(req request.NginxProxyUpdate) (err error
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
}
func (w WebsiteService) ClearProxyCache(req request.NginxCommonReq) error {
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
if err != nil {
return err
}
cacheDir := GetSitePath(website, SiteProxyDir)
fileOp := files.NewFileOp()
if fileOp.Stat(cacheDir) {
if err = fileOp.CleanDir(cacheDir); err != nil {
return err
}
}
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return err
}
if err = opNginx(nginxInstall.ContainerName, constant.NginxReload); err != nil {
return err
}
return nil
}
func (w WebsiteService) GetAuthBasics(req request.NginxAuthReq) (res response.NginxAuthRes, err error) {
var (
website model.Website

View File

@ -18,7 +18,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)
groupRouter.POST("/build", baseApi.BuildNginx)
groupRouter.POST("/modules/update", baseApi.UpdateNginxModule)
groupRouter.GET("/modules", baseApi.GetNginxModules)

View File

@ -53,6 +53,7 @@ func (a *WebsiteRouter) InitRouter(Router *gin.RouterGroup) {
websiteRouter.POST("/proxies/file", baseApi.UpdateProxyConfigFile)
websiteRouter.POST("/proxy/config", baseApi.UpdateProxyCache)
websiteRouter.GET("/proxy/config/:id", baseApi.GetProxyCache)
websiteRouter.POST("/proxy/clear", baseApi.ClearProxyCache)
websiteRouter.POST("/auths", baseApi.GetAuthConfig)
websiteRouter.POST("/auths/update", baseApi.UpdateAuthConfig)

View File

@ -22,10 +22,6 @@ export const UpdateNginxConfigFile = (req: Nginx.NginxFileUpdate) => {
return http.post(`/openresty/file`, req);
};
export const ClearNginxCache = () => {
return http.post(`/openresty/clear`);
};
export const BuildNginx = (req: Nginx.NginxBuildReq) => {
return http.post(`/openresty/build`, req);
};

View File

@ -190,6 +190,10 @@ export const UpdateProxyConfigFile = (req: Website.ProxyFileUpdate) => {
return http.post<any>(`/websites/proxies/file`, req);
};
export const ClearProxtCache = (req: Website.WebsiteReq) => {
return http.post(`/websites/proxy/clear`, req);
};
export const GetAuthConfig = (req: Website.AuthReq) => {
return http.post<Website.AuthConfig>(`/websites/auths`, req);
};

View File

@ -51,18 +51,6 @@
>
{{ $t('commons.button.set') }}
</el-button>
<el-divider v-if="data.app === 'OpenResty'" direction="vertical" />
<el-button
v-if="data.app === 'OpenResty'"
type="primary"
@click="clear"
link
:disabled="
data.status === 'Installing' || (data.status !== 'Running' && data.app === 'OpenResty')
"
>
{{ $t('nginx.clearProxyCache') }}
</el-button>
</div>
<div class="ml-5" v-if="key === 'openresty' && (httpPort != 80 || httpsPort != 443)">
<el-tooltip
@ -91,7 +79,6 @@ 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: {
@ -146,16 +133,6 @@ const onCheck = async (key: any, name: any) => {
});
};
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

@ -2364,8 +2364,7 @@ const message = {
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? ',
clearProxyCacheWarn: 'This action will delete all files in the cache directory. Do you want to continue?',
create: 'Add a new module',
update: 'Edit a module',
params: 'Parameters',

View File

@ -2203,7 +2203,7 @@ const message = {
configResource: '配置修改',
saveAndReload: '保存並重載',
clearProxyCache: '清除反代快取',
clearProxyCacheWarn: '清除反代快取會影響所有配置快取的網站並且需要重新啟動 OpenResty 是否繼續 ',
clearProxyCacheWarn: '此操作將刪除緩存目錄下的所有文件是否繼續',
create: '新增模組',
update: '編輯模組',
params: '參數',

View File

@ -2202,7 +2202,7 @@ const message = {
configResource: '配置修改',
saveAndReload: '保存并重载',
clearProxyCache: '清除反代缓存',
clearProxyCacheWarn: '清除反代缓存会影响所有配置缓存的网站并且需要重启 OpenResty 是否继续',
clearProxyCacheWarn: '此操作将删除缓存目录下的所有文件, 是否继续',
create: '新增模块',
update: '编辑模块',
params: '参数',

View File

@ -3,6 +3,9 @@
<template #toolbar>
<el-button type="primary" plain @click="openCreate">{{ $t('commons.button.create') }}</el-button>
<el-button @click="openCache">{{ $t('website.proxyCache') }}</el-button>
<el-button type="primary" @click="clear" link>
{{ $t('nginx.clearProxyCache') }}
</el-button>
</template>
<el-table-column :label="$t('commons.table.name')" prop="name"></el-table-column>
<el-table-column :label="$t('website.proxyPath')" prop="match"></el-table-column>
@ -35,7 +38,7 @@
<script lang="ts" setup name="proxy">
import { Website } from '@/api/interface/website';
import { OperateProxyConfig, GetProxyConfig } from '@/api/modules/website';
import { OperateProxyConfig, GetProxyConfig, ClearProxtCache } from '@/api/modules/website';
import { computed, onMounted, ref } from 'vue';
import i18n from '@/lang';
import { MsgSuccess } from '@/utils/message';
@ -198,6 +201,16 @@ const search = 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 ClearProxtCache({ websiteID: id.value });
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
});
};
onMounted(() => {
search();
});