| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 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) // 密码验证访问
- // 需要认证的管理接口
- notes.POST("", noteHandler.CreateNote)
- notes.PUT("/:id", noteHandler.UpdateNote)
- notes.DELETE("/:id", noteHandler.DeleteNote)
- }
- api.GET("/categories", noteHandler.GetCategories)
- api.GET("/tags", noteHandler.GetTags)
- api.GET("/tree", noteHandler.GetPublicTree) // 前台公开树
- }
- // 管理后台专用 API(需要认证)
- adminApi := r.Group("/admin/api")
- adminApi.Use(func(c *gin.Context) {
- token, err := c.Cookie("admin_token")
- if err != nil || token != "authenticated" {
- c.JSON(401, gin.H{"code": 401, "message": "请先登录"})
- c.Abort()
- return
- }
- c.Next()
- })
- {
- adminApi.GET("/tree", noteHandler.GetTree) // 后台完整树
- adminApi.POST("/upload", imageHandler.Upload) // 图片上传
- adminApi.GET("/export/:id", noteHandler.ExportNote) // 导出笔记
- adminApi.POST("/import", noteHandler.ImportNotes) // 导入笔记
- }
- // 后台管理路由
- 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("/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
- }
|