refactor: 重构代码架构,解耦处理器和服务 (#13)
重大架构改进: - 移除全局变量 xiaohongshuService,改用依赖注入 - 将代码按职责分离到不同文件: * app_server.go - 核心服务器结构 * types.go - 统一类型定义 * routes.go - 路由配置 * middleware.go - 中间件 * handlers_api.go - REST API 处理器 * handlers_mcp.go - MCP 协议处理器 - 将通用工具函数从 AppServer 方法改为独立函数 - 删除未使用的类型定义(LoginWaitRequest) - 修复 JSON 编码错误处理 优势: ✅ 更好的依赖注入模式 ✅ 清晰的职责分离 ✅ 提高代码可测试性 ✅ 符合 Go 最佳实践 ✅ 降低耦合度,提高内聚性 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
37
routes.go
Normal file
37
routes.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// setupRoutes 设置路由配置
|
||||
func setupRoutes(appServer *AppServer) *gin.Engine {
|
||||
// 设置 Gin 模式
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
router := gin.New()
|
||||
router.Use(gin.Logger())
|
||||
router.Use(gin.Recovery())
|
||||
|
||||
// 添加中间件
|
||||
router.Use(errorHandlingMiddleware())
|
||||
router.Use(corsMiddleware())
|
||||
|
||||
// 健康检查
|
||||
router.GET("/health", healthHandler)
|
||||
|
||||
// MCP 端点 - 使用 SSE 协议
|
||||
mcpHandler := appServer.createMCPHandler()
|
||||
router.Any("/mcp", gin.WrapH(mcpHandler))
|
||||
router.Any("/mcp/*path", gin.WrapH(mcpHandler))
|
||||
|
||||
// API 路由组
|
||||
api := router.Group("/api/v1")
|
||||
{
|
||||
api.GET("/login/status", appServer.checkLoginStatusHandler)
|
||||
api.POST("/publish", appServer.publishHandler)
|
||||
api.GET("/feeds/list", appServer.listFeedsHandler)
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
Reference in New Issue
Block a user