wms-be/modules/monitoring/controller/monitor_controller.go

69 lines
1.3 KiB
Go

package controller
import (
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
)
type MonitoringController interface {
HealthCheck(ctx *gin.Context)
ReadErrorLog(ctx *gin.Context)
}
type monitoringController struct{}
func NewMonitoringController() MonitoringController {
return &monitoringController{}
}
func (c *monitoringController) HealthCheck(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"status": "healthy",
"message": "Service is running",
})
}
func (c *monitoringController) ReadErrorLog(ctx *gin.Context) {
logFile := "gin.log"
// Cari file error di beberapa lokasi
locations := []string{
"./logs/" + logFile,
"/tmp/" + logFile,
"/var/log/" + logFile,
"./" + logFile,
}
var content string
var found bool
for _, location := range locations {
if data, err := os.ReadFile(location); err == nil {
content = string(data)
found = true
break
}
}
if !found {
ctx.JSON(http.StatusNotFound, gin.H{
"error": "Error log file not found",
})
return
}
// Return sebagai plain text atau JSON
if ctx.GetHeader("Accept") == "application/json" {
lines := strings.Split(content, "\n")
ctx.JSON(http.StatusOK, gin.H{
"log_entries": lines,
"total_lines": len(lines),
})
} else {
ctx.Data(http.StatusOK, "text/plain", []byte(content))
}
}