-
Notifications
You must be signed in to change notification settings - Fork 6
/
restful_router.go
141 lines (125 loc) · 3 KB
/
restful_router.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
package main
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/thrasher-corp/gocryptotrader/common"
log "github.com/thrasher-corp/gocryptotrader/logger"
_ "net/http/pprof"
)
// RESTLogger logs the requests internally
func RESTLogger(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
inner.ServeHTTP(w, r)
log.Debugf(
"%s\t%s\t%s\t%s",
r.Method,
r.RequestURI,
name,
time.Since(start),
)
})
}
// Route is a sub type that holds the request routes
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
// Routes is an array of all the registered routes
type Routes []Route
var routes = Routes{}
// NewRouter takes in the exchange interfaces and returns a new multiplexor
// router
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
var listenAddr string
if common.ExtractPort(bot.config.Webserver.ListenAddress) == 80 {
listenAddr = common.ExtractHost(bot.config.Webserver.ListenAddress)
} else {
listenAddr = common.JoinStrings([]string{common.ExtractHost(bot.config.Webserver.ListenAddress),
strconv.Itoa(common.ExtractPort(bot.config.Webserver.ListenAddress))}, ":")
}
routes = Routes{
Route{
"",
http.MethodGet,
"/",
getIndex,
},
Route{
"GetAllSettings",
http.MethodGet,
"/config/all",
RESTGetAllSettings,
},
Route{
"SaveAllSettings",
http.MethodPost,
"/config/all/save",
RESTSaveAllSettings,
},
Route{
"AllEnabledAccountInfo",
http.MethodGet,
"/exchanges/enabled/accounts/all",
RESTGetAllEnabledAccountInfo,
},
Route{
"AllActiveExchangesAndCurrencies",
http.MethodGet,
"/exchanges/enabled/latest/all",
RESTGetAllActiveTickers,
},
Route{
"IndividualExchangeAndCurrency",
http.MethodGet,
"/exchanges/{exchangeName}/latest/{currency}",
RESTGetTicker,
},
Route{
"GetPortfolio",
http.MethodGet,
"/portfolio/all",
RESTGetPortfolio,
},
Route{
"AllActiveExchangesAndOrderbooks",
http.MethodGet,
"/exchanges/orderbook/latest/all",
RESTGetAllActiveOrderbooks,
},
Route{
"IndividualExchangeOrderbook",
http.MethodGet,
"/exchanges/{exchangeName}/orderbook/latest/{currency}",
RESTGetOrderbook,
},
Route{
"ws",
http.MethodGet,
"/ws",
WebsocketClientHandler,
},
}
for _, route := range routes {
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(RESTLogger(route.HandlerFunc, route.Name)).
Host(listenAddr)
}
if bot.config.Profiler.Enabled {
log.Debugln("Profiler enabled")
router.PathPrefix("/debug").Handler(http.DefaultServeMux)
}
return router
}
func getIndex(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, "<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>")
}