refactor server routes

This commit is contained in:
c9s 2021-02-03 18:09:33 +08:00
parent bf8508d4fb
commit c73e5f00f3
2 changed files with 62 additions and 31 deletions

View File

@ -66,7 +66,14 @@ func runSetup(basectx context.Context, userConfig *bbgo.Config, enableApiServer
if enableApiServer {
go func() {
if err := server.Run(ctx, userConfig, environ, trader, true); err != nil {
s := &server.Server{
Config: userConfig,
Environ: environ,
Trader: trader,
Setup: true,
}
if err := s.Run(ctx); err != nil {
log.WithError(err).Errorf("server error")
}
}()
@ -258,7 +265,14 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config, enableApiServer
if enableApiServer {
go func() {
if err := server.Run(ctx, userConfig, environ, trader, false); err != nil {
s := &server.Server{
Config: userConfig,
Environ: environ,
Trader: trader,
Setup: false,
}
if err := s.Run(ctx); err != nil {
log.WithError(err).Errorf("server error")
}
}()

View File

@ -21,7 +21,18 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
func Run(ctx context.Context, userConfig *bbgo.Config, environ *bbgo.Environment, trader *bbgo.Trader, setup bool) error {
type Server struct {
Config *bbgo.Config
Environ *bbgo.Environment
Trader *bbgo.Trader
Setup bool
}
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{
AllowOrigins: []string{"*"},
@ -91,14 +102,17 @@ func Run(ctx context.Context, userConfig *bbgo.Config, environ *bbgo.Environment
c.JSON(http.StatusOK, gin.H{"success": true})
})
r.POST("/api/setup/restart", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"success": true})
})
r.POST("/api/setup/save", func(c *gin.Context) {
if len(userConfig.Sessions) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "session is not configured"})
return
}
envVars, err := collectSessionEnvVars(userConfig.Sessions)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
@ -489,36 +503,39 @@ func Run(ctx context.Context, userConfig *bbgo.Config, environ *bbgo.Environment
c.JSON(200, gin.H{"message": "pong"})
})
r.GET("/api/strategies/single", func(c *gin.Context) {
var stashes []map[string]interface{}
for _, mount := range userConfig.ExchangeStrategies {
stash, err := mount.Map()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
stash["strategy"] = mount.Strategy.ID()
stashes = append(stashes, stash)
}
if len(stashes) > 0 {
c.JSON(http.StatusOK, gin.H{"strategies": stashes})
} else {
c.JSON(http.StatusOK, gin.H{"strategies": []int{}})
}
})
fs := pkger.Dir("/frontend/out")
r.NoRoute(func(c *gin.Context) {
http.FileServer(fs).ServeHTTP(c.Writer, c.Request)
})
r.GET("/api/strategies/single", s.listStrategies)
r.NoRoute(s.pkgerHandler)
return r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
func (s *Server) listStrategies(c *gin.Context) {
var stashes []map[string]interface{}
for _, mount := range s.Config.ExchangeStrategies {
stash, err := mount.Map()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
stash["strategy"] = mount.Strategy.ID()
stashes = append(stashes, stash)
}
if len(stashes) > 0 {
c.JSON(http.StatusOK, gin.H{"strategies": stashes})
} else {
c.JSON(http.StatusOK, gin.H{"strategies": []int{}})
}
}
func (s *Server) pkgerHandler(c *gin.Context) {
fs := pkger.Dir("/frontend/out")
http.FileServer(fs).ServeHTTP(c.Writer, c.Request)
}
func moveFileToBackup(filename string) error {
stat, err := os.Stat(filename)