Files
gocryptotrader/restful_router.go
Andrew d01e7bad72 Implement Logger (#228)
* Added new base logger

* updated example and test configs

* updated exchange helpers restful router & server

* logPath is now passed to the logger to remove dependency on common package

* updated everything besides exchanges to use new logger

* alphapoint to bitmex done

* updated bitmex bitstamp bittrex btcc and also performance changes to logger

* btcmarkets coinbase coinut exmo gateio wrappers updated

* gateio and gemini logger updated

* hitbtc huobi itbit & kraken updated

* All exchanges updatd

* return correct error for disabled websocket

* don't disconnect client on invalid json

* updated router internal logging

* log.Fatal to t.Error for tests

* Changed from fatal to error failure to set maxprocs

* output ANSI codes for everything but windows for now due to lack of windows support

* added error handling to logger and unit tests

* clear wording on print -> log.print

* added benchmark test

* cleaned up import sections

* Updated logger based on PR requests (added default config options on failure/setting errors)

* ah this should fix travici enc config issue

* Load entire config and clear out logging to hopefully fix travisci issue

* wording & test error handling

* fixed formatting issues based on feedback

* fixed formatting issues based on feedback

* changed CheckDir to use mkdirall instead of mkdir and other changes based on feedback
2019-01-08 21:56:22 +11:00

129 lines
2.5 KiB
Go

package main
import (
"fmt"
"net/http"
"time"
"github.com/gorilla/mux"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
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(exchanges []exchange.IBotExchange) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
routes = Routes{
Route{
"",
"GET",
"/",
getIndex,
},
Route{
"GetAllSettings",
"GET",
"/config/all",
RESTGetAllSettings,
},
Route{
"SaveAllSettings",
"POST",
"/config/all/save",
RESTSaveAllSettings,
},
Route{
"AllEnabledAccountInfo",
"GET",
"/exchanges/enabled/accounts/all",
RESTGetAllEnabledAccountInfo,
},
Route{
"AllActiveExchangesAndCurrencies",
"GET",
"/exchanges/enabled/latest/all",
RESTGetAllActiveTickers,
},
Route{
"IndividualExchangeAndCurrency",
"GET",
"/exchanges/{exchangeName}/latest/{currency}",
RESTGetTicker,
},
Route{
"GetPortfolio",
"GET",
"/portfolio/all",
RESTGetPortfolio,
},
Route{
"AllActiveExchangesAndOrderbooks",
"GET",
"/exchanges/orderbook/latest/all",
RESTGetAllActiveOrderbooks,
},
Route{
"IndividualExchangeOrderbook",
"GET",
"/exchanges/{exchangeName}/orderbook/latest/{currency}",
RESTGetOrderbook,
},
Route{
"ws",
"GET",
"/ws",
WebsocketClientHandler,
},
}
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
handler = RESTLogger(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
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)
}