From f85d17cf65e11cfbaae5c790ff737ae68d01b731 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Tue, 13 Dec 2022 18:54:46 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E4=BF=AE=E6=94=B9nginx=E7=9A=84API?= =?UTF-8?q?=E5=92=8Cdto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/nginx.go | 8 ++-- backend/app/api/v1/website.go | 4 +- backend/app/dto/nginx.go | 43 +++---------------- backend/app/dto/request/nginx.go | 14 ++++++ backend/app/dto/response/nginx.go | 16 +++++++ backend/app/dto/response/website.go | 5 +-- backend/app/service/nginx.go | 13 +++--- backend/app/service/nginx_utils.go | 9 ++-- backend/app/service/website.go | 12 +++--- backend/app/service/website_utils.go | 7 ++- backend/constant/nginx.go | 6 ++- backend/router/ro_nginx.go | 2 +- frontend/src/api/interface/website.ts | 5 +++ frontend/src/api/modules/website.ts | 2 +- .../website/config/basic/limit-conn/index.vue | 11 +++-- 15 files changed, 86 insertions(+), 71 deletions(-) create mode 100644 backend/app/dto/response/nginx.go diff --git a/backend/app/api/v1/nginx.go b/backend/app/api/v1/nginx.go index 954473841..5a4b9d9dc 100644 --- a/backend/app/api/v1/nginx.go +++ b/backend/app/api/v1/nginx.go @@ -2,7 +2,6 @@ package v1 import ( "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" - "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/dto/request" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/gin-gonic/gin" @@ -18,7 +17,7 @@ func (b *BaseApi) GetNginx(c *gin.Context) { } func (b *BaseApi) GetNginxConfigByScope(c *gin.Context) { - var req dto.NginxScopeReq + var req request.NginxScopeReq if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -32,13 +31,12 @@ func (b *BaseApi) GetNginxConfigByScope(c *gin.Context) { helper.SuccessWithData(c, params) } -func (b *BaseApi) UpdateNginxConfigBy(c *gin.Context) { - var req dto.NginxConfigReq +func (b *BaseApi) UpdateNginxConfigByScope(c *gin.Context) { + var req request.NginxConfigUpdate if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - if err := nginxService.UpdateConfigByScope(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return diff --git a/backend/app/api/v1/website.go b/backend/app/api/v1/website.go index 7895c7848..6c9ee71dd 100644 --- a/backend/app/api/v1/website.go +++ b/backend/app/api/v1/website.go @@ -197,7 +197,7 @@ func (b *BaseApi) CreateWebDomain(c *gin.Context) { } func (b *BaseApi) GetNginxConfig(c *gin.Context) { - var req dto.NginxConfigReq + var req request.NginxScopeReq if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -211,7 +211,7 @@ func (b *BaseApi) GetNginxConfig(c *gin.Context) { } func (b *BaseApi) UpdateNginxConfig(c *gin.Context) { - var req dto.NginxConfigReq + var req request.NginxConfigUpdate if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return diff --git a/backend/app/dto/nginx.go b/backend/app/dto/nginx.go index 2a7907779..b6b7b6730 100644 --- a/backend/app/dto/nginx.go +++ b/backend/app/dto/nginx.go @@ -17,30 +17,15 @@ type NginxFull struct { } type NginxConfig struct { - FilePath string `json:"filePath"` - Config *components.Config `json:"config"` - OldContent string `json:"oldContent"` + FilePath string + Config *components.Config + OldContent string } -type NginxConfigReq struct { - Scope NginxKey `json:"scope"` - Operate NginxOp `json:"operate"` - WebsiteID uint `json:"webSiteId"` - Params interface{} `json:"params"` -} - -type NginxScopeReq struct { - Scope NginxKey `json:"scope"` -} - -type NginxStatus struct { - Active string `json:"active"` - Accepts string `json:"accepts"` - Handled string `json:"handled"` - Requests string `json:"requests"` - Reading string `json:"reading"` - Writing string `json:"writing"` - Waiting string `json:"waiting"` +type NginxParam struct { + UpdateScope string + Name string + Params []string } type NginxKey string @@ -52,14 +37,6 @@ const ( HttpPer NginxKey = "http-per" ) -type NginxOp string - -const ( - ConfigNew NginxOp = "add" - ConfigUpdate NginxOp = "update" - ConfigDel NginxOp = "delete" -) - var ScopeKeyMap = map[NginxKey][]string{ Index: {"index"}, LimitConn: {"limit_conn", "limit_rate", "limit_conn_zone"}, @@ -72,9 +49,3 @@ var StaticFileKeyMap = map[NginxKey]struct { SSL: {}, LimitConn: {}, } - -type NginxParam struct { - UpdateScope string `json:"scope"` - Name string `json:"name"` - Params []string `json:"params"` -} diff --git a/backend/app/dto/request/nginx.go b/backend/app/dto/request/nginx.go index 7a216cb4e..7e2b0f18a 100644 --- a/backend/app/dto/request/nginx.go +++ b/backend/app/dto/request/nginx.go @@ -1,7 +1,21 @@ package request +import "github.com/1Panel-dev/1Panel/backend/app/dto" + type NginxConfigFileUpdate struct { Content string `json:"content" validate:"required"` FilePath string `json:"filePath" validate:"required"` Backup bool `json:"backup" validate:"required"` } + +type NginxScopeReq struct { + Scope dto.NginxKey `json:"scope" validate:"required"` + WebsiteID uint `json:"websiteId"` +} + +type NginxConfigUpdate struct { + Scope dto.NginxKey `json:"scope"` + Operate string `json:"operate" validate:"required;oneof=add update delete"` + WebsiteID uint `json:"websiteId" validate:"required"` + Params interface{} `json:"params"` +} diff --git a/backend/app/dto/response/nginx.go b/backend/app/dto/response/nginx.go new file mode 100644 index 000000000..a92736e4e --- /dev/null +++ b/backend/app/dto/response/nginx.go @@ -0,0 +1,16 @@ +package response + +type NginxStatus struct { + Active string `json:"active"` + Accepts string `json:"accepts"` + Handled string `json:"handled"` + Requests string `json:"requests"` + Reading string `json:"reading"` + Writing string `json:"writing"` + Waiting string `json:"waiting"` +} + +type NginxParam struct { + Name string `json:"name"` + Params []string `json:"params"` +} diff --git a/backend/app/dto/response/website.go b/backend/app/dto/response/website.go index 6206a86ef..240164809 100644 --- a/backend/app/dto/response/website.go +++ b/backend/app/dto/response/website.go @@ -1,7 +1,6 @@ package response import ( - "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/model" ) @@ -17,8 +16,8 @@ type WebsitePreInstallCheck struct { } type WebsiteNginxConfig struct { - Enable bool `json:"enable"` - Params []dto.NginxParam `json:"params"` + Enable bool `json:"enable"` + Params []NginxParam `json:"params"` } type WebsiteWafConfig struct { diff --git a/backend/app/service/nginx.go b/backend/app/service/nginx.go index f6d0bd7df..f03a0cc3b 100644 --- a/backend/app/service/nginx.go +++ b/backend/app/service/nginx.go @@ -2,6 +2,7 @@ package service import ( "github.com/1Panel-dev/1Panel/backend/app/dto/request" + "github.com/1Panel-dev/1Panel/backend/app/dto/response" "io/ioutil" "net/http" "os" @@ -33,7 +34,7 @@ func (n NginxService) GetNginxConfig() (dto.FileInfo, error) { return dto.FileInfo{FileInfo: *info}, nil } -func (n NginxService) GetConfigByScope(req dto.NginxScopeReq) ([]dto.NginxParam, error) { +func (n NginxService) GetConfigByScope(req request.NginxScopeReq) ([]response.NginxParam, error) { keys, ok := dto.ScopeKeyMap[req.Scope] if !ok || len(keys) == 0 { return nil, nil @@ -41,7 +42,7 @@ func (n NginxService) GetConfigByScope(req dto.NginxScopeReq) ([]dto.NginxParam, return getNginxParamsByKeys(constant.NginxScopeHttp, keys, nil) } -func (n NginxService) UpdateConfigByScope(req dto.NginxConfigReq) error { +func (n NginxService) UpdateConfigByScope(req request.NginxConfigUpdate) error { keys, ok := dto.ScopeKeyMap[req.Scope] if !ok || len(keys) == 0 { return nil @@ -49,16 +50,16 @@ func (n NginxService) UpdateConfigByScope(req dto.NginxConfigReq) error { return updateNginxConfig(constant.NginxScopeHttp, getNginxParams(req.Params, keys), nil) } -func (n NginxService) GetStatus() (dto.NginxStatus, error) { +func (n NginxService) GetStatus() (response.NginxStatus, error) { res, err := http.Get("http://127.0.0.1/nginx_status") if err != nil { - return dto.NginxStatus{}, err + return response.NginxStatus{}, err } content, err := ioutil.ReadAll(res.Body) if err != nil { - return dto.NginxStatus{}, err + return response.NginxStatus{}, err } - var status dto.NginxStatus + var status response.NginxStatus resArray := strings.Split(string(content), " ") status.Active = resArray[2] status.Accepts = resArray[7] diff --git a/backend/app/service/nginx_utils.go b/backend/app/service/nginx_utils.go index d7bd31f62..c54cc9b05 100644 --- a/backend/app/service/nginx_utils.go +++ b/backend/app/service/nginx_utils.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/1Panel-dev/1Panel/backend/app/dto" + "github.com/1Panel-dev/1Panel/backend/app/dto/response" "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/utils/cmd" @@ -62,12 +63,12 @@ func getNginxFull(website *model.Website) (dto.NginxFull, error) { return nginxFull, nil } -func getNginxParamsByKeys(scope string, keys []string, website *model.Website) ([]dto.NginxParam, error) { +func getNginxParamsByKeys(scope string, keys []string, website *model.Website) ([]response.NginxParam, error) { nginxFull, err := getNginxFull(website) if err != nil { return nil, err } - var res []dto.NginxParam + var res []response.NginxParam var block components.IBlock if scope == constant.NginxScopeHttp { block = nginxFull.RootConfig.Config.FindHttp() @@ -77,14 +78,14 @@ func getNginxParamsByKeys(scope string, keys []string, website *model.Website) ( for _, key := range keys { dirs := block.FindDirectives(key) for _, dir := range dirs { - nginxParam := dto.NginxParam{ + nginxParam := response.NginxParam{ Name: dir.GetName(), Params: dir.GetParameters(), } res = append(res, nginxParam) } if len(dirs) == 0 { - nginxParam := dto.NginxParam{ + nginxParam := response.NginxParam{ Name: key, Params: []string{}, } diff --git a/backend/app/service/website.go b/backend/app/service/website.go index a0386a294..dcff30c9a 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -39,8 +39,8 @@ type IWebsiteService interface { CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error) GetWebsiteDomain(websiteId uint) ([]model.WebsiteDomain, error) DeleteWebsiteDomain(domainId uint) error - GetNginxConfigByScope(req dto.NginxConfigReq) (*response.WebsiteNginxConfig, error) - UpdateNginxConfigByScope(req dto.NginxConfigReq) error + GetNginxConfigByScope(req request.NginxScopeReq) (*response.WebsiteNginxConfig, error) + UpdateNginxConfigByScope(req request.NginxConfigUpdate) error GetWebsiteNginxConfig(websiteId uint) (dto.FileInfo, error) GetWebsiteHTTPS(websiteId uint) (response.WebsiteHTTPS, error) OpWebsiteHTTPS(req request.WebsiteHTTPSOp) (response.WebsiteHTTPS, error) @@ -340,7 +340,7 @@ func (w WebsiteService) DeleteWebsiteDomain(domainId uint) error { return websiteDomainRepo.DeleteBy(context.TODO(), commonRepo.WithByID(domainId)) } -func (w WebsiteService) GetNginxConfigByScope(req dto.NginxConfigReq) (*response.WebsiteNginxConfig, error) { +func (w WebsiteService) GetNginxConfigByScope(req request.NginxScopeReq) (*response.WebsiteNginxConfig, error) { keys, ok := dto.ScopeKeyMap[req.Scope] if !ok || len(keys) == 0 { return nil, nil @@ -361,7 +361,7 @@ func (w WebsiteService) GetNginxConfigByScope(req dto.NginxConfigReq) (*response return &config, nil } -func (w WebsiteService) UpdateNginxConfigByScope(req dto.NginxConfigReq) error { +func (w WebsiteService) UpdateNginxConfigByScope(req request.NginxConfigUpdate) error { keys, ok := dto.ScopeKeyMap[req.Scope] if !ok || len(keys) == 0 { return nil @@ -370,11 +370,11 @@ func (w WebsiteService) UpdateNginxConfigByScope(req dto.NginxConfigReq) error { if err != nil { return err } - if req.Operate == dto.ConfigDel { + if req.Operate == constant.ConfigDel { return deleteNginxConfig(constant.NginxScopeServer, keys, &website) } params := getNginxParams(req.Params, keys) - if req.Operate == dto.ConfigNew { + if req.Operate == constant.ConfigNew { if _, ok := dto.StaticFileKeyMap[req.Scope]; ok { params = getNginxParamsFromStaticFile(req.Scope, params) } diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go index edc977882..7b096199b 100644 --- a/backend/app/service/website_utils.go +++ b/backend/app/service/website_utils.go @@ -178,7 +178,12 @@ func delNginxConfig(website model.Website, force bool) error { if err := fileOp.DeleteFile(configPath); err != nil { return err } - if err := opNginx(nginxInstall.ContainerName, "reload"); err != nil { + sitePath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "www", "sites", website.PrimaryDomain) + if fileOp.Stat(sitePath) { + _ = fileOp.DeleteDir(sitePath) + } + + if err := opNginx(nginxInstall.ContainerName, constant.NginxReload); err != nil { if force { return nil } diff --git a/backend/constant/nginx.go b/backend/constant/nginx.go index 68604d14d..996edd307 100644 --- a/backend/constant/nginx.go +++ b/backend/constant/nginx.go @@ -4,9 +4,11 @@ const ( NginxScopeServer = "server" NginxScopeHttp = "http" NginxScopeOut = "out" -) -const ( NginxReload = "reload" NginxCheck = "check" + + ConfigNew = "add" + ConfigUpdate = "update" + ConfigDel = "delete" ) diff --git a/backend/router/ro_nginx.go b/backend/router/ro_nginx.go index 90e85a859..48d3e6624 100644 --- a/backend/router/ro_nginx.go +++ b/backend/router/ro_nginx.go @@ -17,7 +17,7 @@ func (a *NginxRouter) InitNginxRouter(Router *gin.RouterGroup) { { groupRouter.GET("", baseApi.GetNginx) groupRouter.POST("/scope", baseApi.GetNginxConfigByScope) - groupRouter.POST("/update", baseApi.UpdateNginxConfigBy) + groupRouter.POST("/update", baseApi.UpdateNginxConfigByScope) groupRouter.GET("/status", baseApi.GetNginxStatus) groupRouter.POST("/file", baseApi.UpdateNginxFile) } diff --git a/frontend/src/api/interface/website.ts b/frontend/src/api/interface/website.ts index 16e717a05..486339e90 100644 --- a/frontend/src/api/interface/website.ts +++ b/frontend/src/api/interface/website.ts @@ -98,6 +98,11 @@ export namespace Website { params?: any; } + export interface NginxScopeReq { + websiteId: number; + scope: string; + } + export interface NginxParam { name: string; params: string[]; diff --git a/frontend/src/api/modules/website.ts b/frontend/src/api/modules/website.ts index db4c01a98..224783c60 100644 --- a/frontend/src/api/modules/website.ts +++ b/frontend/src/api/modules/website.ts @@ -71,7 +71,7 @@ export const CreateDomain = (req: Website.DomainCreate) => { return http.post(`/websites/domains`, req); }; -export const GetNginxConfig = (req: Website.NginxConfigReq) => { +export const GetNginxConfig = (req: Website.NginxScopeReq) => { return http.post(`/websites/config`, req); }; diff --git a/frontend/src/views/website/website/config/basic/limit-conn/index.vue b/frontend/src/views/website/website/config/basic/limit-conn/index.vue index bedcc19a6..3dd4e4e4b 100644 --- a/frontend/src/views/website/website/config/basic/limit-conn/index.vue +++ b/frontend/src/views/website/website/config/basic/limit-conn/index.vue @@ -57,12 +57,16 @@ let req = reactive({ websiteId: websiteId.value, params: [{}], }); +let scopeReq = reactive({ + scope: 'limit-conn', + websiteId: websiteId.value, +}); let enable = ref(false); let loading = ref(false); -const search = (req: Website.NginxConfigReq) => { +const search = (scopeReq: Website.NginxScopeReq) => { loading.value = true; - GetNginxConfig(req) + GetNginxConfig(scopeReq) .then((res) => { if (res.data) { enable.value = res.data.enable; @@ -125,10 +129,9 @@ const changeEnable = () => { } else { req.operate = 'add'; } - // submit(limitForm.value); }; onMounted(() => { - search(req); + search(scopeReq); });