forked from oss-jtyd/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestful_router.go
118 lines (105 loc) · 3.73 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
package engine
import (
"fmt"
"net/http"
"net/http/pprof"
"runtime"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/log"
)
// 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(log.RESTSys,
"%s\t%s\t%s\t%s",
r.Method,
r.RequestURI,
name,
time.Since(start),
)
})
}
// StartRESTServer starts a REST server
func StartRESTServer(bot *Engine) {
listenAddr := bot.Config.RemoteControl.DeprecatedRPC.ListenAddress
log.Debugf(log.RESTSys,
"Deprecated RPC server support enabled. Listen URL: http://%s:%d\n",
common.ExtractHost(listenAddr), common.ExtractPort(listenAddr))
err := http.ListenAndServe(listenAddr, newRouter(bot, true))
if err != nil {
log.Errorf(log.RESTSys, "Failed to start deprecated RPC server. Err: %s", err)
}
}
// StartWebsocketServer starts a Websocket server
func StartWebsocketServer(bot *Engine) {
listenAddr := bot.Config.RemoteControl.WebsocketRPC.ListenAddress
log.Debugf(log.RESTSys,
"Websocket RPC support enabled. Listen URL: ws://%s:%d/ws\n",
common.ExtractHost(listenAddr), common.ExtractPort(listenAddr))
err := http.ListenAndServe(listenAddr, newRouter(bot, false))
if err != nil {
log.Errorf(log.RESTSys, "Failed to start websocket RPC server. Err: %s", err)
}
}
// newRouter takes in the exchange interfaces and returns a new multiplexor
// router
func newRouter(bot *Engine, isREST bool) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
var routes []Route
var listenAddr string
if isREST {
listenAddr = bot.Config.RemoteControl.DeprecatedRPC.ListenAddress
} else {
listenAddr = bot.Config.RemoteControl.WebsocketRPC.ListenAddress
}
if common.ExtractPort(listenAddr) == 80 {
listenAddr = common.ExtractHost(listenAddr)
} else {
listenAddr = strings.Join([]string{common.ExtractHost(listenAddr),
strconv.Itoa(common.ExtractPort(listenAddr))}, ":")
}
if isREST {
routes = []Route{
{"", http.MethodGet, "/", getIndex},
{"GetAllSettings", http.MethodGet, "/config/all", RESTGetAllSettings},
{"SaveAllSettings", http.MethodPost, "/config/all/save", RESTSaveAllSettings},
{"AllEnabledAccountInfo", http.MethodGet, "/exchanges/enabled/accounts/all", RESTGetAllEnabledAccountInfo},
{"AllActiveExchangesAndCurrencies", http.MethodGet, "/exchanges/enabled/latest/all", RESTGetAllActiveTickers},
{"GetPortfolio", http.MethodGet, "/portfolio/all", RESTGetPortfolio},
{"AllActiveExchangesAndOrderbooks", http.MethodGet, "/exchanges/orderbook/latest/all", RESTGetAllActiveOrderbooks},
}
if bot.Config.Profiler.Enabled {
if bot.Config.Profiler.MutexProfileFraction > 0 {
runtime.SetMutexProfileFraction(bot.Config.Profiler.MutexProfileFraction)
}
log.Debugf(log.RESTSys,
"HTTP Go performance profiler (pprof) endpoint enabled: http://%s:%d/debug/pprof/\n",
common.ExtractHost(listenAddr),
common.ExtractPort(listenAddr))
router.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
}
} else {
routes = []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)
}
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>")
w.WriteHeader(http.StatusOK)
}