mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
refactor database configure method
This commit is contained in:
parent
1763fb8904
commit
6845db6dd3
|
@ -47,10 +47,10 @@ type Environment struct {
|
||||||
Notifiability
|
Notifiability
|
||||||
|
|
||||||
PersistenceServiceFacade *service.PersistenceServiceFacade
|
PersistenceServiceFacade *service.PersistenceServiceFacade
|
||||||
DatabaseService *service.DatabaseService
|
DatabaseService *service.DatabaseService
|
||||||
OrderService *service.OrderService
|
OrderService *service.OrderService
|
||||||
TradeService *service.TradeService
|
TradeService *service.TradeService
|
||||||
TradeSync *service.SyncService
|
TradeSync *service.SyncService
|
||||||
|
|
||||||
// startTime is the time of start point (which is used in the backtest)
|
// startTime is the time of start point (which is used in the backtest)
|
||||||
startTime time.Time
|
startTime time.Time
|
||||||
|
@ -69,6 +69,10 @@ func NewEnvironment() *Environment {
|
||||||
syncStartTime: time.Now().AddDate(-1, 0, 0), // defaults to sync from 1 year ago
|
syncStartTime: time.Now().AddDate(-1, 0, 0), // defaults to sync from 1 year ago
|
||||||
sessions: make(map[string]*ExchangeSession),
|
sessions: make(map[string]*ExchangeSession),
|
||||||
startTime: time.Now(),
|
startTime: time.Now(),
|
||||||
|
|
||||||
|
PersistenceServiceFacade: &service.PersistenceServiceFacade{
|
||||||
|
Memory: service.NewMemoryService(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +100,28 @@ func (environ *Environment) SelectSessions(names ...string) map[string]*Exchange
|
||||||
return sessions
|
return sessions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (environ *Environment) ConfigureDatabase(ctx context.Context, driver string, dsn string) error {
|
func (environ *Environment) ConfigureDatabase(ctx context.Context) error {
|
||||||
|
// configureDB configures the database service based on the environment variable
|
||||||
|
if driver, ok := os.LookupEnv("DB_DRIVER"); ok {
|
||||||
|
|
||||||
|
if dsn, ok := os.LookupEnv("DB_DSN"); ok {
|
||||||
|
return environ.ConfigureDatabaseDriver(ctx, driver, dsn)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if dsn, ok := os.LookupEnv("SQLITE3_DSN"); ok {
|
||||||
|
|
||||||
|
return environ.ConfigureDatabaseDriver(ctx, "sqlite3", dsn)
|
||||||
|
|
||||||
|
} else if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
|
||||||
|
|
||||||
|
return environ.ConfigureDatabaseDriver(ctx, "mysql", dsn)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (environ *Environment) ConfigureDatabaseDriver(ctx context.Context, driver string, dsn string) error {
|
||||||
environ.DatabaseService = service.NewDatabaseService(driver, dsn)
|
environ.DatabaseService = service.NewDatabaseService(driver, dsn)
|
||||||
err := environ.DatabaseService.Connect()
|
err := environ.DatabaseService.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -235,16 +260,12 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error {
|
func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error {
|
||||||
var facade = &service.PersistenceServiceFacade{
|
|
||||||
Memory: service.NewMemoryService(),
|
|
||||||
}
|
|
||||||
|
|
||||||
if conf.Redis != nil {
|
if conf.Redis != nil {
|
||||||
if err := env.Set(conf.Redis); err != nil {
|
if err := env.Set(conf.Redis); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
facade.Redis = service.NewRedisPersistenceService(conf.Redis)
|
environ.PersistenceServiceFacade.Redis = service.NewRedisPersistenceService(conf.Redis)
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.Json != nil {
|
if conf.Json != nil {
|
||||||
|
@ -255,10 +276,9 @@ func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
facade.Json = &service.JsonPersistenceService{Directory: conf.Json.Directory}
|
environ.PersistenceServiceFacade.Json = &service.JsonPersistenceService{Directory: conf.Json.Directory}
|
||||||
}
|
}
|
||||||
|
|
||||||
environ.PersistenceServiceFacade = facade
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -111,8 +111,7 @@ var BacktestCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
environ := bbgo.NewEnvironment()
|
environ := bbgo.NewEnvironment()
|
||||||
|
if err := environ.ConfigureDatabase(ctx) ; err != nil {
|
||||||
if err := configureDB(ctx, environ) ; err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ var CancelCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
environ := bbgo.NewEnvironment()
|
environ := bbgo.NewEnvironment()
|
||||||
if err := configureDB(ctx, environ) ; err != nil {
|
if err := environ.ConfigureDatabase(ctx) ; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,10 +57,11 @@ var PnLCmd = &cobra.Command{
|
||||||
|
|
||||||
|
|
||||||
environ := bbgo.NewEnvironment()
|
environ := bbgo.NewEnvironment()
|
||||||
if err := configureDB(ctx, environ) ; err != nil {
|
if err := environ.ConfigureDatabase(ctx) ; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var trades []types.Trade
|
var trades []types.Trade
|
||||||
tradingFeeCurrency := exchange.PlatformFeeCurrency()
|
tradingFeeCurrency := exchange.PlatformFeeCurrency()
|
||||||
if strings.HasPrefix(symbol, tradingFeeCurrency) {
|
if strings.HasPrefix(symbol, tradingFeeCurrency) {
|
||||||
|
|
|
@ -165,7 +165,7 @@ func newNotificationSystem(userConfig *bbgo.Config, persistence bbgo.Persistence
|
||||||
}
|
}
|
||||||
|
|
||||||
func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
|
func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
|
||||||
if err := configureDB(ctx, environ) ; err != nil {
|
if err := environ.ConfigureDatabase(ctx) ; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ var SyncCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
environ := bbgo.NewEnvironment()
|
environ := bbgo.NewEnvironment()
|
||||||
if err := configureDB(ctx, environ); err != nil {
|
if err := environ.ConfigureDatabase(ctx) ; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/bbgo"
|
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,23 +10,3 @@ func inBaseAsset(balances types.BalanceMap, market types.Market, price float64)
|
||||||
return (base.Locked.Float64() + base.Available.Float64()) + ((quote.Locked.Float64() + quote.Available.Float64()) / price)
|
return (base.Locked.Float64() + base.Available.Float64()) + ((quote.Locked.Float64() + quote.Available.Float64()) / price)
|
||||||
}
|
}
|
||||||
|
|
||||||
// configureDB configures the database service based on the environment variable
|
|
||||||
func configureDB(ctx context.Context, environ *bbgo.Environment) error {
|
|
||||||
if driver, ok := os.LookupEnv("DB_DRIVER"); ok {
|
|
||||||
|
|
||||||
if dsn, ok := os.LookupEnv("DB_DSN"); ok {
|
|
||||||
return environ.ConfigureDatabase(ctx, driver, dsn)
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if dsn, ok := os.LookupEnv("SQLITE3_DSN"); ok {
|
|
||||||
|
|
||||||
return environ.ConfigureDatabase(ctx, "sqlite3", dsn)
|
|
||||||
|
|
||||||
} else if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
|
|
||||||
|
|
||||||
return environ.ConfigureDatabase(ctx, "mysql", dsn)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -69,7 +69,7 @@ func (s *Server) setupConfigureDB(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.Environ.ConfigureDatabase(c, payload.Driver, payload.DSN); err != nil {
|
if err := s.Environ.ConfigureDatabaseDriver(c, payload.Driver, payload.DSN); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user