Files
ToNav-go/main.go
T
openclaw c707667a84 feat: 原生集成 MCP Server (POST /mcp, JSON-RPC 2.0, Bearer Token)
- Streamable HTTP 传输,支持 initialize/ping/tools/list/tools/call 及批量调用
- 6 个工具: add_service(自动抓 title/description/favicon)、create_category、
  list_categories(含服务计数)、list_services(keyword/category/tag 过滤)、
  update_service、delete_service
- 鉴权: TONAV_MCP_TOKEN 环境变量 (systemd 注入)
2026-09-11 06:33:28 +08:00

80 lines
2.4 KiB
Go

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()
handlers.LoadMCPConfig()
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) 端点
r.POST("/mcp", handlers.MCPAuthRequired(), handlers.MCPHandler)
log.Printf("ToNav-go 启动在端口 %s...", cfg.Port)
r.Run(":" + cfg.Port)
}