- 新增 site_settings 单行表(迁移 v7),保存网站名称、Logo 图片地址与页脚版权文案 - 新增 GET /api/site(公开)与 PUT /api/site(仅管理员),空值回退前端默认展示 - 前端头部、登录注册页、浏览器标题与页脚接入站点信息,Pinia store 启动时加载 - 新增 /admin/site 后台管理页面,头像下拉仅管理员显示“后台管理”,路由加 requiresAdmin 守卫 - 补充站点接口、迁移与权限测试,重新生成 Swagger 文档
105 lines
2.9 KiB
Go
105 lines
2.9 KiB
Go
// Package api 负责 HTTP 接口路由装配。
|
|
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"path"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
swaggerFiles "github.com/swaggo/files"
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
"gorm.io/gorm"
|
|
|
|
"rill/internal/auth"
|
|
"rill/internal/config"
|
|
"rill/internal/database"
|
|
"rill/internal/note"
|
|
"rill/internal/site"
|
|
"rill/internal/user"
|
|
"rill/internal/usergroup"
|
|
)
|
|
|
|
// RegisterRoutes 注册 API 路由。health、swagger、auth 与站点信息读取公开;notes 与个人资料需登录;站点信息更新、用户与用户组管理仅限管理员。
|
|
func RegisterRoutes(rg *gin.RouterGroup, db *gorm.DB, cfg *config.Config) {
|
|
authn := auth.NewAuthenticator(cfg)
|
|
|
|
rg.GET("/health", health(db))
|
|
|
|
swagger := rg.Group("/swagger")
|
|
{
|
|
swagger.GET("", func(c *gin.Context) {
|
|
c.Redirect(http.StatusFound, path.Join("/", rg.BasePath(), "swagger", "index.html"))
|
|
})
|
|
swagger.GET("/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("doc.json")))
|
|
}
|
|
|
|
authGroup := rg.Group("/auth")
|
|
{
|
|
authGroup.POST("/register", auth.Register(db))
|
|
authGroup.POST("/login", auth.Login(db, authn))
|
|
}
|
|
|
|
rg.GET("/site", site.Get(db))
|
|
|
|
authed := rg.Group("", authn.RequireAuth(db))
|
|
{
|
|
authed.GET("/me", auth.Me())
|
|
authed.PUT("/me", auth.UpdateMe(db))
|
|
|
|
notes := authed.Group("/notes")
|
|
{
|
|
notes.GET("", note.List(db))
|
|
notes.POST("", note.Create(db))
|
|
notes.GET("/:id", note.Get(db))
|
|
notes.PUT("/:id", note.Update(db))
|
|
notes.DELETE("/:id", note.Delete(db))
|
|
}
|
|
}
|
|
|
|
admin := rg.Group("", authn.RequireAuth(db), auth.RequireAdmin())
|
|
{
|
|
admin.PUT("/site", site.Update(db))
|
|
|
|
users := admin.Group("/users")
|
|
{
|
|
users.GET("", user.List(db))
|
|
users.POST("", user.Create(db))
|
|
users.GET("/:id", user.Get(db))
|
|
users.PUT("/:id", user.Update(db))
|
|
users.DELETE("/:id", user.Delete(db))
|
|
}
|
|
|
|
groups := admin.Group("/user-groups")
|
|
{
|
|
groups.GET("", usergroup.List(db))
|
|
groups.POST("", usergroup.Create(db))
|
|
groups.GET("/:id", usergroup.Get(db))
|
|
groups.PUT("/:id", usergroup.Update(db))
|
|
groups.DELETE("/:id", usergroup.Delete(db))
|
|
}
|
|
}
|
|
}
|
|
|
|
// HealthResponse 健康检查响应。
|
|
type HealthResponse struct {
|
|
Status string `json:"status" example:"ok"`
|
|
Error string `json:"error,omitempty" example:"database unavailable"`
|
|
}
|
|
|
|
// @Summary Health check
|
|
// @Description Check service and database connectivity; returns 503 when the database is unavailable.
|
|
// @Tags system
|
|
// @Produce json
|
|
// @Success 200 {object} api.HealthResponse
|
|
// @Failure 503 {object} api.HealthResponse
|
|
// @Router /health [get]
|
|
func health(db *gorm.DB) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if err := database.Ping(c.Request.Context(), db); err != nil {
|
|
c.JSON(http.StatusServiceUnavailable, HealthResponse{Status: "error", Error: "database unavailable"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, HealthResponse{Status: "ok"})
|
|
}
|
|
}
|