-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_server.go
316 lines (272 loc) · 10.1 KB
/
api_server.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package backend
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/google/uuid"
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/kelp/plugins"
"github.com/stellar/kelp/support/kelpos"
)
// UserData is the json data passed in to represent a user
type UserData struct {
ID string `json:"id"`
}
// toUser converts to the format understood by kelpos
func (u UserData) toUser() *kelpos.User {
return kelpos.MakeUser(u.ID)
}
// String is the stringer method
func (u UserData) String() string {
return fmt.Sprintf("UserData[ID=%s]", u.ID)
}
// kelpErrorDataForUser tracks errors for a given user
type kelpErrorDataForUser struct {
errorMap map[string]KelpError
lock *sync.Mutex
}
// APIServer is an instance of the API service
type APIServer struct {
kelpBinPath *kelpos.OSPath
botConfigsPath *kelpos.OSPath
botLogsPath *kelpos.OSPath
kos *kelpos.KelpOS
horizonTestnetURI string
horizonPubnetURI string
ccxtRestUrl string
apiTestNet *horizonclient.Client
apiPubNet *horizonclient.Client
disablePubnet bool
enableKaas bool
noHeaders bool
quitFn func()
metricsTracker *plugins.MetricsTracker
kelpErrorsByUser map[string]kelpErrorDataForUser
kelpErrorsByUserLock *sync.Mutex
cachedOptionsMetadata metadata
}
// MakeAPIServer is a factory method
func MakeAPIServer(
kos *kelpos.KelpOS,
botConfigsPath *kelpos.OSPath,
botLogsPath *kelpos.OSPath,
horizonTestnetURI string,
apiTestNet *horizonclient.Client,
horizonPubnetURI string,
apiPubNet *horizonclient.Client,
ccxtRestUrl string,
disablePubnet bool,
enableKaas bool,
noHeaders bool,
quitFn func(),
metricsTracker *plugins.MetricsTracker,
) (*APIServer, error) {
kelpBinPath := kos.GetBinDir().Join(filepath.Base(os.Args[0]))
optionsMetadata, e := loadOptionsMetadata()
if e != nil {
return nil, fmt.Errorf("error while loading options metadata when making APIServer: %s", e)
}
return &APIServer{
kelpBinPath: kelpBinPath,
botConfigsPath: botConfigsPath,
botLogsPath: botLogsPath,
kos: kos,
horizonTestnetURI: horizonTestnetURI,
horizonPubnetURI: horizonPubnetURI,
ccxtRestUrl: ccxtRestUrl,
apiTestNet: apiTestNet,
apiPubNet: apiPubNet,
disablePubnet: disablePubnet,
enableKaas: enableKaas,
noHeaders: noHeaders,
cachedOptionsMetadata: optionsMetadata,
quitFn: quitFn,
metricsTracker: metricsTracker,
kelpErrorsByUser: map[string]kelpErrorDataForUser{},
kelpErrorsByUserLock: &sync.Mutex{},
}, nil
}
func (s *APIServer) botConfigsPathForUser(userID string) *kelpos.OSPath {
return s.botConfigsPath.Join(userID)
}
func (s *APIServer) botLogsPathForUser(userID string) *kelpos.OSPath {
return s.botLogsPath.Join(userID)
}
func (s *APIServer) kelpErrorsForUser(userID string) kelpErrorDataForUser {
s.kelpErrorsByUserLock.Lock()
defer s.kelpErrorsByUserLock.Unlock()
var kefu kelpErrorDataForUser
if v, ok := s.kelpErrorsByUser[userID]; ok {
kefu = v
} else {
// create new value and insert in map
kefu = kelpErrorDataForUser{
errorMap: map[string]KelpError{},
lock: &sync.Mutex{},
}
s.kelpErrorsByUser[userID] = kefu
}
return kefu
}
// InitBackend initializes anything required to get the backend ready to serve
func (s *APIServer) InitBackend() error {
// do not do an initial load of bots into memory for now since it's based on the user context which we don't have right now
// and we don't want to do it for all users right now
return nil
}
func (s *APIServer) parseBotName(r *http.Request) (string, error) {
botNameBytes, e := ioutil.ReadAll(r.Body)
if e != nil {
return "", fmt.Errorf("error when reading request input: %s", e)
}
return string(botNameBytes), nil
}
func (s *APIServer) writeError(w http.ResponseWriter, message string) {
log.Print(message)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(message))
}
// ErrorResponse represents an error (deprecated)
type ErrorResponse struct {
Error string `json:"error"`
}
// KelpError represents an error
type KelpError struct {
UUID string `json:"uuid"`
ObjectType errorType `json:"object_type"`
ObjectName string `json:"object_name"`
Date time.Time `json:"date"`
Level errorLevel `json:"level"`
Message string `json:"message"`
}
// String is the Stringer method
func (ke *KelpError) String() string {
return fmt.Sprintf("KelpError[UUID=%s, objectType=%s, objectName=%s, date=%s, level=%s, message=%s]", ke.UUID, ke.ObjectType, ke.ObjectName, ke.Date.Format("20060102T150405MST"), ke.Level, ke.Message)
}
// KelpErrorResponseWrapper is the outer object that contains the Kelp Error
type KelpErrorResponseWrapper struct {
KelpError KelpError `json:"kelp_error"`
}
func makeKelpErrorResponseWrapper(
objectType errorType,
objectName string,
date time.Time,
level errorLevel,
message string,
) KelpErrorResponseWrapper {
uuid, e := uuid.NewRandom()
if e != nil {
// TODO NS - panic here instead of returning and handling smoothly because interface is a lot cleaner without returning error
// need to find a better solution that does not require a panic
panic(fmt.Errorf("unable to generate new uuid: %s", e))
}
return KelpErrorResponseWrapper{
KelpError: KelpError{
UUID: uuid.String(),
ObjectType: objectType,
ObjectName: objectName,
Date: date,
Level: level,
Message: message,
},
}
}
// String is the Stringer method
func (kerw *KelpErrorResponseWrapper) String() string {
return fmt.Sprintf("KelpErrorResponseWrapper[kelp_error=%s]", kerw.KelpError.String())
}
func (s *APIServer) writeErrorJson(w http.ResponseWriter, message string) {
log.Println(message)
w.WriteHeader(http.StatusInternalServerError)
marshalledJson, e := json.MarshalIndent(ErrorResponse{Error: message}, "", " ")
if e != nil {
log.Printf("unable to marshal json with indentation: %s\n", e)
w.Write([]byte(fmt.Sprintf("unable to marshal json with indentation: %s\n", e)))
return
}
w.Write(marshalledJson)
}
func (s *APIServer) addKelpErrorToMap(userData UserData, ke KelpError) {
key := ke.UUID
kefu := s.kelpErrorsForUser(userData.ID)
// need to use a lock because we could encounter a "concurrent map writes" error against the map which is being updated by multiple threads
kefu.lock.Lock()
defer kefu.lock.Unlock()
kefu.errorMap[key] = ke
}
// removeKelpErrorUserDataIfEmpty removes user error data if the underlying map is empty
func (s *APIServer) removeKelpErrorUserDataIfEmpty(userData UserData) {
// issue with this is that someone can hold a reference to this object when it is empty
// and then we remove from parent map and the other thread will add a value, which would result
// in the object having an entry in the map but being orphaned.
//
// We can get creative with timeouts too but that is all an overoptimizationn
//
// We could resolve this by always holding both the higher level lock and the per-user lock to modify
// values inside a user's error map, but that will slow things down
//
// for now we do not remove Kelp error user data even if empty.
// do nothing
}
func (s *APIServer) writeKelpError(userData UserData, w http.ResponseWriter, kerw KelpErrorResponseWrapper) {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("writing error: %s\n", kerw.String())
s.addKelpErrorToMap(userData, kerw.KelpError)
marshalledJSON, e := json.MarshalIndent(kerw, "", " ")
if e != nil {
log.Printf("unable to marshal json with indentation: %s\n", e)
w.Write([]byte(fmt.Sprintf("unable to marshal json with indentation: %s\n", e)))
return
}
w.Write(marshalledJSON)
}
func (s *APIServer) writeJson(w http.ResponseWriter, v interface{}) {
s.writeJsonWithLog(w, v, true)
}
func (s *APIServer) writeJsonWithLog(w http.ResponseWriter, v interface{}, doLog bool) {
marshalledJson, e := json.MarshalIndent(v, "", " ")
if e != nil {
s.writeErrorJson(w, fmt.Sprintf("unable to marshal json with indentation: %s", e))
return
}
if doLog {
log.Printf("responseJson: %s\n", string(marshalledJson))
}
w.WriteHeader(http.StatusOK)
w.Write(marshalledJson)
}
func (s *APIServer) runKelpCommandBlocking(userID string, namespace string, cmd string) ([]byte, error) {
// There is a weird issue on windows where the absolute path for the kelp binary does not work on the release GUI
// version because of the unzipped directory name but it will work on the released cli version or if we change the
// name of the folder in which the GUI version is unzipped.
// To avoid these issues we only invoke with the binary name as opposed to the absolute path that contains the
// directory name. see start_bot.go for some experimentation with absolute and relative paths
cmdString := fmt.Sprintf("%s %s", s.kelpBinPath.Unix(), cmd)
return s.kos.Blocking(userID, namespace, cmdString)
}
func (s *APIServer) runKelpCommandBackground(userID string, namespace string, cmd string) (*kelpos.Process, error) {
// There is a weird issue on windows where the absolute path for the kelp binary does not work on the release GUI
// version because of the unzipped directory name but it will work on the released cli version or if we change the
// name of the folder in which the GUI version is unzipped.
// To avoid these issues we only invoke with the binary name as opposed to the absolute path that contains the
// directory name. see start_bot.go for some experimentation with absolute and relative paths
cmdString := fmt.Sprintf("%s %s", s.kelpBinPath.Unix(), cmd)
return s.kos.Background(userID, namespace, cmdString)
}
func (s *APIServer) setupOpsDirectory(userID string) error {
e := s.kos.Mkdir(userID, s.botConfigsPathForUser(userID))
if e != nil {
return fmt.Errorf("error setting up configs directory (%s): %s", s.botConfigsPathForUser(userID).Native(), e)
}
e = s.kos.Mkdir(userID, s.botLogsPathForUser(userID))
if e != nil {
return fmt.Errorf("error setting up logs directory (%s): %s", s.botLogsPathForUser(userID).Native(), e)
}
return nil
}