diff --git a/backend/app/api/v1/website.go b/backend/app/api/v1/website.go index d24d4d02f..b7ff96bfe 100644 --- a/backend/app/api/v1/website.go +++ b/backend/app/api/v1/website.go @@ -304,3 +304,16 @@ func (b *BaseApi) UpdateWebsiteWafConfig(c *gin.Context) { } helper.SuccessWithData(c, nil) } + +func (b *BaseApi) UpdateWebsiteNginxConfig(c *gin.Context) { + var req request.WebsiteNginxUpdate + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + if err := websiteService.UpdateNginxConfigFile(req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, nil) +} diff --git a/backend/app/dto/request/website.go b/backend/app/dto/request/website.go index 98a637369..08014a39b 100644 --- a/backend/app/dto/request/website.go +++ b/backend/app/dto/request/website.go @@ -103,3 +103,8 @@ type WebsiteHTTPSOp struct { PrivateKey string `json:"privateKey"` Certificate string `json:"certificate"` } + +type WebsiteNginxUpdate struct { + ID uint `json:"id" validate:"required"` + Content string `json:"content" validate:"required"` +} diff --git a/backend/app/service/website.go b/backend/app/service/website.go index c9d64ac18..fcccf4ed5 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -653,7 +653,7 @@ func (w WebsiteService) GetWafConfig(req request.WebsiteWafReq) (response.Websit func (w WebsiteService) UpdateWafConfig(req request.WebsiteWafUpdate) error { website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID)) if err != nil { - return nil + return err } updateValue := "on" if !req.Enable { @@ -663,3 +663,19 @@ func (w WebsiteService) UpdateWafConfig(req request.WebsiteWafUpdate) error { {Name: "set", Params: []string{req.Key, updateValue}}, }, &website) } + +func (w WebsiteService) UpdateNginxConfigFile(req request.WebsiteNginxUpdate) error { + website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID)) + if err != nil { + return err + } + nginxFull, err := getNginxFull(&website) + if err != nil { + return err + } + filePath := nginxFull.SiteConfig.FilePath + if err := files.NewFileOp().WriteFile(filePath, strings.NewReader(req.Content), 0755); err != nil { + return err + } + return nginxCheckAndReload(nginxFull.SiteConfig.OldContent, filePath, nginxFull.Install.ContainerName) +} diff --git a/backend/router/ro_website.go b/backend/router/ro_website.go index 8e9933e2c..9130596e0 100644 --- a/backend/router/ro_website.go +++ b/backend/router/ro_website.go @@ -38,5 +38,6 @@ func (a *WebsiteRouter) InitWebsiteRouter(Router *gin.RouterGroup) { groupRouter.POST("/:id/https", baseApi.UpdateHTTPSConfig) groupRouter.POST("/waf/config", baseApi.GetWebsiteWafConfig) groupRouter.POST("/waf/update", baseApi.UpdateWebsiteWafConfig) + groupRouter.POST("/nginx/update", baseApi.UpdateWebsiteNginxConfig) } } diff --git a/frontend/src/api/interface/website.ts b/frontend/src/api/interface/website.ts index ee570dd08..f55c1b0bd 100644 --- a/frontend/src/api/interface/website.ts +++ b/frontend/src/api/interface/website.ts @@ -247,4 +247,9 @@ export namespace Website { export interface BackupReq { id: number; } + + export interface NginxUpdate { + id: number; + content: string; + } } diff --git a/frontend/src/api/modules/website.ts b/frontend/src/api/modules/website.ts index 8f3c28d3d..7a8218d09 100644 --- a/frontend/src/api/modules/website.ts +++ b/frontend/src/api/modules/website.ts @@ -162,3 +162,7 @@ export const GetWafConfig = (req: Website.WafReq) => { export const UpdateWafEnable = (req: Website.WafUpdate) => { return http.post(`/websites/waf/update`, req); }; + +export const UpdateNginxFile = (req: Website.NginxUpdate) => { + return http.post(`/websites/nginx/update`, req); +}; diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index cc89d6c06..bdb7c879e 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -939,6 +939,7 @@ export default { status: '当前状态', nginxConfig: 'OpenResty 设置', configResource: '配置修改', + saveAndReload: '保存并重载', }, ssl: { provider: '类型', diff --git a/frontend/src/views/website/website/config/resource/nginx/index.vue b/frontend/src/views/website/website/config/resource/nginx/index.vue index 59659e205..8730ff049 100644 --- a/frontend/src/views/website/website/config/resource/nginx/index.vue +++ b/frontend/src/views/website/website/config/resource/nginx/index.vue @@ -16,7 +16,7 @@ />
- {{ $t('commons.button.save') }} + {{ $t('nginx.saveAndReload') }}
@@ -24,10 +24,9 @@