Files
ToNav-go/main.go
T
openclaw 9d7282074f feat: MCP v2 — SSE 流式端点、独立搜索工具、移除 token 默认兜底
- GET /mcp SSE 端点:endpoint 握手 + notifications/tools/list_changed 实时推送(服务/分类增删触发),30s keepalive
- tonav_search_services 独立全文搜索工具,相关度排序(name>tags>description>url),支持 tag 收窄
- TONAV_MCP_TOKEN 未设置时 MCP 端点禁用(503)+ 启动警告,不再回退到硬编码默认 token
- 服务端版本号 1.0.0 -> 2.0.0
- README 同步更新(工具表 7 个、双端点说明、部署变量表)
2026-09-11 06:48:37 +08:00

90 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"log"
"net/http"
"tonav-go/database"
"tonav-go/handlers"
config "tonav-go/utils"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
)
func main() {
cfg := config.LoadConfig()
database.InitDB(cfg.DBPath)
database.Seed()
handlers.StartHealthCheck()
handlers.StartAutoBackup()
if err := handlers.LoadMCPConfig(); err != nil {
log.Printf("⚠️ MCP 未启用: %v", err)
}
r := gin.Default()
store := cookie.NewStore([]byte(cfg.SecretKey))
r.Use(sessions.Sessions("tonav_session", store))
r.Static("/static", "./static")
// 显式逐个加载,彻底杜绝路径猜测问题
r.LoadHTMLFiles(
"templates/index.html",
"templates/admin/login.html",
"templates/admin/dashboard.html",
"templates/admin/categories.html",
"templates/admin/services.html",
"templates/admin/change_password.html",
)
r.GET("/", handlers.IndexHandler)
r.GET("/admin/login", func(c *gin.Context) {
c.HTML(http.StatusOK, "login.html", nil)
})
r.POST("/admin/login", handlers.LoginHandler)
r.GET("/admin/logout", handlers.LogoutHandler)
admin := r.Group("/admin")
admin.Use(handlers.AuthRequired())
{
admin.GET("", func(c *gin.Context) {
c.Redirect(http.StatusFound, "/admin/dashboard")
})
admin.GET("/dashboard", handlers.DashboardHandler)
admin.GET("/services", handlers.ServicesPageHandler)
admin.GET("/categories", handlers.CategoriesPageHandler)
admin.GET("/change-password", handlers.ChangePasswordHandler)
admin.POST("/change-password", handlers.ChangePasswordHandler)
admin.GET("/api/services", handlers.GetServices)
admin.POST("/api/services", handlers.SaveService)
admin.PUT("/api/services/:id", handlers.SaveService)
admin.DELETE("/api/services/:id", handlers.DeleteService)
admin.GET("/api/categories", handlers.GetCategories)
admin.POST("/api/categories", handlers.SaveCategory)
admin.PUT("/api/categories/:id", handlers.SaveCategory)
admin.DELETE("/api/categories/:id", handlers.DeleteCategory)
admin.GET("/api/settings", handlers.GetSettings)
admin.POST("/api/settings", handlers.SaveSettings)
admin.POST("/api/backup/webdav", handlers.RunCloudBackup)
admin.GET("/api/backup/list", handlers.ListCloudBackups)
admin.DELETE("/api/backup/delete", handlers.DeleteCloudBackup)
admin.POST("/api/backup/restore", handlers.RestoreCloudBackup)
}
// MCP (Model Context Protocol) 端点
// POST: 请求-响应模式;GET: SSE 流式(服务端主动推送 notifications/tools/list_changed)
if handlers.MCPEnabled() {
r.POST("/mcp", handlers.MCPAuthRequired(), handlers.MCPHandler)
r.GET("/mcp", handlers.MCPAuthRequired(), handlers.MCPSSEHandler)
} else {
// 未配置 token 时仍注册端点,但返回 503 明确提示
r.POST("/mcp", handlers.MCPHandler)
r.GET("/mcp", handlers.MCPSSEHandler)
}
log.Printf("ToNav-go 启动在端口 %s...", cfg.Port)
r.Run(":" + cfg.Port)
}