mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 01:01:56 +00:00
refactor database configure method
This commit is contained in:
parent
1763fb8904
commit
6845db6dd3
|
@ -69,6 +69,10 @@ func NewEnvironment() *Environment {
|
|||
syncStartTime: time.Now().AddDate(-1, 0, 0), // defaults to sync from 1 year ago
|
||||
sessions: make(map[string]*ExchangeSession),
|
||||
startTime: time.Now(),
|
||||
|
||||
PersistenceServiceFacade: &service.PersistenceServiceFacade{
|
||||
Memory: service.NewMemoryService(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -96,7 +100,28 @@ func (environ *Environment) SelectSessions(names ...string) map[string]*Exchange
|
|||
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)
|
||||
err := environ.DatabaseService.Connect()
|
||||
if err != nil {
|
||||
|
@ -235,16 +260,12 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
|||
}
|
||||
|
||||
func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error {
|
||||
var facade = &service.PersistenceServiceFacade{
|
||||
Memory: service.NewMemoryService(),
|
||||
}
|
||||
|
||||
if conf.Redis != nil {
|
||||
if err := env.Set(conf.Redis); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
facade.Redis = service.NewRedisPersistenceService(conf.Redis)
|
||||
environ.PersistenceServiceFacade.Redis = service.NewRedisPersistenceService(conf.Redis)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -111,8 +111,7 @@ var BacktestCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
environ := bbgo.NewEnvironment()
|
||||
|
||||
if err := configureDB(ctx, environ) ; err != nil {
|
||||
if err := environ.ConfigureDatabase(ctx) ; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ var CancelCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
environ := bbgo.NewEnvironment()
|
||||
if err := configureDB(ctx, environ) ; err != nil {
|
||||
if err := environ.ConfigureDatabase(ctx) ; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -57,10 +57,11 @@ var PnLCmd = &cobra.Command{
|
|||
|
||||
|
||||
environ := bbgo.NewEnvironment()
|
||||
if err := configureDB(ctx, environ) ; err != nil {
|
||||
if err := environ.ConfigureDatabase(ctx) ; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
var trades []types.Trade
|
||||
tradingFeeCurrency := exchange.PlatformFeeCurrency()
|
||||
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 {
|
||||
if err := configureDB(ctx, environ) ; err != nil {
|
||||
if err := environ.ConfigureDatabase(ctx) ; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ var SyncCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
environ := bbgo.NewEnvironment()
|
||||
if err := configureDB(ctx, environ); err != nil {
|
||||
if err := environ.ConfigureDatabase(ctx) ; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/bbgo"
|
||||
"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)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
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()})
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user