Skip to content

Commit 21d722e

Browse files
feat(website): Add Cache Clearing Functionality for Website Reverse Proxy
1 parent 18321d6 commit 21d722e

File tree

13 files changed

+63
-68
lines changed

13 files changed

+63
-68
lines changed

agent/app/api/v2/nginx.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,6 @@ func (b *BaseApi) UpdateNginxFile(c *gin.Context) {
101101
helper.SuccessWithData(c, nil)
102102
}
103103

104-
// @Tags OpenResty
105-
// @Summary Clear OpenResty proxy cache
106-
// @Description 清理 OpenResty 代理缓存
107-
// @Success 200
108-
// @Security ApiKeyAuth
109-
// @Router /openresty/clear [post]
110-
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清理 Openresty 代理缓存","formatEN":"Clear nginx proxy cache"}
111-
func (b *BaseApi) ClearNginxProxyCache(c *gin.Context) {
112-
if err := nginxService.ClearProxyCache(); err != nil {
113-
helper.InternalServer(c, err)
114-
return
115-
}
116-
helper.SuccessWithOutData(c)
117-
}
118-
119104
// @Tags OpenResty
120105
// @Summary Build OpenResty
121106
// @Description 构建 OpenResty

agent/app/api/v2/website.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,3 +1082,21 @@ func (b *BaseApi) ListCustomRewrite(c *gin.Context) {
10821082
}
10831083
helper.SuccessWithData(c, res)
10841084
}
1085+
1086+
// @Tags Website
1087+
// @Summary Clear Website proxy cache
1088+
// @Success 200
1089+
// @Security ApiKeyAuth
1090+
// @Router /websites/proxy/clear [post]
1091+
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清理 Openresty 代理缓存","formatEN":"Clear nginx proxy cache"}
1092+
func (b *BaseApi) ClearProxyCache(c *gin.Context) {
1093+
var req request.NginxCommonReq
1094+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
1095+
return
1096+
}
1097+
if err := websiteService.ClearProxyCache(req); err != nil {
1098+
helper.InternalServer(c, err)
1099+
return
1100+
}
1101+
helper.SuccessWithOutData(c)
1102+
}

agent/app/service/nginx.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ type INginxService interface {
3535
UpdateConfigByScope(req request.NginxConfigUpdate) error
3636
GetStatus() (response.NginxStatus, error)
3737
UpdateConfigFile(req request.NginxConfigFileUpdate) error
38-
ClearProxyCache() error
3938

4039
Build(req request.NginxBuildReq) error
4140
GetModules() (*response.NginxBuildConfig, error)
@@ -166,25 +165,6 @@ func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error
166165
return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName)
167166
}
168167

