fixed
This commit is contained in:
104
backend/internal/admin/ui/handler.go
Normal file
104
backend/internal/admin/ui/handler.go
Normal file
@@ -0,0 +1,104 @@
|
||||
// Package ui serves the admin dashboard pages at /admin.
|
||||
// Pages are template shells; all data is fetched client-side from the
|
||||
// existing JWT-protected /v1/admin API.
|
||||
package ui
|
||||
|
||||
import (
|
||||
"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("/admin/assets", "./web/admin/assets")
|
||||
|
||||
admin := router.Group("/admin")
|
||||
|
||||
admin.GET("/login", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "pages/login", gin.H{
|
||||
"Title": "Login",
|
||||
"Page": "login",
|
||||
})
|
||||
})
|
||||
|
||||
admin.GET("", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "pages/dashboard", gin.H{
|
||||
"Title": "Dashboard",
|
||||
"Page": "dashboard",
|
||||
"Active": "dashboard",
|
||||
})
|
||||
})
|
||||
|
||||
admin.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
|
||||
|
||||
admin.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,
|
||||
})
|
||||
})
|
||||
|
||||
admin.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",
|
||||
})
|
||||
})
|
||||
|
||||
admin.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, "/admin") {
|
||||
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"})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user