Files
nl-im-websocket-demo/route/route.go

49 lines
1.3 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 route
* 作用:定义 Gin Web Server 的路由规则。
*/
package route
import (
"xk-websocket/controller" // 替换为实际路径
"github.com/gin-gonic/gin"
)
/**
* SetupRoutes
* 功能:配置 HTTP 和 WebSocket 路由。
* @param router *gin.Engine Gin引擎实例
* @param wsCtrl *controller.WebSocketController 控制器实例
*/
func SetupRoutes(router *gin.Engine, wsCtrl *controller.WebSocketController) {
router.Use(gin.Recovery())
router.GET("/ws", wsCtrl.HandleWebSocket)
// 内部 API(公开接口,不需要验证)
apiGroup := router.Group("/api")
{
apiGroup.POST("/send", wsCtrl.SendMessageHandler)
apiGroup.POST("/bind", wsCtrl.BindHandler)
apiGroup.GET("/health", wsCtrl.HealthHandler)
apiGroup.GET("/check-user-online", wsCtrl.IsUserOnline)
apiGroup.GET("/messages", wsCtrl.GetMessagesHandler)
apiGroup.GET("/messages/sync", wsCtrl.SyncMessagesHandler)
}
// 客户端 API(需要 Token 验证)
clientApiGroup := router.Group("/api")
clientApiGroup.Use(wsCtrl.APITokenAuthMiddleware())
{
clientApiGroup.POST("/send-to-user", wsCtrl.SendToUserHandler) // 客户端发送消息
}
// Open API(需要 HMAC 签名验证)
openApiGroup := router.Group("/open-api")
openApiGroup.Use(wsCtrl.OpenAPIAuthMiddleware())
{
openApiGroup.POST("/send-to-user", wsCtrl.SendToUserHandler)
}
}