Files
gocryptotrader/restful_router.go
Adrian Gallagher 5e5ca8a887 Linter fixes (#246)
Linter fixes
2019-02-05 16:26:04 +11:00

124 lines
2.4 KiB
Go

package main
import (
"fmt"
"net/http"
"time"
"github.com/gorilla/mux"
log "github.com/thrasher-/gocryptotrader/logger"
)
// 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)
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))
}
return router
}
func getIndex(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<html>GoCryptoTrader RESTful interface. For the web GUI, please visit the <a href=https://github.com/thrasher-/gocryptotrader/blob/master/web/README.md>web GUI readme.</a></html>")
w.WriteHeader(http.StatusOK)
}