Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
update headers for use with UI, closes #299
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsaraf committed Sep 23, 2019
1 parent 3fd3cd9 commit a860208
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 28 deletions.
36 changes: 35 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"github.com/go-chi/chi/middleware"
"github.com/rs/cors"
"github.com/spf13/cobra"
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/kelp/gui"
"github.com/stellar/kelp/gui/backend"
"github.com/stellar/kelp/support/kelpos"
"github.com/stellar/kelp/support/prefs"
)

var serverCmd = &cobra.Command{
Expand All @@ -29,6 +31,7 @@ type serverInputs struct {
devAPIPort *uint16
horizonTestnetURI *string
horizonPubnetURI *string
noHeaders *bool
}

func init() {
Expand All @@ -38,6 +41,7 @@ func init() {
options.devAPIPort = serverCmd.Flags().Uint16("dev-api-port", 8001, "port on which to run API server when in dev mode")
options.horizonTestnetURI = serverCmd.Flags().String("horizon-testnet-uri", "https://horizon-testnet.stellar.org", "URI to use for the horizon instance connected to the Stellar Test Network (must contain the word 'test')")
options.horizonPubnetURI = serverCmd.Flags().String("horizon-pubnet-uri", "https://horizon.stellar.org", "URI to use for the horizon instance connected to the Stellar Public Network (must not contain the word 'test')")
options.noHeaders = serverCmd.Flags().Bool("no-headers", false, "do not set X-App-Name and X-App-Version headers on requests to horizon")

serverCmd.Run = func(ccmd *cobra.Command, args []string) {
log.Printf("Starting Kelp GUI Server: %s [%s]\n", version, gitHash)
Expand All @@ -51,7 +55,37 @@ func init() {
}

kos := kelpos.GetKelpOS()
s, e := backend.MakeAPIServer(kos, *options.horizonTestnetURI, *options.horizonPubnetURI, *rootCcxtRestURL)
horizonTestnetURI := strings.TrimSuffix(*options.horizonTestnetURI, "/")
horizonPubnetURI := strings.TrimSuffix(*options.horizonPubnetURI, "/")
log.Printf("using horizonTestnetURI: %s\n", horizonTestnetURI)
log.Printf("using horizonPubnetURI: %s\n", horizonPubnetURI)
log.Printf("using ccxtRestUrl: %s\n", *rootCcxtRestURL)
apiTestNet := &horizonclient.Client{
HorizonURL: horizonTestnetURI,
HTTP: http.DefaultClient,
}
apiPubNet := &horizonclient.Client{
HorizonURL: horizonPubnetURI,
HTTP: http.DefaultClient,
}
if !*options.noHeaders {
apiTestNet.AppName = "kelp-ui"
apiTestNet.AppVersion = version
apiPubNet.AppName = "kelp-ui"
apiPubNet.AppVersion = version

p := prefs.Make(prefsFilename)
if p.FirstTime() {
log.Printf("Kelp sets the `X-App-Name` and `X-App-Version` headers on requests made to Horizon. These headers help us track overall Kelp usage, so that we can learn about general usage patterns and adapt Kelp to be more useful in the future. These can be turned off using the `--no-headers` flag. See `kelp trade --help` for more information.\n")
e := p.SetNotFirstTime()
if e != nil {
log.Println("")
log.Printf("unable to create preferences file: %s", e)
// we can still proceed with this error
}
}
}
s, e := backend.MakeAPIServer(kos, *options.horizonTestnetURI, apiTestNet, *options.horizonPubnetURI, apiPubNet, *rootCcxtRestURL, *options.noHeaders)
if e != nil {
panic(e)
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type inputs struct {
logPrefix *string
fixedIterations *uint64
noHeaders *bool
ui *bool
}

func validateCliParams(l logger.Logger, options inputs) {
Expand Down Expand Up @@ -130,11 +131,13 @@ func init() {
options.logPrefix = tradeCmd.Flags().StringP("log", "l", "", "log to a file (and stdout) with this prefix for the filename")
options.fixedIterations = tradeCmd.Flags().Uint64("iter", 0, "only run the bot for the first N iterations (defaults value 0 runs unboundedly)")
options.noHeaders = tradeCmd.Flags().Bool("no-headers", false, "do not set X-App-Name and X-App-Version headers on requests to horizon")
options.ui = tradeCmd.Flags().Bool("ui", false, "indicates a bot that is started from the Kelp UI server")

requiredFlag("botConf")
requiredFlag("strategy")
hiddenFlag("operationalBuffer")
hiddenFlag("operationalBufferNonNativePct")
hiddenFlag("ui")
tradeCmd.Flags().SortFlags = false

tradeCmd.Run = func(ccmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -402,6 +405,9 @@ func runTradeCmd(options inputs) {
}
if !*options.noHeaders {
client.AppName = "kelp"
if *options.ui {
client.AppName = "kelp-ui"
}
client.AppVersion = version

p := prefs.Make(prefsFilename)
Expand Down
48 changes: 22 additions & 26 deletions gui/backend/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,38 @@ import (
"net/http"
"os"
"path/filepath"
"strings"

"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/kelp/support/kelpos"
)

// APIServer is an instance of the API service
type APIServer struct {
dirPath string
binPath string
configsDir string
logsDir string
kos *kelpos.KelpOS
horizonTestnetURI string
horizonPubnetURI string
ccxtRestUrl string
apiTestNet *horizonclient.Client
apiPubNet *horizonclient.Client
dirPath string
binPath string
configsDir string
logsDir string
kos *kelpos.KelpOS
horizonTestnetURI string
horizonPubnetURI string
ccxtRestUrl string
apiTestNet *horizonclient.Client
apiPubNet *horizonclient.Client
noHeaders bool

cachedOptionsMetadata metadata
}

// MakeAPIServer is a factory method
func MakeAPIServer(kos *kelpos.KelpOS, horizonTestnetURI string, horizonPubnetURI string, ccxtRestUrl string) (*APIServer, error) {
func MakeAPIServer(
kos *kelpos.KelpOS,
horizonTestnetURI string,
apiTestNet *horizonclient.Client,
horizonPubnetURI string,
apiPubNet *horizonclient.Client,
ccxtRestUrl string,
noHeaders bool,
) (*APIServer, error) {
binPath, e := filepath.Abs(os.Args[0])
if e != nil {
return nil, fmt.Errorf("could not get binPath of currently running binary: %s", e)
Expand All @@ -40,20 +49,6 @@ func MakeAPIServer(kos *kelpos.KelpOS, horizonTestnetURI string, horizonPubnetUR
configsDir := dirPath + "/ops/configs"
logsDir := dirPath + "/ops/logs"

horizonTestnetURI = strings.TrimSuffix(horizonTestnetURI, "/")
horizonPubnetURI = strings.TrimSuffix(horizonPubnetURI, "/")
log.Printf("using horizonTestnetURI: %s\n", horizonTestnetURI)
log.Printf("using horizonPubnetURI: %s\n", horizonPubnetURI)
log.Printf("using ccxtRestUrl: %s\n", ccxtRestUrl)
apiTestNet := &horizonclient.Client{
HorizonURL: horizonTestnetURI,
HTTP: http.DefaultClient,
}
apiPubNet := &horizonclient.Client{
HorizonURL: horizonPubnetURI,
HTTP: http.DefaultClient,
}

optionsMetadata, e := loadOptionsMetadata()
if e != nil {
return nil, fmt.Errorf("error while loading options metadata when making APIServer: %s", e)
Expand All @@ -70,6 +65,7 @@ func MakeAPIServer(kos *kelpos.KelpOS, horizonTestnetURI string, horizonPubnetUR
ccxtRestUrl: ccxtRestUrl,
apiTestNet: apiTestNet,
apiPubNet: apiPubNet,
noHeaders: noHeaders,
cachedOptionsMetadata: optionsMetadata,
}, nil
}
Expand Down
5 changes: 4 additions & 1 deletion gui/backend/start_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ func (s *APIServer) startBot(w http.ResponseWriter, r *http.Request) {
func (s *APIServer) doStartBot(botName string, strategy string, iterations *uint8, maybeFinishCallback func()) error {
filenamePair := model2.GetBotFilenames(botName, strategy)
logPrefix := model2.GetLogPrefix(botName, strategy)
command := fmt.Sprintf("trade -c %s/%s -s %s -f %s/%s -l %s/%s --with-ipc", s.configsDir, filenamePair.Trader, strategy, s.configsDir, filenamePair.Strategy, s.logsDir, logPrefix)
command := fmt.Sprintf("trade -c %s/%s -s %s -f %s/%s -l %s/%s --with-ipc --ui", s.configsDir, filenamePair.Trader, strategy, s.configsDir, filenamePair.Strategy, s.logsDir, logPrefix)
if iterations != nil {
command = fmt.Sprintf("%s --iter %d", command, *iterations)
}
if s.noHeaders {
command = fmt.Sprintf("%s --no-headers", command)
}
if s.ccxtRestUrl != "" {
command = fmt.Sprintf("%s --ccxt-rest-url %s", command, s.ccxtRestUrl)
}
Expand Down

0 comments on commit a860208

Please sign in to comment.