-
Notifications
You must be signed in to change notification settings - Fork 264
/
api_route.go
65 lines (56 loc) · 1.5 KB
/
api_route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package server
import (
"net/http"
"github.com/WuKongIM/WuKongIM/pkg/wkhttp"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"go.uber.org/zap"
)
// 这个主要为了模拟proxy模式。
type RouteAPI struct {
s *Server
wklog.Log
}
// NewRouteAPI NewRouteAPI
func NewRouteAPI(s *Server) *RouteAPI {
return &RouteAPI{
s: s,
}
}
// Route Route
func (a *RouteAPI) Route(r *wkhttp.WKHttp) {
r.GET("/route", a.routeUserIMAddr) // 获取用户所在节点的连接信息
r.POST("/route/batch", a.routeUserIMAddrOfBatch) // 批量获取用户所在节点的连接信息
}
// 路由用户的IM连接地址
func (a *RouteAPI) routeUserIMAddr(c *wkhttp.Context) {
c.JSON(http.StatusOK, gin.H{
"tcp_addr": a.s.opts.External.TCPAddr,
"ws_addr": a.s.opts.External.WSAddr,
"wss_addr": a.s.opts.External.WSSAddr,
})
}
// 批量获取用户所在节点地址
func (a *RouteAPI) routeUserIMAddrOfBatch(c *wkhttp.Context) {
var uids []string
if err := c.BindJSON(&uids); err != nil {
a.Error("数据格式有误!", zap.Error(err))
c.ResponseError(errors.New("数据格式有误!"))
return
}
c.JSON(http.StatusOK, []userAddrResp{
{
UIDs: uids,
TCPAddr: a.s.opts.External.TCPAddr,
WSAddr: a.s.opts.External.WSAddr,
WSSAddr: a.s.opts.External.WSSAddr,
},
})
}
type userAddrResp struct {
TCPAddr string `json:"tcp_addr"`
WSAddr string `json:"ws_addr"`
WSSAddr string `json:"wss_addr"`
UIDs []string `json:"uids"`
}