package controller import ( "net/http" "os" "strings" "time" "github.com/gin-gonic/gin" ) type MonitoringController interface { HealthCheck(ctx *gin.Context) ReadRouteLog(ctx *gin.Context) ReadQueryLog(ctx *gin.Context) ReadAppLog(ctx *gin.Context) } type monitoringController struct{} var validMonths = map[string]bool{ "january": true, "february": true, "march": true, "april": true, "may": true, "june": true, "july": true, "august": true, "september": true, "october": true, "november": true, "december": true, } // ReadAppLog implements MonitoringController. func (c *monitoringController) ReadAppLog(ctx *gin.Context) { // Ambil query param 'month', default ke bulan sekarang jika tidak ada month := ctx.Query("month") if month == "" { month = time.Now().Format("January") // contoh: "December" } else { month = strings.ToLower(month) if !validMonths[month] { ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid month"}) return } month = strings.ToTitle(month) } logFile := strings.ToLower(month) + "_app.log" // contoh: "december_app.log" // Cari file log di beberapa lokasi locations := []string{ "config/logs/query_log/" + logFile, // lokasi log sesuai permintaan } 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": "App log file not found", }) return } // Return sebagai plain text atau JSON if strings.Contains(ctx.GetHeader("Accept"), "application/json") { lines := strings.Split(strings.ReplaceAll(content, "\r\n", "\n"), "\n") if len(lines) > 0 && lines[len(lines)-1] == "" { lines = lines[:len(lines)-1] } ctx.JSON(http.StatusOK, gin.H{ "log_entries": lines, "total_lines": len(lines), }) } else { ctx.Data(http.StatusOK, "text/plain; charset=utf-8", []byte(content)) } } // ReadQueryLog implements MonitoringController. func (c *monitoringController) ReadQueryLog(ctx *gin.Context) { // Ambil query param 'month', default ke bulan sekarang jika tidak ada month := ctx.Query("month") if month == "" { month = time.Now().Format("January") // contoh: "December" } else { month = strings.ToLower(month) if !validMonths[month] { ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid month"}) return } month = strings.ToTitle(month) } logFile := strings.ToLower(month) + "_query.log" // contoh: "december_query.log" // Lokasi file log locations := []string{ "config/logs/query_log/" + logFile, // lokasi log sesuai permintaan } 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": "Query log file not found", }) return } // Return sebagai plain text atau JSON if strings.Contains(ctx.GetHeader("Accept"), "application/json") { lines := strings.Split(strings.ReplaceAll(content, "\r\n", "\n"), "\n") if len(lines) > 0 && lines[len(lines)-1] == "" { lines = lines[:len(lines)-1] } ctx.JSON(http.StatusOK, gin.H{ "log_entries": lines, "total_lines": len(lines), }) } else { ctx.Data(http.StatusOK, "text/plain; charset=utf-8", []byte(content)) } } 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) ReadRouteLog(ctx *gin.Context) { logFile := "gin.log" // Cari file error di beberapa lokasi locations := []string{ "./" + 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)) } }