feat: split website log APIs (#12761)

This commit is contained in:
ssongliu
2026-05-21 12:39:49 +08:00
committed by zhengkunwang223
parent d0faee4eeb
commit 956bf0992a
6 changed files with 83 additions and 40 deletions
+26 -6
View File
@@ -330,25 +330,45 @@ func (b *BaseApi) UpdateWebsiteNginxConfig(c *gin.Context) {
}
// @Tags Website
// @Summary Operate website log
// @Summary Get website log
// @Accept json
// @Param request body request.WebsiteLogReq true "request"
// @Param request body request.WebsiteLogSearchReq true "request"
// @Success 200 {object} response.WebsiteLog
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /websites/log [post]
// @Router /websites/log/search [post]
func (b *BaseApi) GetWebsiteLog(c *gin.Context) {
var req request.WebsiteLogSearchReq
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
res, err := websiteService.GetWebsiteLog(req)
if err != nil {
helper.InternalServer(c, err)
return
}
helper.SuccessWithData(c, res)
}
// @Tags Website
// @Summary Operate website log
// @Accept json
// @Param request body request.WebsiteLogReq true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /websites/log/operate [post]
// @x-panel-log {"bodyKeys":["id", "operate"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"websites","output_column":"primary_domain","output_value":"domain"}],"formatZH":"[domain][operate] 日志","formatEN":"[domain][operate] logs"}
func (b *BaseApi) OpWebsiteLog(c *gin.Context) {
var req request.WebsiteLogReq
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
res, err := websiteService.OpWebsiteLog(req)
if err != nil {
if err := websiteService.OpWebsiteLog(req); err != nil {
helper.InternalServer(c, err)
return
}
helper.SuccessWithData(c, res)
helper.Success(c)
}
// @Tags Website
+7 -2
View File
@@ -223,9 +223,14 @@ type WebsiteNginxUpdate struct {
}
type WebsiteLogReq struct {
ID uint `json:"id" validate:"required"`
Operate string `json:"operate" validate:"required,oneof=enable disable delete"`
LogType string `json:"logType" validate:"required,oneof=access.log error.log"`
}
type WebsiteLogSearchReq struct {
ID uint `json:"id" validate:"required"`
Operate string `json:"operate" validate:"required"`
LogType string `json:"logType" validate:"required"`
LogType string `json:"logType" validate:"required,oneof=access.log error.log"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
+38 -30
View File
@@ -85,7 +85,8 @@ type IWebsiteService interface {
ChangeGroup(group, newGroup uint) error
ChangeDefaultServer(id uint) error
PreInstallCheck(req request.WebsiteInstallCheckReq) ([]response.WebsitePreInstallCheck, error)
OpWebsiteLog(req request.WebsiteLogReq) (*response.WebsiteLog, error)
GetWebsiteLog(req request.WebsiteLogSearchReq) (*response.WebsiteLog, error)
OpWebsiteLog(req request.WebsiteLogReq) error
UpdateStream(req request.StreamUpdate) error
GetNginxConfigByScope(req request.NginxScopeReq) (*response.WebsiteNginxConfig, error)
@@ -1124,7 +1125,7 @@ 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) {
func (w WebsiteService) GetWebsiteLog(req request.WebsiteLogSearchReq) (*response.WebsiteLog, error) {
website, err := websiteRepo.GetFirst(repo.WithByID(req.ID))
if err != nil {
return nil, err
@@ -1133,29 +1134,36 @@ func (w WebsiteService) OpWebsiteLog(req request.WebsiteLogReq) (*response.Websi
res := &response.WebsiteLog{
Content: "",
}
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
}
}
filePath := path.Join(sitePath, "log", req.LogType)
logFileRes, err := files.ReadFileByLine(filePath, req.Page, req.PageSize, false)
if err != nil {
return nil, err
}
res.End = logFileRes.IsEndOfFile
res.Path = filePath
res.Content = strings.Join(logFileRes.Lines, "\n")
return res, nil
}
func (w WebsiteService) OpWebsiteLog(req request.WebsiteLogReq) error {
website, err := websiteRepo.GetFirst(repo.WithByID(req.ID))
if err != nil {
return err
}
sitePath := GetSitePath(website, SiteDir)
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
}
}
filePath := path.Join(sitePath, "log", req.LogType)
logFileRes, err := files.ReadFileByLine(filePath, req.Page, req.PageSize, false)
if err != nil {
return nil, err
}
res.End = logFileRes.IsEndOfFile
res.Path = filePath
res.Content = strings.Join(logFileRes.Lines, "\n")
return res, nil
case constant.DisableLog:
params := dto.NginxParam{}
switch req.LogType {
@@ -1172,10 +1180,10 @@ func (w WebsiteService) OpWebsiteLog(req request.WebsiteLogReq) (*response.Websi
nginxParams = append(nginxParams, params)
if err := updateNginxConfig(constant.NginxScopeServer, nginxParams, &website); err != nil {
return nil, err
return err
}
if err := websiteRepo.Save(context.Background(), &website); err != nil {
return nil, err
return err
}
case constant.EnableLog:
key := "access_log"
@@ -1194,18 +1202,18 @@ func (w WebsiteService) OpWebsiteLog(req request.WebsiteLogReq) (*response.Websi
website.ErrorLog = true
}
if err := updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: key, Params: params}}, &website); err != nil {
return nil, err
return err
}
if err := websiteRepo.Save(context.Background(), &website); err != nil {
return nil, err
return err
}
case constant.DeleteLog:
logPath := path.Join(sitePath, "log", req.LogType)
if err := files.NewFileOp().WriteFile(logPath, strings.NewReader(""), constant.DirPerm); err != nil {
return nil, err
return err
}
}
return res, nil
return nil
}
func (w WebsiteService) ChangeDefaultServer(id uint) error {
+2 -1
View File
@@ -17,7 +17,8 @@ func (a *WebsiteRouter) InitRouter(Router *gin.RouterGroup) {
websiteRouter.GET("/list", baseApi.GetWebsites)
websiteRouter.POST("", baseApi.CreateWebsite)
websiteRouter.POST("/operate", baseApi.OpWebsite)
websiteRouter.POST("/log", baseApi.OpWebsiteLog)
websiteRouter.POST("/log/search", baseApi.GetWebsiteLog)
websiteRouter.POST("/log/operate", baseApi.OpWebsiteLog)
websiteRouter.POST("/check", baseApi.CreateWebsiteCheck)
websiteRouter.POST("/options", baseApi.GetWebsiteOptions)
websiteRouter.POST("/update", baseApi.UpdateWebsite)
+5
View File
@@ -130,8 +130,13 @@ export namespace Website {
id: number;
operate: string;
logType: string;
}
export interface WebSiteLogReq {
id: number;
page?: number;
pageSize?: number;
logType: string;
}
export interface OptionReq {
+5 -1
View File
@@ -27,7 +27,11 @@ export const opWebsite = (req: Website.WebSiteOp, node?: string) => {
};
export const opWebsiteLog = (req: Website.WebSiteOpLog) => {
return http.post<Website.WebSiteLog>(`/websites/log`, req);
return http.post<any>(`/websites/log/operate`, req);
};
export const getWebsiteLog = (req: Website.WebSiteLogReq) => {
return http.post<Website.WebSiteLog>(`/websites/log/search`, req);
};
export const updateWebsite = (req: Website.WebSiteUpdateReq) => {