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/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)

View File

@ -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
}
}
}
}