47 lines
1.7 KiB
Go
47 lines
1.7 KiB
Go
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)
|
|
// }
|
|
|
|
// Serve HTML page when browser requests text/html (no auth) — placed BEFORE protected group
|
|
// server.GET("/admin/logs", func(ctx *gin.Context) {
|
|
// acceptHeader := ctx.GetHeader("Accept")
|
|
// if strings.Contains(acceptHeader, "text/html") {
|
|
// ctx.HTML(http.StatusOK, "logs.html", gin.H{})
|
|
// return
|
|
// }
|
|
// // untuk API call lanjut ke handler berikutnya (mis. protected group)
|
|
// ctx.Next()
|
|
// })
|
|
|
|
server.GET("/admin/logs", logsController.ServeLogsPage)
|
|
|
|
// Protected routes (hanya superadmin)
|
|
protectedLogs := server.Group("/api/v1/logs")
|
|
protectedLogs.Use(middlewares.Authenticate(jwtService))
|
|
protectedLogs.Use(middlewares.RoleSuperAdmin(userService))
|
|
{
|
|
protectedLogs.GET("", logsController.GetLogs)
|
|
protectedLogs.GET("/:month", logsController.GetLogsByMonth)
|
|
}
|
|
}
|