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

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

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