From 7c6fce076fbf78693fc5de13f99a25f4e634b7b0 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 4 Feb 2021 13:48:21 +0800 Subject: [PATCH] use ping try the api server --- pkg/cmd/run.go | 10 +----- pkg/server/routes.go | 72 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index f1a88c236..89cbe8649 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -9,7 +9,6 @@ import ( "os" "os/exec" "path/filepath" - "runtime" "strings" "syscall" "time" @@ -71,6 +70,7 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer Config: userConfig, Environ: environ, Trader: trader, + OpenInBrowser: true, Setup: &server.Setup{ Context: ctx, Cancel: cancelTrading, @@ -82,14 +82,6 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer log.WithError(err).Errorf("server error") } }() - - if false && runtime.GOOS == "darwin" { - <-time.After(time.Second * 3) - cmd := exec.Command("open", "http://localhost:8080/setup") - if err := cmd.Start(); err != nil { - log.WithError(err).Errorf("can not call open command to open the web page") - } - } } cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM) diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 85c9f9bef..8ee8effb4 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -2,11 +2,14 @@ package server import ( "context" + "encoding/json" "fmt" "io/ioutil" "net/http" "os" + "os/exec" "regexp" + "runtime" "strconv" "strings" "time" @@ -22,6 +25,8 @@ import ( "github.com/c9s/bbgo/pkg/types" ) +const DefaultBindAddress = "localhost:8080" + type Setup struct { // Context is the trader context Context context.Context @@ -34,11 +39,12 @@ type Setup struct { } type Server struct { - Config *bbgo.Config - Environ *bbgo.Environment - Trader *bbgo.Trader - Setup *Setup - Bind string + Config *bbgo.Config + Environ *bbgo.Environment + Trader *bbgo.Trader + Setup *Setup + Bind string + OpenInBrowser bool } func (s *Server) Run(ctx context.Context) error { @@ -533,6 +539,19 @@ func (s *Server) Run(ctx context.Context) error { r.GET("/api/strategies/single", s.listStrategies) r.NoRoute(s.pkgerHandler) + if s.OpenInBrowser { + if runtime.GOOS == "darwin" { + baseURL := "http://" + DefaultBindAddress + if len(s.Bind) > 0 { + baseURL = "http://" + s.Bind + } + + go pingAndOpenURL(ctx, baseURL) + } else { + logrus.Warnf("%s is not supported for opening browser automatically", runtime.GOOS) + } + } + if len(s.Bind) > 0 { return r.Run(s.Bind) } @@ -620,3 +639,46 @@ func collectSessionEnvVars(sessions map[string]*bbgo.ExchangeSession) (envVars m return } + +func getJSON(url string, data interface{}) error { + var client = &http.Client{Timeout: 500 * time.Millisecond} + r, err := client.Get(url) + if err != nil { + return err + } + + defer r.Body.Close() + + return json.NewDecoder(r.Body).Decode(data) +} + +func openURL(url string) error { + cmd := exec.Command("open", url) + return cmd.Start() +} + +func pingAndOpenURL(ctx context.Context, baseURL string) { + pingURL := baseURL + "/api/ping" + setupURL := baseURL + "/setup" + + ticker := time.NewTimer(time.Second) + defer ticker.Stop() + + for { + select { + + case <-ctx.Done(): + return + + case <-ticker.C: + var response map[string]interface{} + var err = getJSON(pingURL, &response) + if err == nil { + if err := openURL(setupURL); err != nil { + logrus.WithError(err).Errorf("can not call open command to open the web page") + } + return + } + } + } +}