mirror of
https://github.com/xpzouying/xiaohongshu-mcp.git
synced 2026-08-28 17:45:51 +08:00
16ec75c7a3
为 /api/v1/* 与 /mcp 增加可选的静态 Bearer Token 鉴权,默认关闭以保持向后兼容。 - 通过 AUTH_TOKEN 环境变量或 -token 启动参数配置,非空参数优先 - /health 与 CORS OPTIONS 预检保持公开 - scheme 大小写不敏感,仅对 credentials 做常量时间比较 - Token 不出现在日志、错误响应与 -h 帮助输出中 - 补充中英文 README、docs/API.md 与 Docker 文档
74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// AppServer 应用服务器结构体,封装所有服务和处理器
|
|
type AppServer struct {
|
|
xiaohongshuService *XiaohongshuService
|
|
mcpServer *mcp.Server
|
|
router *gin.Engine
|
|
httpServer *http.Server
|
|
authToken string
|
|
}
|
|
|
|
// NewAppServer 创建新的应用服务器实例
|
|
func NewAppServer(xiaohongshuService *XiaohongshuService, authToken string) *AppServer {
|
|
appServer := &AppServer{
|
|
xiaohongshuService: xiaohongshuService,
|
|
authToken: authToken,
|
|
}
|
|
|
|
// 初始化 MCP Server(需要在创建 appServer 之后,因为工具注册需要访问 appServer)
|
|
appServer.mcpServer = InitMCPServer(appServer)
|
|
|
|
return appServer
|
|
}
|
|
|
|
// Start 启动服务器
|
|
func (s *AppServer) Start(port string) error {
|
|
s.router = setupRoutes(s)
|
|
|
|
s.httpServer = &http.Server{
|
|
Addr: port,
|
|
Handler: s.router,
|
|
}
|
|
|
|
// 启动服务器的 goroutine
|
|
go func() {
|
|
logrus.Infof("启动 HTTP 服务器: %s", port)
|
|
if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
logrus.Errorf("服务器启动失败: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
|
|
// 等待中断信号
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
<-quit
|
|
|
|
logrus.Infof("正在关闭服务器...")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
if err := s.httpServer.Shutdown(ctx); err != nil {
|
|
logrus.Warnf("等待连接关闭超时,强制退出: %v", err)
|
|
} else {
|
|
logrus.Infof("服务器已优雅关闭")
|
|
}
|
|
|
|
return nil
|
|
}
|