Files
ToNav-go/main.go
T
openclaw 909387ef52 feat: MCP 管理进后台 UI (v2.1) — 开关/Token 即时生效无需重启
- settings 表新增 mcp_enabled / mcp_token,鉴权中间件动态读库,后台改动即时生效(实测验证:关->503、开->200、换 token 旧401新200,均不重启)
- 首次启动自动把 TONAV_MCP_TOKEN 环境变量迁移进数据库(兼容旧部署,systemd 现有 token 无感切换)
- 后台新增 API:GET /admin/api/mcp/status(token 掩码)、POST /admin/api/mcp/token(重新生成+自动启用,仅一次明文展示)、POST /admin/api/mcp/enabled
- 后台设置弹窗新增 MCP Server 区块:开关 checkbox、token 掩码展示、一键重新生成
- README 同步
2026-09-11 14:46:47 +08:00

88 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()
handlers.SetupMCP()
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 管理(后台 UI)
admin.GET("/api/mcp/status", handlers.MCPStatus)
admin.POST("/api/mcp/token", handlers.MCPRegenerateToken)
admin.POST("/api/mcp/enabled", handlers.MCPSetEnabled)
}
// MCP (Model Context Protocol) 端点
// POST: 请求-响应模式;GET: SSE 流式(服务端主动推送 notifications/tools/list_changed)
// 鉴权中间件动态读数据库:后台开关/Token 改动即时生效,无需重启
r.POST("/mcp", handlers.MCPAuthRequired(), handlers.MCPHandler)
r.GET("/mcp", handlers.MCPAuthRequired(), handlers.MCPSSEHandler)
log.Printf("ToNav-go 启动在端口 %s...", cfg.Port)
r.Run(":" + cfg.Port)
}