mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
move setup handlers to setup.go
This commit is contained in:
parent
c12161ff1c
commit
e1b4ff3450
|
@ -244,96 +244,10 @@ func (s *Server) Run(ctx context.Context, bindArgs ...string) error {
|
|||
return listenAndServe(s.srv)
|
||||
}
|
||||
|
||||
func (s *Server) setupTestDB(c *gin.Context) {
|
||||
payload := struct {
|
||||
DSN string `json:"dsn"`
|
||||
}{}
|
||||
|
||||
if err := c.BindJSON(&payload); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing arguments"})
|
||||
return
|
||||
}
|
||||
|
||||
dsn := payload.DSN
|
||||
if len(dsn) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn argument"})
|
||||
return
|
||||
}
|
||||
|
||||
db, err := bbgo.ConnectMySQL(dsn)
|
||||
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 {
|
||||
DSN string `json:"dsn"`
|
||||
}{}
|
||||
|
||||
if err := c.BindJSON(&payload); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing arguments"})
|
||||
return
|
||||
}
|
||||
|
||||
dsn := payload.DSN
|
||||
if len(dsn) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn argument"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.Environ.ConfigureDatabase(c, dsn); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
|
||||
func (s *Server) ping(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
|
||||
func (s *Server) setupRestart(c *gin.Context) {
|
||||
if s.srv == nil {
|
||||
logrus.Error("nil srv")
|
||||
|
|
97
pkg/server/setup.go
Normal file
97
pkg/server/setup.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/bbgo"
|
||||
)
|
||||
|
||||
func (s *Server) setupTestDB(c *gin.Context) {
|
||||
payload := struct {
|
||||
DSN string `json:"dsn"`
|
||||
}{}
|
||||
|
||||
if err := c.BindJSON(&payload); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing arguments"})
|
||||
return
|
||||
}
|
||||
|
||||
dsn := payload.DSN
|
||||
if len(dsn) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn argument"})
|
||||
return
|
||||
}
|
||||
|
||||
db, err := bbgo.ConnectMySQL(dsn)
|
||||
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 {
|
||||
DSN string `json:"dsn"`
|
||||
}{}
|
||||
|
||||
if err := c.BindJSON(&payload); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing arguments"})
|
||||
return
|
||||
}
|
||||
|
||||
dsn := payload.DSN
|
||||
if len(dsn) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn argument"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.Environ.ConfigureDatabase(c, dsn); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user