forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_routes.go
74 lines (68 loc) · 1.98 KB
/
config_routes.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
package main
import (
"encoding/json"
"net/http"
"github.com/thrasher-/gocryptotrader/config"
)
// GetAllSettings replies to a request with an encoded JSON response about the
// trading bots configuration.
func GetAllSettings(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(bot.config); err != nil {
panic(err)
}
}
// SaveAllSettings saves all current settings from request body as a JSON
// document then reloads state and returns the settings
func SaveAllSettings(w http.ResponseWriter, r *http.Request) {
//Get the data from the request
decoder := json.NewDecoder(r.Body)
var responseData config.Post
jsonerr := decoder.Decode(&responseData)
if jsonerr != nil {
panic(jsonerr)
}
//Save change the settings
for x := range bot.config.Exchanges {
for i := 0; i < len(responseData.Data.Exchanges); i++ {
if responseData.Data.Exchanges[i].Name == bot.config.Exchanges[x].Name {
bot.config.Exchanges[x].Enabled = responseData.Data.Exchanges[i].Enabled
bot.config.Exchanges[x].APIKey = responseData.Data.Exchanges[i].APIKey
bot.config.Exchanges[x].APISecret = responseData.Data.Exchanges[i].APISecret
bot.config.Exchanges[x].EnabledPairs = responseData.Data.Exchanges[i].EnabledPairs
}
}
}
//Reload the configuration
err := bot.config.SaveConfig(bot.configFile)
if err != nil {
panic(err)
}
err = bot.config.LoadConfig(bot.configFile)
if err != nil {
panic(err)
}
setupBotExchanges()
//Return response status
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(bot.config); err != nil {
panic(err)
}
}
// ConfigRoutes declares the current routes for config_routes.go
var ConfigRoutes = Routes{
Route{
"GetAllSettings",
"GET",
"/config/all",
GetAllSettings,
},
Route{
"SaveAllSettings",
"POST",
"/config/all/save",
SaveAllSettings,
},
}