use ping try the api server

This commit is contained in:
c9s 2021-02-04 13:48:21 +08:00
parent 63b837dbd0
commit 7c6fce076f
2 changed files with 68 additions and 14 deletions

View File

@ -9,7 +9,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"syscall" "syscall"
"time" "time"
@ -71,6 +70,7 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer
Config: userConfig, Config: userConfig,
Environ: environ, Environ: environ,
Trader: trader, Trader: trader,
OpenInBrowser: true,
Setup: &server.Setup{ Setup: &server.Setup{
Context: ctx, Context: ctx,
Cancel: cancelTrading, Cancel: cancelTrading,
@ -82,14 +82,6 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer
log.WithError(err).Errorf("server error") 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) cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)

View File

@ -2,11 +2,14 @@ package server
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec"
"regexp" "regexp"
"runtime"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -22,6 +25,8 @@ import (
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
const DefaultBindAddress = "localhost:8080"
type Setup struct { type Setup struct {
// Context is the trader context // Context is the trader context
Context context.Context Context context.Context
@ -39,6 +44,7 @@ type Server struct {
Trader *bbgo.Trader Trader *bbgo.Trader
Setup *Setup Setup *Setup
Bind string Bind string
OpenInBrowser bool
} }
func (s *Server) Run(ctx context.Context) error { 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.GET("/api/strategies/single", s.listStrategies)
r.NoRoute(s.pkgerHandler) 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 { if len(s.Bind) > 0 {
return r.Run(s.Bind) return r.Run(s.Bind)
} }
@ -620,3 +639,46 @@ func collectSessionEnvVars(sessions map[string]*bbgo.ExchangeSession) (envVars m
return 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
}
}
}
}