-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
126 lines (110 loc) · 2.62 KB
/
http.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package registry
import (
"net"
"net/http"
"github.com/gin-gonic/gin"
"github.com/werbenhu/chash"
)
// Http represents the http server object
type Http struct {
addr string // the address that http server listens to
listener net.Listener // the listener for the http server
}
// NewHttp returns a new Http object
func NewHttp() *Http {
return &Http{}
}
// match assigns a service to a key using consistent hashing algorithm
func (h *Http) match(c *gin.Context) {
name := c.Query("group")
key := c.Query("key")
// Get the group based on the provided name
group, err := chash.GetGroup(name)
if err != nil {
// Return error response if group not found
c.JSON(http.StatusOK, gin.H{
"code": 1,
"msg": err.Error(),
})
return
}
// Match the key with a member in the group
_, payload, err := group.Match(key)
if err != nil {
// Return error response if key not found
c.JSON(http.StatusOK, gin.H{
"code": 2,
"msg": err.Error(),
})
return
}
// Unmarshal the member payload
m := &Member{}
if err := m.Unmarshal(payload); err != nil {
// Return error response if payload cannot be unmarshalled
c.JSON(http.StatusOK, gin.H{
"code": 3,
"msg": err.Error(),
})
return
}
// Return success response with the matched service
c.JSON(http.StatusOK, gin.H{
"code": 0,
"msg": "success",
"data": gin.H{
"service": m.Service,
},
})
}
// members returns the list of services for a group
func (h *Http) members(c *gin.Context) {
name := c.Query("group")
// Get the group based on the provided name
group, err := chash.GetGroup(name)
if err != nil {
// Return error response if group not found
c.JSON(http.StatusOK, gin.H{
"code": 1,
"msg": err.Error(),
})
return
}
// Get all the elements in the group and extract their services
elements := group.GetElements()
services := make([]Service, 0)
for _, element := range elements {
m := &Member{}
if err := m.Unmarshal(element.Payload); err == nil {
services = append(services, m.Service)
}
}
// Return success response with the list of services
c.JSON(http.StatusOK, gin.H{
"code": 0,
"msg": "success",
"data": gin.H{
"services": services,
},
})
}
// Start starts the http server
func (h *Http) Start(addr string) error {
var err error
h.addr = addr
r := gin.Default()
r.GET("/match", h.match)
r.GET("/members", h.members)
// Listen on the provided address and run the http server
h.listener, err = net.Listen("tcp", h.addr)
if err != nil {
return err
}
return r.RunListener(h.listener)
}
// Stop stops the http server
func (h *Http) Stop() {
if h.listener != nil {
h.listener.Close()
}
}