Files
kenaito-media-picker/internal/api/router.go
T
2025-09-01 21:11:28 +08:00

58 lines
1.8 KiB
Go

// Package api 是程序的"门面",所有网页请求都先进这里。
//
// 这里的 router.go 是"指路牌",负责把不同的网址指给不同的窗口(controller):
// - /api/scan/... → 扫描整理
// - /api/media/... → 媒体库与统计看板
// - /api/fs/... → 文件目录浏览(页面上"选择目录"用)
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"kenaito-media-picker/internal/service"
"kenaito-media-picker/internal/store"
)
// NewRouter 把"网址 → 窗口"的路由都登记好,返回给主程序去跑。
func NewRouter(st *store.Store) *gin.Engine {
r := gin.New()
r.Use(gin.Recovery())
// 健康检查:别的程序或人想知道"服务还活着吗",就访问这个
r.GET("/health/check", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
// 所有 /api/ 开头的接口都归到这里
apiGroup := r.Group("/api")
{
// 媒体库 + 统计看板
mc := NewMediaController(st)
apiGroup.POST("/media/list", mc.List)
apiGroup.POST("/media/stats", mc.Stats)
apiGroup.GET("/media/preview", mc.Preview)
apiGroup.PUT("/media/:id/manual-nsfw", mc.UpdateManualNsfw)
apiGroup.GET("/media/manual-nsfw-avg", mc.AvgManualNsfwScore)
// 扫描整理(开始 / 中止 / 移动 / 查进度 / 实时进度)
sc := NewScanController(service.NewScanService(st))
apiGroup.POST("/scan/start", sc.Start)
apiGroup.POST("/scan/cancel", sc.Cancel)
apiGroup.POST("/scan/move", sc.Move)
apiGroup.GET("/scan/status", sc.Status)
apiGroup.GET("/scan/ws", sc.Ws)
// 文件目录浏览(页面上"选择目录")
fc := NewFsController()
apiGroup.GET("/fs/list", fc.List)
// 系统配置
cc := NewConfigController(st)
apiGroup.GET("/config/nsfw", cc.GetNsfwConfig)
apiGroup.PUT("/config/nsfw", cc.UpdateNsfwConfig)
}
return r
}