Files
main-tbbank/backend/internal/admin/ui/handler.go
2026-08-18 00:59:32 +05:00

109 lines
2.7 KiB
Go

// Package ui serves the admin dashboard pages at /admin.
// Pages are template shells; all data is fetched client-side from the
// existing JWT-protected /api/v1/admin API.
package ui
import (
"bankline/internal/appresult"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
// Resource describes an admin CRUD resource rendered by the generic
// list/form templates. Slug is both the URL segment and the key used by
// the client-side schema in resources.js.
type Resource struct {
Slug string
Title string
}
var resources = []Resource{
{Slug: "categories", Title: "Categories"},
{Slug: "sub-categories", Title: "Sub-categories"},
{Slug: "banners", Title: "Banners"},
{Slug: "news", Title: "News"},
{Slug: "static", Title: "Static pages"},
{Slug: "currency", Title: "Currency rates"},
}
// Register mounts templates, static assets and all dashboard page routes.
func Register(router *gin.Engine) {
router.LoadHTMLGlob("web/admin/templates/**/*.html")
router.Static("/M0n1t0r/assets", "./web/admin/assets")
admin := router.Group("/M0n1t0r")
admin.GET("/login", appresult.AdminGuestOnly(), func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/login", gin.H{
"Title": "Login",
"Page": "login",
})
})
authed := admin.Group("")
authed.Use(appresult.AdminPageAuth())
authed.GET("", func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/dashboard", gin.H{
"Title": "Dashboard",
"Page": "dashboard",
"Active": "dashboard",
})
})
authed.GET("/settings", func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/settings", gin.H{
"Title": "Settings",
"Page": "settings",
"Active": "settings",
})
})
for _, res := range resources {
res := res
authed.GET("/"+res.Slug, func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/list", gin.H{
"Title": res.Title,
"Page": "list",
"Active": res.Slug,
"Resource": res.Slug,
})
})
authed.GET("/"+res.Slug+"/new", func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/form", gin.H{
"Title": "New — " + res.Title,
"Page": "form",
"Active": res.Slug,
"Resource": res.Slug,
"Mode": "create",
})
})
authed.GET("/"+res.Slug+"/:id/edit", func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/form", gin.H{
"Title": "Edit — " + res.Title,
"Page": "form",
"Active": res.Slug,
"Resource": res.Slug,
"Mode": "edit",
"Id": c.Param("id"),
})
})
}
router.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/M0n1t0r") {
c.HTML(http.StatusNotFound, "pages/404", gin.H{
"Title": "Page not found",
"Page": "404",
})
return
}
c.JSON(http.StatusNotFound, gin.H{"status": false, "message": "not found"})
})
}