初始化

This commit is contained in:
李琦
2026-08-15 07:41:11 +08:00
commit d9475ac9da
18 changed files with 1109 additions and 0 deletions

44
internal/router/router.go Normal file
View File

@@ -0,0 +1,44 @@
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
}