Files
nl-pms-api/internal/router/router.go
2026-08-15 07:41:11 +08:00

45 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package router
import (
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"nl-pms-api/internal/config"
"nl-pms-api/internal/handler"
"nl-pms-api/internal/middleware"
)
// New 组装路由:
// - GET /healthz 健康检查公开view「测试连接」用
// - POST /api/v1/files 上传图片Bearer 密钥鉴权)
// - GET /api/v1/files 素材库列表scope=mine|team|all按角色限定范围
// - DELETE /api/v1/files/:id 删除素材(本人 / 超管 id=1 / 团队 owner|admin
// - GET /files/*filepath 文件公开访问img 标签无法带鉴权头;随机文件名不可枚举)
func New(cfg *config.Config, db *gorm.DB) *gin.Engine {
if cfg.Env != "dev" {
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
r.Use(gin.Logger(), gin.Recovery())
r.MaxMultipartMemory = 8 << 20
r.GET("/healthz", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"ok": true, "service": "nl-pms-api"})
})
h := &handler.FileHandler{DB: db, Cfg: cfg}
api := r.Group("/api/v1", middleware.RequireAPIKey(cfg.APIKey))
api.POST("/files", h.Upload)
api.GET("/files", h.List)
api.DELETE("/files/:id", h.Delete)
files := r.Group("/files", func(c *gin.Context) {
// 文件名含随机 hex内容不可变允许长缓存。
c.Header("Cache-Control", "public, max-age=31536000, immutable")
})
files.Static("/", cfg.StorageDir)
return r
}