package router import ( "github.com/gin-gonic/gin" "note-manager/config" "note-manager/handler" "note-manager/middleware" ) // Setup 初始化路由 func Setup(r *gin.Engine, noteHandler *handler.NoteHandler, adminHandler *handler.AdminHandler, imageHandler *handler.ImageHandler, cfg *config.Config) *gin.Engine { // 全局中间件 r.Use(middleware.CORS()) // 静态文件服务(图片) r.Static("/uploads", cfg.UploadDir) // ─────────── 公开 API ─────────── api := r.Group("/api") { notes := api.Group("/notes") { notes.GET("", noteHandler.ListNotes) notes.GET("/search", noteHandler.SearchNotes) notes.GET("/:id", noteHandler.GetNote) // 公开安全版:仅公开且无密码的笔记 notes.POST("/:id/access", noteHandler.AccessNote) // 密码验证访问 } api.GET("/categories", noteHandler.GetCategories) api.GET("/tags", noteHandler.GetTags) api.GET("/tree", noteHandler.GetPublicTree) // 前台公开树 // 分享 JSON 接口(公开) api.GET("/share/:token", noteHandler.GetSharedNote) } // ─────────── 管理后台 API(需认证)─────────── adminApi := r.Group("/admin/api") adminApi.Use(middleware.AuthRequired()) { // 笔记写操作 adminApi.POST("/notes", noteHandler.CreateNote) adminApi.GET("/notes/:id", noteHandler.GetAdminNote) adminApi.PUT("/notes/:id", noteHandler.UpdateNote) adminApi.DELETE("/notes/:id", noteHandler.DeleteNote) // 回收站 adminApi.GET("/trash", noteHandler.ListTrash) adminApi.POST("/restore/:id", noteHandler.RestoreNote) adminApi.POST("/purge/:id", noteHandler.PurgeNote) adminApi.POST("/empty-trash", noteHandler.EmptyTrash) // 版本历史 adminApi.GET("/notes/:id/versions", noteHandler.ListVersions) adminApi.POST("/notes/:id/restore-version", noteHandler.RestoreVersion) // 分享 adminApi.POST("/notes/:id/share", noteHandler.CreateShare) adminApi.POST("/notes/:id/revoke-share", noteHandler.RevokeShare) // 导入导出 adminApi.GET("/export/:id", noteHandler.ExportNote) adminApi.GET("/export-all", noteHandler.ExportAll) adminApi.POST("/import", noteHandler.ImportNotes) // 完整树 + 图片上传 adminApi.GET("/tree", noteHandler.GetTree) adminApi.POST("/upload", imageHandler.Upload) } // ─────────── 后台管理路由 ─────────── admin := r.Group("/admin") { admin.GET("/login", adminHandler.LoginPage) admin.POST("/login", adminHandler.Login) admin.POST("/logout", adminHandler.Logout) admin.GET("/auth", adminHandler.CheckAuth) admin.GET("/", adminHandler.IndexPage) } // ─────────── 分享阅读页 ─────────── r.GET("/share/:token", noteHandler.SharePage) // 健康检查 r.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) }) // 前端页面 - 根路径返回展示页面 r.GET("/", func(c *gin.Context) { c.File("./web/index.html") }) return r }