package handler import ( "net/http" "strconv" "github.com/gin-gonic/gin" "zeromesh/internal/service" ) type LogHandler struct { logService *service.LogService } func NewLogHandler(logService *service.LogService) *LogHandler { return &LogHandler{logService: logService} } func (h *LogHandler) ListApiLogs(c *gin.Context) { page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20")) ip := c.Query("ip") path := c.Query("path") method := c.Query("method") statusCode, _ := strconv.Atoi(c.Query("status_code")) logs, total, err := h.logService.ListApiLogs(page, pageSize, ip, path, method, statusCode) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"data": logs, "total": total, "page": page, "page_size": pageSize}) } func (h *LogHandler) ListErrorLogs(c *gin.Context) { page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20")) ip := c.Query("ip") path := c.Query("path") method := c.Query("method") statusCode, _ := strconv.Atoi(c.Query("status_code")) logs, total, err := h.logService.ListErrorLogs(page, pageSize, ip, path, method, statusCode) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"data": logs, "total": total, "page": page, "page_size": pageSize}) } func (h *LogHandler) ListApiLogGrouped(c *gin.Context) { groups, err := h.logService.ListApiLogGrouped() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"data": groups}) } func (h *LogHandler) ClearApiLogs(c *gin.Context) { if err := h.logService.ClearApiLogs(); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "ok"}) } func (h *LogHandler) ClearErrorLogs(c *gin.Context) { if err := h.logService.ClearErrorLogs(); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "ok"}) }