From 28de822917fa94a55065eaf36fef99fa46f5b177 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Fri, 30 Dec 2022 17:39:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BD=91=E7=AB=99=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/website.go | 14 ++++ backend/app/dto/request/website.go | 6 ++ backend/app/dto/response/website.go | 5 ++ backend/app/model/website.go | 2 + backend/app/service/nginx_utils.go | 1 - backend/app/service/website.go | 70 +++++++++++++++++++ backend/constant/website.go | 7 ++ backend/router/ro_website.go | 1 + frontend/src/api/interface/website.ts | 11 +++ frontend/src/api/modules/website.ts | 4 ++ frontend/src/lang/modules/zh.ts | 1 + .../website/website/config/log/index.vue | 27 ++----- .../website/config/log/log-fiile/index.vue | 55 ++++++++++++--- 13 files changed, 170 insertions(+), 34 deletions(-) diff --git a/backend/app/api/v1/website.go b/backend/app/api/v1/website.go index 771d493b9..d0fc01d42 100644 --- a/backend/app/api/v1/website.go +++ b/backend/app/api/v1/website.go @@ -320,3 +320,17 @@ func (b *BaseApi) UpdateWebsiteNginxConfig(c *gin.Context) { } helper.SuccessWithData(c, nil) } + +func (b *BaseApi) OpWebsiteLog(c *gin.Context) { + var req request.WebsiteLogReq + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + res, err := websiteService.OpWebsiteLog(req) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, res) +} diff --git a/backend/app/dto/request/website.go b/backend/app/dto/request/website.go index a546bb7a6..b2448450e 100644 --- a/backend/app/dto/request/website.go +++ b/backend/app/dto/request/website.go @@ -116,3 +116,9 @@ type WebsiteNginxUpdate struct { ID uint `json:"id" validate:"required"` Content string `json:"content" validate:"required"` } + +type WebsiteLogReq struct { + ID uint `json:"id" validate:"required"` + Operate string `json:"operate" validate:"required"` + LogType string `json:"logType" validate:"required"` +} diff --git a/backend/app/dto/response/website.go b/backend/app/dto/response/website.go index a56ac3fc1..a77ee781c 100644 --- a/backend/app/dto/response/website.go +++ b/backend/app/dto/response/website.go @@ -36,3 +36,8 @@ type WebsiteHTTPS struct { SSLProtocol []string `json:"SSLProtocol"` Algorithm string `json:"algorithm"` } + +type WebsiteLog struct { + Enable bool `json:"enable"` + Content string `json:"content"` +} diff --git a/backend/app/model/website.go b/backend/app/model/website.go index 449278d69..a948d8fd4 100644 --- a/backend/app/model/website.go +++ b/backend/app/model/website.go @@ -16,6 +16,8 @@ type Website struct { WebsiteGroupID uint `gorm:"type:integer" json:"webSiteGroupId"` WebsiteSSLID uint `gorm:"type:integer" json:"webSiteSSLId"` Proxy string `gorm:"type:varchar(128);not null" json:"proxy"` + ErrorLog bool `json:"errorLog"` + AccessLog bool `json:"accessLog"` Domains []WebsiteDomain `json:"domains"` WebsiteSSL WebsiteSSL `json:"webSiteSSL"` } diff --git a/backend/app/service/nginx_utils.go b/backend/app/service/nginx_utils.go index 10d65e76e..e3306c523 100644 --- a/backend/app/service/nginx_utils.go +++ b/backend/app/service/nginx_utils.go @@ -96,7 +96,6 @@ func getNginxParamsByKeys(scope string, keys []string, website *model.Website) ( } func updateNginxConfig(scope string, params []dto.NginxParam, website *model.Website) error { - nginxFull, err := getNginxFull(website) if err != nil { return err diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 5313949dd..e9ac19336 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -49,6 +49,8 @@ type IWebsiteService interface { PreInstallCheck(req request.WebsiteInstallCheckReq) ([]response.WebsitePreInstallCheck, error) GetWafConfig(req request.WebsiteWafReq) (response.WebsiteWafConfig, error) UpdateWafConfig(req request.WebsiteWafUpdate) error + UpdateNginxConfigFile(req request.WebsiteNginxUpdate) error + OpWebsiteLog(req request.WebsiteLogReq) (*response.WebsiteLog, error) } func NewWebsiteService() IWebsiteService { @@ -707,3 +709,71 @@ func (w WebsiteService) UpdateNginxConfigFile(req request.WebsiteNginxUpdate) er } return nginxCheckAndReload(nginxFull.SiteConfig.OldContent, filePath, nginxFull.Install.ContainerName) } + +func (w WebsiteService) OpWebsiteLog(req request.WebsiteLogReq) (*response.WebsiteLog, error) { + website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID)) + if err != nil { + return nil, err + } + nginx, err := getNginxFull(&website) + if err != nil { + return nil, err + } + sitePath := path.Join(nginx.SiteDir, "sites", website.Alias) + res := &response.WebsiteLog{ + Content: "", + } + switch req.Operate { + case constant.GetLog: + switch req.LogType { + case constant.AccessLog: + res.Enable = website.AccessLog + if !website.AccessLog { + return res, nil + } + case constant.ErrorLog: + res.Enable = website.ErrorLog + if !website.ErrorLog { + return res, nil + } + } + content, err := os.ReadFile(path.Join(sitePath, "log", req.LogType)) + if err != nil { + return nil, err + } + res.Content = string(content) + return res, nil + case constant.DisableLog: + key := "access_log" + switch req.LogType { + case constant.AccessLog: + website.AccessLog = false + case constant.ErrorLog: + key = "error_log" + website.ErrorLog = false + } + if err := deleteNginxConfig(constant.NginxScopeServer, []string{key}, &website); err != nil { + return nil, err + } + if err := websiteRepo.Save(context.Background(), &website); err != nil { + return nil, err + } + case constant.EnableLog: + key := "access_log" + logPath := path.Join("/www", "sites", website.Alias, "log", req.LogType) + switch req.LogType { + case constant.AccessLog: + website.AccessLog = true + case constant.ErrorLog: + key = "error_log" + website.ErrorLog = true + } + if err := updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: key, Params: []string{logPath}}}, &website); err != nil { + return nil, err + } + if err := websiteRepo.Save(context.Background(), &website); err != nil { + return nil, err + } + } + return res, nil +} diff --git a/backend/constant/website.go b/backend/constant/website.go index d8282f7b2..ea400ce96 100644 --- a/backend/constant/website.go +++ b/backend/constant/website.go @@ -32,4 +32,11 @@ const ( HTTPSOnly = "HTTPSOnly" HTTPAlso = "HTTPAlso" HTTPToHTTPS = "HTTPToHTTPS" + + GetLog = "get" + DisableLog = "disable" + EnableLog = "enable" + + AccessLog = "access.log" + ErrorLog = "error.log" ) diff --git a/backend/router/ro_website.go b/backend/router/ro_website.go index 9130596e0..82d001b46 100644 --- a/backend/router/ro_website.go +++ b/backend/router/ro_website.go @@ -18,6 +18,7 @@ func (a *WebsiteRouter) InitWebsiteRouter(Router *gin.RouterGroup) { groupRouter.POST("/search", baseApi.PageWebsite) groupRouter.POST("", baseApi.CreateWebsite) groupRouter.POST("/operate", baseApi.OpWebsite) + groupRouter.POST("/log", baseApi.OpWebsiteLog) groupRouter.POST("/check", baseApi.CreateWebsiteCheck) groupRouter.GET("/options", baseApi.GetWebsiteOptions) groupRouter.POST("/update", baseApi.UpdateWebsite) diff --git a/frontend/src/api/interface/website.ts b/frontend/src/api/interface/website.ts index fb2059c5a..6fe1f70fe 100644 --- a/frontend/src/api/interface/website.ts +++ b/frontend/src/api/interface/website.ts @@ -76,6 +76,17 @@ export namespace Website { operate: string; } + export interface WebSiteOpLog { + id: number; + operate: string; + logType: string; + } + + export interface WebSiteLog { + enable: boolean; + content: string; + } + export interface Group extends CommonModel { name: string; default: boolean; diff --git a/frontend/src/api/modules/website.ts b/frontend/src/api/modules/website.ts index 501c514aa..17c9cb1d8 100644 --- a/frontend/src/api/modules/website.ts +++ b/frontend/src/api/modules/website.ts @@ -15,6 +15,10 @@ export const OpWebsite = (req: Website.WebSiteOp) => { return http.post(`/websites/operate`, req); }; +export const OpWebsiteLog = (req: Website.WebSiteOpLog) => { + return http.post(`/websites/log`, req); +}; + export const BackupWebsite = (req: Website.BackupReq) => { return http.post(`/websites/backup`, req); }; diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index 38e2dfbe0..591acfc76 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -952,6 +952,7 @@ export default { ever: '永久', nextYear: '一年后', allGroup: '所有分组', + noLog: '当前没有日志...', }, nginx: { serverNamesHashBucketSizeHelper: '服务器名字的hash表大小', diff --git a/frontend/src/views/website/website/config/log/index.vue b/frontend/src/views/website/website/config/log/index.vue index 6e99f3a00..4b5b0ec4b 100644 --- a/frontend/src/views/website/website/config/log/index.vue +++ b/frontend/src/views/website/website/config/log/index.vue @@ -1,17 +1,16 @@ diff --git a/frontend/src/views/website/website/config/log/log-fiile/index.vue b/frontend/src/views/website/website/config/log/log-fiile/index.vue index a1f6254d4..e3b5f4561 100644 --- a/frontend/src/views/website/website/config/log/log-fiile/index.vue +++ b/frontend/src/views/website/website/config/log/log-fiile/index.vue @@ -1,46 +1,81 @@