b93c6f1e16
- FTS5中文分词搜索(需-tags=sqlite_fts5) - [[wiki链接]]反向链接+SVG知识图谱 - 自动保存草稿(不触发版本历史) - 标签重命名/合并/删除+使用统计 - 后台编辑实时预览+格式工具栏 - PWA manifest+service worker+移动端适配 - 冒烟测试扩至66例全通过
116 lines
3.8 KiB
Go
116 lines
3.8 KiB
Go
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)
|
|
|
|
// 自动保存草稿
|
|
adminApi.POST("/notes/:id/draft", noteHandler.SaveDraft)
|
|
adminApi.DELETE("/notes/:id/draft", noteHandler.DiscardDraft)
|
|
|
|
// 双向链接 / 知识图谱
|
|
adminApi.GET("/notes/:id/backlinks", noteHandler.GetBacklinks)
|
|
adminApi.GET("/graph", noteHandler.GetKnowledgeGraph)
|
|
|
|
// 标签管理
|
|
adminApi.GET("/tags/usage", noteHandler.GetTagUsage)
|
|
adminApi.POST("/tags/rename", noteHandler.RenameTag)
|
|
adminApi.POST("/tags/merge", noteHandler.MergeTag)
|
|
adminApi.DELETE("/tags", noteHandler.DeleteTag)
|
|
}
|
|
|
|
// ─────────── 后台管理路由 ───────────
|
|
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")
|
|
})
|
|
|
|
// PWA 静态资源(manifest / service worker / 图标)
|
|
r.StaticFile("/manifest.json", "./web/manifest.json")
|
|
r.StaticFile("/sw.js", "./web/sw.js")
|
|
r.StaticFile("/icon-192.png", "./web/icon-192.png")
|
|
r.StaticFile("/icon-512.png", "./web/icon-512.png")
|
|
|
|
return r
|
|
}
|