Add logs controller and routes for log retrieval by month
This commit is contained in:
parent
fb49163803
commit
3ea963bd9e
|
|
@ -0,0 +1,93 @@
|
||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/samber/do"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
LogsController interface {
|
||||||
|
GetLogs(ctx *gin.Context)
|
||||||
|
GetLogsByMonth(ctx *gin.Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
logsController struct {
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewLogsController(injector *do.Injector) LogsController {
|
||||||
|
db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB)
|
||||||
|
return &logsController{
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *logsController) GetLogs(ctx *gin.Context) {
|
||||||
|
month := strings.ToLower(time.Now().Format("January"))
|
||||||
|
c.getLogsForMonth(ctx, month)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *logsController) GetLogsByMonth(ctx *gin.Context) {
|
||||||
|
month := ctx.Param("month")
|
||||||
|
c.getLogsForMonth(ctx, month)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *logsController) getLogsForMonth(ctx *gin.Context, month string) {
|
||||||
|
// Multiple possible log locations
|
||||||
|
logLocations := []string{
|
||||||
|
"/app/config/logs/query_log",
|
||||||
|
"./config/logs/query_log",
|
||||||
|
"/tmp/logs",
|
||||||
|
}
|
||||||
|
|
||||||
|
var logs []string
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
for _, logDir := range logLocations {
|
||||||
|
logFile := filepath.Join(logDir, month+"_query.log")
|
||||||
|
|
||||||
|
content, err := os.ReadFile(logFile)
|
||||||
|
if err == nil {
|
||||||
|
// File found, parse content
|
||||||
|
logEntries := strings.Split(string(content), "\r\n")
|
||||||
|
for _, entry := range logEntries {
|
||||||
|
if strings.TrimSpace(entry) != "" {
|
||||||
|
logs = append(logs, entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
logs = []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Jika request Accept header adalah application/json, kembalikan JSON
|
||||||
|
if ctx.GetHeader("Accept") == "application/json" {
|
||||||
|
res := utils.BuildResponseSuccess("Success get logs", gin.H{
|
||||||
|
"month": month,
|
||||||
|
"logs": logs,
|
||||||
|
"total": len(logs),
|
||||||
|
})
|
||||||
|
ctx.JSON(http.StatusOK, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default kembalikan HTML page
|
||||||
|
ctx.HTML(http.StatusOK, "logs.html", gin.H{
|
||||||
|
"Month": month,
|
||||||
|
"Logs": logs,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
package logs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/middlewares"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/modules/logs/controller"
|
||||||
|
userService "github.com/Caknoooo/go-gin-clean-starter/modules/user/service"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/samber/do"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterRoutes(server *gin.Engine, injector *do.Injector) {
|
||||||
|
logsController := do.MustInvoke[controller.LogsController](injector)
|
||||||
|
userService := do.MustInvoke[userService.UserService](injector)
|
||||||
|
jwtService := do.MustInvokeNamed[service.JWTService](injector, constants.JWTService)
|
||||||
|
|
||||||
|
// Public routes (tanpa auth) - untuk testing
|
||||||
|
publicLogs := server.Group("/logs")
|
||||||
|
{
|
||||||
|
publicLogs.GET("", logsController.GetLogs)
|
||||||
|
publicLogs.GET("/:month", logsController.GetLogsByMonth)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Protected routes (hanya superadmin)
|
||||||
|
protectedLogs := server.Group("/admin/logs")
|
||||||
|
protectedLogs.Use(middlewares.Authenticate(jwtService))
|
||||||
|
protectedLogs.Use(middlewares.RoleSuperAdmin(userService))
|
||||||
|
{
|
||||||
|
protectedLogs.GET("", logsController.GetLogs)
|
||||||
|
protectedLogs.GET("/:month", logsController.GetLogsByMonth)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue