bbgo_origin/pkg/server/setup.go

160 lines
3.5 KiB
Go
Raw Permalink Normal View History

2021-02-04 08:51:53 +00:00
package server
import (
2021-02-04 08:54:24 +00:00
"context"
2021-02-04 08:51:53 +00:00
"net/http"
2021-02-04 08:54:24 +00:00
"os"
"syscall"
"time"
2021-02-04 08:51:53 +00:00
"github.com/gin-gonic/gin"
2021-02-17 09:35:47 +00:00
"github.com/jmoiron/sqlx"
2021-02-04 08:51:53 +00:00
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
)
func (s *Server) setupTestDB(c *gin.Context) {
payload := struct {
2021-02-17 06:57:29 +00:00
Driver string `json:"driver"`
DSN string `json:"dsn"`
2021-02-04 08:51:53 +00:00
}{}
if err := c.BindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing arguments"})
return
}
2021-02-17 06:57:29 +00:00
if len(payload.Driver) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing driver parameter"})
return
}
if len(payload.DSN) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn parameter"})
2021-02-04 08:51:53 +00:00
return
}
2021-02-17 09:35:47 +00:00
db, err := sqlx.Connect(payload.Driver, payload.DSN)
2021-02-04 08:51:53 +00:00
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if err := db.Close(); err != nil {
logrus.WithError(err).Error("db connection close error")
}
c.JSON(http.StatusOK, gin.H{"success": true})
}
func (s *Server) setupConfigureDB(c *gin.Context) {
payload := struct {
2021-02-06 06:31:46 +00:00
Driver string `json:"driver"`
DSN string `json:"dsn"`
2021-02-04 08:51:53 +00:00
}{}
if err := c.BindJSON(&payload); err != nil {
2021-02-17 06:57:29 +00:00
c.JSON(http.StatusBadRequest, gin.H{"error": "missing parameters"})
2021-02-04 08:51:53 +00:00
return
}
2021-02-17 06:57:29 +00:00
if len(payload.Driver) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing driver parameter"})
2021-02-04 08:51:53 +00:00
return
}
2021-02-17 06:57:29 +00:00
if len(payload.DSN) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn parameter"})
return
2021-02-06 06:31:46 +00:00
}
2021-02-20 16:58:34 +00:00
if err := s.Environ.ConfigureDatabaseDriver(c, payload.Driver, payload.DSN); err != nil {
2021-02-04 08:51:53 +00:00
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
2021-02-06 06:31:46 +00:00
c.JSON(http.StatusOK, gin.H{
"success": true,
2021-02-17 06:57:29 +00:00
"driver": payload.Driver,
"dsn": payload.DSN,
2021-02-06 06:31:46 +00:00
})
2021-02-04 08:51:53 +00:00
}
func (s *Server) setupAddStrategy(c *gin.Context) {
sessionName := c.Param("session")
strategyID := c.Param("id")
_, ok := s.Environ.Session(sessionName)
if !ok {
c.JSON(http.StatusNotFound, "session not found")
return
}
var conf map[string]interface{}
if err := c.BindJSON(&conf); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing arguments"})
return
}
strategy, err := bbgo.NewStrategyFromMap(strategyID, conf)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
mount := bbgo.ExchangeStrategyMount{
Mounts: []string{sessionName},
Strategy: strategy,
}
s.Config.ExchangeStrategies = append(s.Config.ExchangeStrategies, mount)
c.JSON(http.StatusOK, gin.H{"success": true})
}
2021-02-04 08:54:24 +00:00
func (s *Server) setupRestart(c *gin.Context) {
if s.srv == nil {
logrus.Error("nil srv")
return
}
go func() {
logrus.Info("shutting down web server...")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.srv.Shutdown(ctx); err != nil {
logrus.WithError(err).Error("server forced to shutdown")
}
2021-02-05 01:09:59 +00:00
logrus.Info("server shutdown completed")
2021-02-04 08:54:24 +00:00
2021-02-05 05:01:07 +00:00
if s.Setup.BeforeRestart != nil {
s.Setup.BeforeRestart()
}
2021-02-04 08:54:24 +00:00
bin := os.Args[0]
args := os.Args[0:]
// filter out setup parameters
args = filterStrings(args, "--setup")
envVars := os.Environ()
2021-02-17 06:57:29 +00:00
logrus.Infof("exec %s %v", bin, args)
2021-02-04 08:54:24 +00:00
if err := syscall.Exec(bin, args, envVars); err != nil {
logrus.WithError(err).Errorf("failed to restart %s", bin)
}
s.Setup.Cancel()
}()
c.JSON(http.StatusOK, gin.H{"success": true})
}