forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiserver_types.go
168 lines (147 loc) · 5.38 KB
/
apiserver_types.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
package engine
import (
"errors"
"net/http"
"sync"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
)
// Const vars for websocket
const (
WebsocketResponseSuccess = "OK"
restIndexResponse = "<html>GoCryptoTrader RESTful interface. For the web GUI, please visit the <a href=https://github.com/thrasher-corp/gocryptotrader/blob/master/web/README.md>web GUI readme.</a></html>"
DeprecatedName = "deprecated_rpc"
WebsocketName = "websocket_rpc"
)
var (
wsHub *websocketHub
wsHubStarted bool
errNilRemoteConfig = errors.New("received nil remote config")
errNilPProfConfig = errors.New("received nil pprof config")
errNilBot = errors.New("received nil engine bot")
errEmptyConfigPath = errors.New("received empty config path")
errServerDisabled = errors.New("server disabled")
errAlreadyRunning = errors.New("already running")
// ErrWebsocketServiceNotRunning occurs when a message is sent to be broadcast via websocket
// and its not running
ErrWebsocketServiceNotRunning = errors.New("websocket service not started")
)
// apiServerManager holds all relevant fields to manage both REST and websocket
// api servers
type apiServerManager struct {
restStarted int32
websocketStarted int32
restListenAddress string
websocketListenAddress string
gctConfigPath string
restHTTPServer *http.Server
websocketHTTPServer *http.Server
wgRest sync.WaitGroup
wgWebsocket sync.WaitGroup
restRouter *mux.Router
websocketRouter *mux.Router
websocketHub *websocketHub
remoteConfig *config.RemoteControlConfig
pprofConfig *config.Profiler
exchangeManager iExchangeManager
bot iBot
portfolioManager iPortfolioManager
}
// websocketClient stores information related to the websocket client
type websocketClient struct {
Hub *websocketHub
Conn *websocket.Conn
Authenticated bool
authFailures int
Send chan []byte
username string
password string
maxAuthFailures int
exchangeManager iExchangeManager
bot iBot
portfolioManager iPortfolioManager
configPath string
}
// websocketHub stores the data for managing websocket clients
type websocketHub struct {
Clients map[*websocketClient]bool
Broadcast chan []byte
Register chan *websocketClient
Unregister chan *websocketClient
}
// WebsocketEvent is the struct used for websocket events
type WebsocketEvent struct {
Exchange string `json:"exchange,omitempty"`
AssetType string `json:"assetType,omitempty"`
Event string
Data interface{}
}
// WebsocketEventResponse is the struct used for websocket event responses
type WebsocketEventResponse struct {
Event string `json:"event"`
Data interface{} `json:"data"`
Error string `json:"error"`
}
// WebsocketOrderbookTickerRequest is a struct used for ticker and orderbook
// requests
type WebsocketOrderbookTickerRequest struct {
Exchange string `json:"exchangeName"`
Currency string `json:"currency"`
AssetType string `json:"assetType"`
}
// WebsocketAuth is a struct used for
type WebsocketAuth struct {
Username string `json:"username"`
Password string `json:"password"`
}
// Route is a sub type that holds the request routes
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
// AllEnabledExchangeOrderbooks holds the enabled exchange orderbooks
type AllEnabledExchangeOrderbooks struct {
Data []EnabledExchangeOrderbooks `json:"data"`
}
// EnabledExchangeOrderbooks is a sub type for singular exchanges and respective
// orderbooks
type EnabledExchangeOrderbooks struct {
ExchangeName string `json:"exchangeName"`
ExchangeValues []orderbook.Base `json:"exchangeValues"`
}
// AllEnabledExchangeCurrencies holds the enabled exchange currencies
type AllEnabledExchangeCurrencies struct {
Data []EnabledExchangeCurrencies `json:"data"`
}
// EnabledExchangeCurrencies is a sub type for singular exchanges and respective
// currencies
type EnabledExchangeCurrencies struct {
ExchangeName string `json:"exchangeName"`
ExchangeValues []ticker.Price `json:"exchangeValues"`
}
// AllEnabledExchangeAccounts holds all enabled accounts info
type AllEnabledExchangeAccounts struct {
Data []account.Holdings `json:"data"`
}
var wsHandlers = map[string]wsCommandHandler{
"auth": {authRequired: false, handler: wsAuth},
"getconfig": {authRequired: true, handler: wsGetConfig},
"saveconfig": {authRequired: true, handler: wsSaveConfig},
"getaccountinfo": {authRequired: true, handler: wsGetAccountInfo},
"gettickers": {authRequired: false, handler: wsGetTickers},
"getticker": {authRequired: false, handler: wsGetTicker},
"getorderbooks": {authRequired: false, handler: wsGetOrderbooks},
"getorderbook": {authRequired: false, handler: wsGetOrderbook},
"getexchangerates": {authRequired: false, handler: wsGetExchangeRates},
"getportfolio": {authRequired: true, handler: wsGetPortfolio},
}
type wsCommandHandler struct {
authRequired bool
handler func(client *websocketClient, data interface{}) error
}