按 content-system.md 设计文档,实现帖子内容系统的最小可行 MVC。 新增: - Model: Post 模型,含 5 种状态(draft/pending/approved/rejected/locked) - Repository: post_repo.go,分页查询(前台 approved 列表 + 后台全状态筛选) - Service: post_service.go,核心业务逻辑(状态流转、Markdown 渲染 Goldmark) - Controller: post_controller + admin_post_controller,SSR 页面 6 个 + API 13 个 - Template: 列表/详情/编辑器/404 + 管理员列表,各配 CSS/JS - 路由: /posts, /posts/:id, /posts/new, /posts/:id/edit, REST API, Admin API - gomod: + github.com/yuin/goldmark v1.8.2 修改: - Main: AutoMigrate Post 表 - Router: deps 注册 PostService/Controller,路由注册 - Nav: 新增“帖子”导航链接 - Admin Sidebar: 新增“内容管理”入口 设计: - 审核开关复用 audit.enabled,开启时帖子为 draft,关闭后直接 approved - 仅 approved 公开可见,作者/admin 可预览非公开状态帖子 - 严格分层 ISP 接口: Controller → postUseCase, Service → postStore, Repo → *PostRepo - 状态保护: pending/locked 禁止编辑,rejected 编辑后自动重置为 draft
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package router
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"metazone.cc/metalab/internal/common"
|
||
"metazone.cc/metalab/internal/config"
|
||
"metazone.cc/metalab/internal/middleware"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// setupFrontendRoutes 注册前端页面路由
|
||
func setupFrontendRoutes(r *gin.Engine, cfg *config.Config, d *dependencies) {
|
||
// --- 页面路由(CSRF 仅下发 token,不验证) ---
|
||
pages := r.Group("/")
|
||
pages.Use(d.authMdw.Optional())
|
||
pages.Use(middleware.CSRF(cfg))
|
||
pages.Use(func(c *gin.Context) {
|
||
middleware.SetCSRFToken(c, cfg)
|
||
c.Next()
|
||
})
|
||
{
|
||
pages.GET("/", func(c *gin.Context) {
|
||
c.HTML(http.StatusOK, "home/index.html", common.BuildPageData(c, gin.H{
|
||
"Title": "首页",
|
||
"ExtraCSS": "/static/css/home.css",
|
||
}))
|
||
})
|
||
|
||
pages.GET("/settings", func(c *gin.Context) {
|
||
c.Redirect(http.StatusFound, "/settings/profile")
|
||
})
|
||
pages.GET("/settings/:tab", d.settingsCtrl.SettingsPage)
|
||
pages.GET("/messages", d.messageCtrl.MessagesPage)
|
||
|
||
// 帖子页面
|
||
pages.GET("/posts", d.postCtrl.ListPage)
|
||
pages.GET("/posts/:id", d.postCtrl.ShowPage)
|
||
}
|
||
|
||
// 帖子页面(需登录)
|
||
postPages := r.Group("/posts")
|
||
postPages.Use(d.authMdw.Required())
|
||
{
|
||
postPages.GET("/new", d.postCtrl.NewPage)
|
||
postPages.GET("/:id/edit", d.postCtrl.EditPage)
|
||
}
|
||
|
||
// 认证页面(已登录自动跳走)
|
||
authPages := r.Group("/auth")
|
||
authPages.Use(d.authMdw.Optional())
|
||
authPages.Use(middleware.CSRF(cfg))
|
||
authPages.Use(func(c *gin.Context) {
|
||
middleware.SetCSRFToken(c, cfg)
|
||
c.Next()
|
||
})
|
||
{
|
||
authPages.GET("/register", d.authCtrl.RegisterPage)
|
||
authPages.GET("/login", d.authCtrl.LoginPage)
|
||
}
|
||
}
|