-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
marketcap_request.go
269 lines (223 loc) · 7.22 KB
/
marketcap_request.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
package main
import (
"database/sql"
"encoding/json"
"io"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
)
// ImportMarketCap pulls in bots from the provided db
func (m *Manager) ImportMarketCap() {
// query
rows, err := m.DB.Query("SELECT clientID, token, ticker, name, nickname, color, activity, decorator, decimals, currency, currencySymbol, frequency FROM marketcaps")
if err != nil {
logger.Warningf("Unable to query marketcaps in db: %s", err)
return
}
// load existing bots from db
for rows.Next() {
var importedMarketCap MarketCap
err = rows.Scan(&importedMarketCap.ClientID, &importedMarketCap.Token, &importedMarketCap.Ticker, &importedMarketCap.Name, &importedMarketCap.Nickname, &importedMarketCap.Color, &importedMarketCap.Activity, &importedMarketCap.Decorator, &importedMarketCap.Decimals, &importedMarketCap.Currency, &importedMarketCap.CurrencySymbol, &importedMarketCap.Frequency)
if err != nil {
logger.Errorf("Unable to load marketcaps from db: %s", err)
continue
}
// activate bot
go importedMarketCap.watchMarketCap()
m.WatchMarketCap(&importedMarketCap)
logger.Infof("Loaded marketcap from db: %s", importedMarketCap.label())
}
rows.Close()
// check all entries have id
for _, marketcap := range m.WatchingMarketCap {
if marketcap.ClientID == "" {
id, err := getIDToken(marketcap.Token)
if err != nil {
logger.Errorf("Unable to get id from token: %s", err)
continue
}
stmt, err := m.DB.Prepare("UPDATE marketcaps SET clientId = ? WHERE token = ?")
if err != nil {
logger.Errorf("Unable to prepare id update: %s", err)
continue
}
res, err := stmt.Exec(id, marketcap.Token)
if err != nil {
logger.Errorf("Unable to update db: %s", err)
continue
}
_, err = res.LastInsertId()
if err != nil {
logger.Errorf("Unable to confirm db update: %s", err)
continue
} else {
logger.Infof("Updated id in db for %s", marketcap.label())
marketcap.ClientID = id
}
}
}
}
// AddMarketCap adds a new MarketCap or crypto to the list of what to watch
func (m *Manager) AddMarketCap(w http.ResponseWriter, r *http.Request) {
m.Lock()
defer m.Unlock()
logger.Debugf("Got an API request to add a MarketCap")
// read body
body, err := io.ReadAll(r.Body)
if err != nil {
logger.Errorf("%s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// unmarshal into struct
var marketCapReq MarketCap
if err := json.Unmarshal(body, &marketCapReq); err != nil {
logger.Errorf("Unmarshalling: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// ensure token is set
if marketCapReq.Token == "" {
logger.Error("Discord token required")
w.WriteHeader(http.StatusBadRequest)
return
}
// make sure token is valid
if marketCapReq.ClientID == "" {
id, err := getIDToken(marketCapReq.Token)
if err != nil {
logger.Errorf("Unable to authenticate with discord token: %s", err)
w.WriteHeader(http.StatusBadRequest)
return
}
marketCapReq.ClientID = id
}
// ensure frequency is set
if marketCapReq.Frequency <= 0 {
marketCapReq.Frequency = 60
}
// ensure currency is set
if marketCapReq.Currency == "" {
marketCapReq.Currency = "usd"
}
// ensure name is set
if marketCapReq.Name == "" {
logger.Error("Name required for crypto")
w.WriteHeader(http.StatusBadRequest)
return
}
// ensure currency is set
if marketCapReq.CurrencySymbol == "" {
marketCapReq.CurrencySymbol = "$"
}
// check if already existing
if _, ok := m.WatchingMarketCap[marketCapReq.label()]; ok {
logger.Error("MarketCap already exists")
w.WriteHeader(http.StatusConflict)
return
}
go marketCapReq.watchMarketCap()
m.WatchMarketCap(&marketCapReq)
if *db != "" {
m.StoreMarketcap(&marketCapReq)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(marketCapReq)
if err != nil {
logger.Errorf("Unable to encode marketcaps: %s", err)
w.WriteHeader(http.StatusBadRequest)
}
logger.Infof("Added marketcap: %s\n", marketCapReq.Name)
}
func (m *Manager) WatchMarketCap(marketcap *MarketCap) {
marketcapCount.Inc()
id := marketcap.label()
m.WatchingMarketCap[id] = marketcap
}
// StoreTicker puts a marketcap into the db
func (m *Manager) StoreMarketcap(marketcap *MarketCap) {
// store new entry in db
stmt, err := m.DB.Prepare("INSERT INTO marketcaps(clientId, token, ticker, name, nickname, color, activity, decorator, decimals, currency, currencySymbol, frequency) values(?,?,?,?,?,?,?,?,?,?,?,?)")
if err != nil {
logger.Warningf("Unable to store marketcap in db %s: %s", marketcap.label(), err)
return
}
res, err := stmt.Exec(marketcap.ClientID, marketcap.Token, marketcap.Ticker, marketcap.Name, marketcap.Nickname, marketcap.Color, marketcap.Activity, marketcap.Decorator, marketcap.Decimals, marketcap.Currency, marketcap.CurrencySymbol, marketcap.Frequency)
if err != nil {
logger.Warningf("Unable to store marketcap in db %s: %s", marketcap.label(), err)
return
}
_, err = res.LastInsertId()
if err != nil {
logger.Warningf("Unable to store marketcap in db %s: %s", marketcap.label(), err)
return
}
}
// DeleteMarketCap addds a new MarketCap or crypto to the list of what to watch
func (m *Manager) DeleteMarketCap(w http.ResponseWriter, r *http.Request) {
m.Lock()
defer m.Unlock()
logger.Debugf("Got an API request to delete a marketcap")
vars := mux.Vars(r)
id := vars["id"]
if _, ok := m.WatchingMarketCap[id]; !ok {
logger.Errorf("No marketcap found: %s", id)
w.WriteHeader(http.StatusNotFound)
return
}
// send shutdown sign
m.WatchingMarketCap[id].Shutdown()
marketcapCount.Dec()
var noDB *sql.DB
if m.DB != noDB {
// remove from db
stmt, err := m.DB.Prepare("DELETE FROM marketcaps WHERE name = ?")
if err != nil {
logger.Warningf("Unable to query marketcap in db %s: %s", id, err)
return
}
_, err = stmt.Exec(m.WatchingMarketCap[id].Name)
if err != nil {
logger.Warningf("Unable to query marketcap in db %s: %s", id, err)
return
}
}
// remove from cache
delete(m.WatchingMarketCap, id)
logger.Infof("Deleted marketcap %s", id)
w.WriteHeader(http.StatusNoContent)
}
// RestartMarketCap stops and starts a marketcap
func (m *Manager) RestartMarketCap(w http.ResponseWriter, r *http.Request) {
m.Lock()
defer m.Unlock()
logger.Debugf("Got an API request to restart a marketcap")
vars := mux.Vars(r)
id := strings.ToUpper(vars["id"])
if _, ok := m.WatchingMarketCap[id]; !ok {
logger.Errorf("No marketcap found: %s", id)
w.WriteHeader(http.StatusNotFound)
return
}
// send shutdown sign
m.WatchingMarketCap[id].Shutdown()
// wait twice the update time
time.Sleep(time.Duration(m.WatchingMarketCap[id].Frequency) * 2 * time.Second)
// start the marketcap again
go m.WatchingMarketCap[id].watchMarketCap()
logger.Infof("Restarted marketcap %s", id)
w.WriteHeader(http.StatusNoContent)
}
// GetMarketCaps returns a list of what the manager is watching
func (m *Manager) GetMarketCaps(w http.ResponseWriter, r *http.Request) {
m.RLock()
defer m.RUnlock()
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(m.WatchingMarketCap); err != nil {
logger.Errorf("Serving request: %s", err)
}
}