c8f03dd932
功能特性: - Markdown 编辑与实时预览 - 代码语法高亮 - 目录树形结构管理 - 图片粘贴上传 - Markdown 文件导入导出 - 笔记密码保护 - 前后端分离架构 技术栈: - Go + Gin + GORM + SQLite - 原生 HTML/CSS/JavaScript - Highlight.js
80 خطوط
2.1 KiB
Go
80 خطوط
2.1 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) // 密码验证访问
|
|
|
|
// 需要认证的管理接口
|
|
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
|
|
}
|