add setup struct for setup mode options

This commit is contained in:
c9s 2021-02-04 13:29:43 +08:00
parent 7b7bcf56c9
commit 6db1924f87
2 changed files with 31 additions and 14 deletions

View File

@ -71,7 +71,11 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer
Config: userConfig,
Environ: environ,
Trader: trader,
Setup: true,
Setup: &server.Setup{
Context: ctx,
Cancel: cancelTrading,
Token: "",
},
}
if err := s.Run(ctx); err != nil {
@ -278,7 +282,6 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config, enableApiServer
Config: userConfig,
Environ: environ,
Trader: trader,
Setup: false,
}
if err := s.Run(ctx); err != nil {

View File

@ -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"`
@ -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) {