Files
sale/backend/internal/api/handlers/log.go
T

75 lines
1.5 KiB
Go

package handlers
import (
"fmt"
"net/http"
"strconv"
"sale/internal/utils"
"github.com/gin-gonic/gin"
)
type LogHandler struct{}
func NewLogHandler() *LogHandler {
return &LogHandler{}
}
func (h *LogHandler) GetLogs(c *gin.Context) {
lines := 500
if l := c.Query("lines"); l != "" {
if v, err := strconv.Atoi(l); err == nil && v > 0 {
lines = v
}
}
logs, err := utils.GetLogs(lines)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read logs"})
return
}
c.JSON(http.StatusOK, gin.H{"data": logs})
}
func (h *LogHandler) ClearLogs(c *gin.Context) {
if err := utils.ClearLogs(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clear logs"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Logs cleared successfully"})
}
func (h *LogHandler) GetLogInfo(c *gin.Context) {
size, err := utils.GetLogSize()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get log info"})
return
}
modTime, err := utils.GetLogModTime()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get log info"})
return
}
var sizeStr string
if size >= 1024*1024 {
sizeStr = fmt.Sprintf("%.2f MB", float64(size)/(1024*1024))
} else if size >= 1024 {
sizeStr = fmt.Sprintf("%.2f KB", float64(size)/1024)
} else {
sizeStr = fmt.Sprintf("%d B", size)
}
c.JSON(http.StatusOK, gin.H{
"data": gin.H{
"size": size,
"size_str": sizeStr,
"mod_time": modTime,
},
})
}