From 6db1924f8726a63104465b01337f8de82806d770 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 4 Feb 2021 13:29:43 +0800 Subject: [PATCH] add setup struct for setup mode options --- pkg/cmd/run.go | 17 ++++++++++------- pkg/server/routes.go | 28 +++++++++++++++++++++------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index adca126c7..f1a88c236 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -68,10 +68,14 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer if enableApiServer { go func() { s := &server.Server{ - Config: userConfig, + Config: userConfig, Environ: environ, - Trader: trader, - Setup: true, + Trader: trader, + Setup: &server.Setup{ + Context: ctx, + Cancel: cancelTrading, + Token: "", + }, } if err := s.Run(ctx); err != nil { @@ -82,7 +86,7 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer if false && runtime.GOOS == "darwin" { <-time.After(time.Second * 3) cmd := exec.Command("open", "http://localhost:8080/setup") - if err := cmd.Start() ; err != nil { + if err := cmd.Start(); err != nil { log.WithError(err).Errorf("can not call open command to open the web page") } } @@ -275,10 +279,9 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config, enableApiServer if enableApiServer { go func() { s := &server.Server{ - Config: userConfig, + Config: userConfig, Environ: environ, - Trader: trader, - Setup: false, + Trader: trader, } if err := s.Run(ctx); err != nil { diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 01668be22..85c9f9bef 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -22,17 +22,28 @@ import ( "github.com/c9s/bbgo/pkg/types" ) +type Setup struct { + // Context is the trader context + Context context.Context + + // Cancel is the trader context cancel function you want to cancel + Cancel context.CancelFunc + + // Token is used for setup api authentication + Token string +} + type Server struct { Config *bbgo.Config Environ *bbgo.Environment Trader *bbgo.Trader - Setup bool + Setup *Setup + Bind string } func (s *Server) Run(ctx context.Context) error { userConfig := s.Config environ := s.Environ - setup := s.Setup r := gin.Default() r.Use(cors.New(cors.Config{ @@ -49,7 +60,7 @@ func (s *Server) Run(ctx context.Context) error { c.JSON(http.StatusOK, gin.H{"message": "pong"}) }) - if setup { + if s.Setup != nil { r.POST("/api/setup/test-db", func(c *gin.Context) { payload := struct { DSN string `json:"dsn"` @@ -196,7 +207,7 @@ func (s *Server) Run(ctx context.Context) error { r.GET("/api/trades", func(c *gin.Context) { if environ.TradeService == nil { - c.JSON(http.StatusInternalServerError, gin.H{ "error": "database is not configured" }) + c.JSON(http.StatusInternalServerError, gin.H{"error": "database is not configured"}) return } @@ -229,7 +240,7 @@ func (s *Server) Run(ctx context.Context) error { r.GET("/api/orders/closed", func(c *gin.Context) { if environ.OrderService == nil { - c.JSON(http.StatusInternalServerError, gin.H{ "error": "database is not configured" }) + c.JSON(http.StatusInternalServerError, gin.H{"error": "database is not configured"}) return } @@ -263,7 +274,7 @@ func (s *Server) Run(ctx context.Context) error { r.GET("/api/trading-volume", func(c *gin.Context) { if environ.TradeService == nil { - c.JSON(http.StatusInternalServerError, gin.H{ "error": "database is not configured" }) + c.JSON(http.StatusInternalServerError, gin.H{"error": "database is not configured"}) return } @@ -522,6 +533,10 @@ func (s *Server) Run(ctx context.Context) error { r.GET("/api/strategies/single", s.listStrategies) r.NoRoute(s.pkgerHandler) + if len(s.Bind) > 0 { + return r.Run(s.Bind) + } + return r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") } @@ -547,7 +562,6 @@ func (s *Server) listStrategies(c *gin.Context) { } } - var pageRoutePattern = regexp.MustCompile("/[a-z]+$") func (s *Server) pkgerHandler(c *gin.Context) {