-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
170 lines (147 loc) · 3.7 KB
/
node.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package node
import (
"bytes"
"encoding/json"
"errors"
"log"
"github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
"io/ioutil"
"net/http"
"fmt"
"time"
)
type (
ExternalNode struct {
ID string `json:"id" storm:"id"`
Name string `json:"name"`
Detail string `json:"detail"`
Url string `json:"url"`
Secret string `json:"secret"`
}
ExternalNodeInstance struct {
ID string `json:"id" storm:"id"`
NodeName string `json:"nodeName"`
Config interface{} `json:"nodeConfig"`
}
ExternalQuery struct {
*ExternalNode
*ExternalNodeInstance
}
)
func Health(c echo.Context) error {
return c.String(http.StatusOK, "I'm ok")
}
func Register(proxeusUrl, name, serviceUrl, jwtSecret, description string, retryInterval int) error {
client := http.Client{Timeout: 5 * time.Second}
var err error
for {
n := ExternalNode{
ID: name,
Name: name,
Detail: description,
Url: serviceUrl,
Secret: jwtSecret,
}
buf, err := json.Marshal(n)
if err != nil {
panic(err.Error())
}
r, err := client.Post(proxeusUrl+"/api/admin/external/register",
"application/json", bytes.NewBuffer(buf))
if err == nil && r.StatusCode == http.StatusOK {
log.Print("[nodeservice] ", n.Name, " registered")
return nil
}
log.Print("[nodeservice] error registering ", n.Name, " err ", err)
time.Sleep(time.Duration(retryInterval) * time.Second)
}
return err
}
func Nop(_ echo.Context) error {
return nil
}
func NodeID(c echo.Context) (string, error) {
id := c.Param("id")
if id == "" {
return "", errors.New("empty id")
}
t := c.Get("user").(*jwt.Token)
if id != t.Claims.(jwt.MapClaims)["jti"].(string) {
return "", errors.New("id mismatch")
}
return id, nil
}
func SetStoredConfig(c echo.Context, proxeusUrl string, conf interface{}) error {
id, err := NodeID(c)
if err != nil {
return err
}
n := ExternalNodeInstance{
ID: id,
Config: conf,
}
buf, err := json.Marshal(n)
if err != nil {
log.Print("[nodeservice] error marshalling config ", id, conf, " err ", err)
return err
}
client := http.Client{Timeout: 5 * time.Second}
_, err = client.Post(proxeusUrl+"/api/admin/external/config/"+n.ID,
"application/json", bytes.NewBuffer(buf))
if err != nil {
log.Print("[nodeservice] error updating config ", id, " err ", err)
return err
}
log.Print("[nodeservice] ", id, " config updated")
return nil
}
func GetStoredConfig(c echo.Context, proxeusUrl string) ([]byte, error) {
id, err := NodeID(c)
if err != nil {
return nil, err
}
client := http.Client{Timeout: 5 * time.Second}
r, err := client.Get(proxeusUrl + "/api/admin/external/config/" + id)
if err == nil && r.StatusCode == http.StatusOK {
jsonBody, _ := ioutil.ReadAll(r.Body)
return jsonBody, nil
}
return nil, errors.New("no valid config received")
}
func (e *ExternalNode) HealthUrl() string {
return fmt.Sprintf("%s/health", e.Url)
}
func (e ExternalQuery) jwtToken() string {
claims := jwt.StandardClaims{
Id: e.ExternalNodeInstance.ID,
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
t, err := token.SignedString([]byte(e.Secret))
if err != nil {
log.Println("Could not sign token with secret", token.Raw)
return ""
}
return t
}
func (e ExternalQuery) nodeUrl(method string) string {
return fmt.Sprintf("%s/node/%s/%s?auth=%s",
e.Url,
e.ExternalNodeInstance.ID,
method,
e.jwtToken(),
)
}
func (e ExternalQuery) ConfigUrl() string {
return e.nodeUrl("config")
}
func (e ExternalQuery) NextUrl() string {
return e.nodeUrl("next")
}
func (e ExternalQuery) RemoveUrl() string {
return e.nodeUrl("remove")
}
func (e ExternalQuery) CloseUrl() string {
return e.nodeUrl("close")
}