169-
func (n NginxService) ClearProxyCache() error {
170-
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
171-
if err != nil {
172-
return err
173-
}
174-
cacheDir := path.Join(nginxInstall.GetPath(), "www/common/proxy/proxy_cache_dir")
175-
fileOp := files.NewFileOp()
176-
if fileOp.Stat(cacheDir) {
177-
if err = fileOp.CleanDir(cacheDir); err != nil {
178-
return err
179-
}
180-
_, err = compose.Restart(nginxInstall.GetComposePath())
181-
if err != nil {
182-
return err
183-
}
184-
}
185-
return nil
186-
}
187-
188168
func (n NginxService) Build(req request.NginxBuildReq) error {
189169
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
190170
if err != nil {

agent/app/service/website.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ type IWebsiteService interface {
9191
UpdateProxyFile(req request.NginxProxyUpdate) (err error)
9292
UpdateProxyCache(req request.NginxProxyCacheUpdate) (err error)
9393
GetProxyCache(id uint) (res response.NginxProxyCache, err error)
94+
ClearProxyCache(req request.NginxCommonReq) error
9495

9596
GetAntiLeech(id uint) (*response.NginxAntiLeechRes, error)
9697
UpdateAntiLeech(req request.NginxAntiLeechUpdate) (err error)
@@ -1822,6 +1823,28 @@ func (w WebsiteService) UpdateProxyFile(req request.NginxProxyUpdate) (err error
18221823
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
18231824
}
18241825

1826+
func (w WebsiteService) ClearProxyCache(req request.NginxCommonReq) error {
1827+
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
1828+
if err != nil {
1829+
return err
1830+
}
1831+
cacheDir := GetSitePath(website, SiteProxyDir)
1832+
fileOp := files.NewFileOp()
1833+
if fileOp.Stat(cacheDir) {
1834+
if err = fileOp.CleanDir(cacheDir); err != nil {
1835+
return err
1836+
}
1837+
}
1838+
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
1839+
if err != nil {
1840+
return err
1841+
}
1842+
if err = opNginx(nginxInstall.ContainerName, constant.NginxReload); err != nil {
1843+
return err
1844+
}
1845+
return nil
1846+
}
1847+
18251848
func (w WebsiteService) GetAuthBasics(req request.NginxAuthReq) (res response.NginxAuthRes, err error) {
18261849
var (
18271850
website model.Website

agent/router/ro_nginx.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ func (a *NginxRouter) InitRouter(Router *gin.RouterGroup) {
1818
groupRouter.POST("/update", baseApi.UpdateNginxConfigByScope)
1919
groupRouter.GET("/status", baseApi.GetNginxStatus)
2020
groupRouter.POST("/file", baseApi.UpdateNginxFile)
21-
groupRouter.POST("/clear", baseApi.ClearNginxProxyCache)
2221
groupRouter.POST("/build", baseApi.BuildNginx)
2322
groupRouter.POST("/modules/update", baseApi.UpdateNginxModule)
2423
groupRouter.GET("/modules", baseApi.GetNginxModules)

agent/router/ro_website.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (a *WebsiteRouter) InitRouter(Router *gin.RouterGroup) {
5353
websiteRouter.POST("/proxies/file", baseApi.UpdateProxyConfigFile)
5454
websiteRouter.POST("/proxy/config", baseApi.UpdateProxyCache)
5555
websiteRouter.GET("/proxy/config/:id", baseApi.GetProxyCache)
56+
websiteRouter.POST("/proxy/clear", baseApi.ClearProxyCache)
5657

5758
websiteRouter.POST("/auths", baseApi.GetAuthConfig)
5859
websiteRouter.POST("/auths/update", baseApi.UpdateAuthConfig)

frontend/src/api/modules/nginx.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ export const UpdateNginxConfigFile = (req: Nginx.NginxFileUpdate) => {
2222
return http.post(`/openresty/file`, req);
2323
};
2424

25-
export const ClearNginxCache = () => {
26-
return http.post(`/openresty/clear`);
27-
};
28-
2925
export const BuildNginx = (req: Nginx.NginxBuildReq) => {
3026
return http.post(`/openresty/build`, req);
3127
};

frontend/src/api/modules/website.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ export const UpdateProxyConfigFile = (req: Website.ProxyFileUpdate) => {
190190
return http.post<any>(`/websites/proxies/file`, req);
191191
};
192192

193+
export const ClearProxtCache = (req: Website.WebsiteReq) => {
194+
return http.post(`/websites/proxy/clear`, req);
195+
};
196+
193197
export const GetAuthConfig = (req: Website.AuthReq) => {
194198
return http.post<Website.AuthConfig>(`/websites/auths`, req);
195199
};

frontend/src/components/app-status/index.vue

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@
5151
>
5252
{{ $t('commons.button.set') }}
5353
</el-button>
54-
<el-divider v-if="data.app === 'OpenResty'" direction="vertical" />
55-
<el-button
56-
v-if="data.app === 'OpenResty'"
57-
type="primary"
58-
@click="clear"
59-
link
60-
:disabled="
61-
data.status === 'Installing' || (data.status !== 'Running' && data.app === 'OpenResty')
62-
"
63-
>
64-
{{ $t('nginx.clearProxyCache') }}
65-
</el-button>
6654
</div>
6755
<div class="ml-5" v-if="key === 'openresty' && (httpPort != 80 || httpsPort != 443)">
6856
<el-tooltip
@@ -91,7 +79,6 @@ import Status from '@/components/status/index.vue';
9179
import { ElMessageBox } from 'element-plus';
9280
import i18n from '@/lang';
9381
import { MsgSuccess } from '@/utils/message';
94-
import { ClearNginxCache } from '@/api/modules/nginx';
9582
9683
const props = defineProps({
9784
appKey: {
@@ -146,16 +133,6 @@ const onCheck = async (key: any, name: any) => {
146133
});
147134
};
148135
149-
const clear = () => {
150-
ElMessageBox.confirm(i18n.global.t('nginx.clearProxyCacheWarn'), i18n.global.t('nginx.clearProxyCache'), {
151-
confirmButtonText: i18n.global.t('commons.button.confirm'),
152-
cancelButtonText: i18n.global.t('commons.button.cancel'),
153-
}).then(async () => {
154-
await ClearNginxCache();
155-
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
156-
});
157-
};
158-
159136
const onOperate = async (operation: string) => {
160137
em('update:maskShow', false);
161138
operateReq.operate = operation;

frontend/src/lang/modules/en.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,8 +2363,7 @@ const message = {
23632363
configResource: 'Configuration',
23642364
saveAndReload: 'Save and Reload',
23652365
clearProxyCache: 'Clear reverse proxy cache',
2366-
clearProxyCacheWarn:
2367-
'Clearing the reverse proxy cache will affect all websites configured with cache and requires restarting OpenResty. Do you want to continue? ',
2366+
clearProxyCacheWarn: 'This action will delete all files in the cache directory. Do you want to continue?',
23682367
create: 'Add a new module',
23692368
update: 'Edit a module',
23702369
params: 'Parameters',

0 commit comments

Comments
 (0)