diff --git a/pkg/bbgo/config.go b/pkg/bbgo/config.go index 959328ff2..6c52a5631 100644 --- a/pkg/bbgo/config.go +++ b/pkg/bbgo/config.go @@ -14,6 +14,7 @@ import ( "github.com/c9s/bbgo/pkg/datatype" "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/service" "github.com/c9s/bbgo/pkg/types" ) @@ -133,20 +134,9 @@ func (m BacktestAccountBalanceMap) BalanceMap() types.BalanceMap { return balances } -type RedisPersistenceConfig struct { - Host string `json:"host" env:"REDIS_HOST"` - Port string `json:"port" env:"REDIS_PORT"` - Password string `json:"password" env:"REDIS_PASSWORD"` - DB int `json:"db" env:"REDIS_DB"` -} - -type JsonPersistenceConfig struct { - Directory string `json:"directory"` -} - type PersistenceConfig struct { - Redis *RedisPersistenceConfig `json:"redis,omitempty" yaml:"redis,omitempty"` - Json *JsonPersistenceConfig `json:"json,omitempty" yaml:"json,omitempty"` + Redis *service.RedisPersistenceConfig `json:"redis,omitempty" yaml:"redis,omitempty"` + Json *service.JsonPersistenceConfig `json:"json,omitempty" yaml:"json,omitempty"` } type BuildTargetConfig struct { diff --git a/pkg/bbgo/persistence.go b/pkg/bbgo/persistence.go index 3d669e337..93d0c3fa7 100644 --- a/pkg/bbgo/persistence.go +++ b/pkg/bbgo/persistence.go @@ -21,7 +21,7 @@ type Persistence struct { Facade *service.PersistenceServiceFacade `json:"-" yaml:"-"` } -func (p *Persistence) backendService(t string) (service PersistenceService, err error) { +func (p *Persistence) backendService(t string) (service service.PersistenceService, err error) { switch t { case "json": service = p.Facade.Json @@ -67,13 +67,3 @@ func (p *Persistence) Save(val interface{}, subIDs ...string) error { return store.Save(val) } -type PersistenceService interface { - NewStore(id string, subIDs ...string) Store -} - -type Store interface { - Load(val interface{}) error - Save(val interface{}) error - Reset() error -} - diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 100c4763e..fae3225cd 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -88,7 +88,7 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer return nil } -func newNotificationSystem(userConfig *bbgo.Config, persistence bbgo.PersistenceService) (*bbgo.Notifiability, error) { +func newNotificationSystem(userConfig *bbgo.Config, persistence service.PersistenceService) (*bbgo.Notifiability, error) { notification := &bbgo.Notifiability{ SymbolChannelRouter: bbgo.NewPatternChannelRouter(nil), SessionChannelRouter: bbgo.NewPatternChannelRouter(nil), @@ -180,7 +180,7 @@ func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userCo } // configure persistence service, by default we will use memory service - var persistence bbgo.PersistenceService = service.NewMemoryService() + var persistence service.PersistenceService = service.NewMemoryService() if environ.PersistenceServiceFacade != nil { if environ.PersistenceServiceFacade.Redis != nil { persistence = environ.PersistenceServiceFacade.Redis diff --git a/pkg/notifier/telegramnotifier/interaction.go b/pkg/notifier/telegramnotifier/interaction.go index dc00e20b5..79678be72 100644 --- a/pkg/notifier/telegramnotifier/interaction.go +++ b/pkg/notifier/telegramnotifier/interaction.go @@ -8,7 +8,7 @@ import ( "github.com/sirupsen/logrus" "gopkg.in/tucnak/telebot.v2" - "github.com/c9s/bbgo/pkg/bbgo" + "github.com/c9s/bbgo/pkg/service" ) var log = logrus.WithField("service", "telegram") @@ -27,7 +27,7 @@ func NewSession(key *otp.Key) Session { //go:generate callbackgen -type Interaction type Interaction struct { - store bbgo.Store + store service.Store bot *telebot.Bot @@ -39,7 +39,7 @@ type Interaction struct { AuthCallbacks []func(user *telebot.User) } -func NewInteraction(bot *telebot.Bot, store bbgo.Store) *Interaction { +func NewInteraction(bot *telebot.Bot, store service.Store) *Interaction { interaction := &Interaction{ store: store, bot: bot, diff --git a/pkg/service/memory.go b/pkg/service/memory.go index 1ff5056f9..a23500aca 100644 --- a/pkg/service/memory.go +++ b/pkg/service/memory.go @@ -3,8 +3,6 @@ package service import ( "reflect" "strings" - - "github.com/c9s/bbgo/pkg/bbgo" ) type MemoryService struct { @@ -17,7 +15,7 @@ func NewMemoryService() *MemoryService { } } -func (s *MemoryService) NewStore(id string, subIDs ...string) bbgo.Store { +func (s *MemoryService) NewStore(id string, subIDs ...string) Store { key := strings.Join(append([]string{id}, subIDs...), ":") return &MemoryStore{ Key: key, diff --git a/pkg/service/persistence.go b/pkg/service/persistence.go new file mode 100644 index 000000000..ef55ca2ef --- /dev/null +++ b/pkg/service/persistence.go @@ -0,0 +1,23 @@ +package service + +type PersistenceService interface { + NewStore(id string, subIDs ...string) Store +} + +type Store interface { + Load(val interface{}) error + Save(val interface{}) error + Reset() error +} + +type RedisPersistenceConfig struct { + Host string `json:"host" env:"REDIS_HOST"` + Port string `json:"port" env:"REDIS_PORT"` + Password string `json:"password" env:"REDIS_PASSWORD"` + DB int `json:"db" env:"REDIS_DB"` +} + +type JsonPersistenceConfig struct { + Directory string `json:"directory"` +} + diff --git a/pkg/service/persistence_json.go b/pkg/service/persistence_json.go index 3ac5c95f6..20ce52fa2 100644 --- a/pkg/service/persistence_json.go +++ b/pkg/service/persistence_json.go @@ -5,15 +5,13 @@ import ( "io/ioutil" "os" "path/filepath" - - "github.com/c9s/bbgo/pkg/bbgo" ) type JsonPersistenceService struct { Directory string } -func (s *JsonPersistenceService) NewStore(id string, subIDs ...string) bbgo.Store { +func (s *JsonPersistenceService) NewStore(id string, subIDs ...string) Store { return &JsonStore{ ID: id, Directory: filepath.Join(append([]string{s.Directory}, subIDs...)...), diff --git a/pkg/service/persistence_redis.go b/pkg/service/persistence_redis.go index e3ea08f2d..52ef4fef3 100644 --- a/pkg/service/persistence_redis.go +++ b/pkg/service/persistence_redis.go @@ -7,15 +7,13 @@ import ( "strings" "github.com/go-redis/redis/v8" - - "github.com/c9s/bbgo/pkg/bbgo" ) type RedisPersistenceService struct { redis *redis.Client } -func NewRedisPersistenceService(config *bbgo.RedisPersistenceConfig) *RedisPersistenceService { +func NewRedisPersistenceService(config *RedisPersistenceConfig) *RedisPersistenceService { client := redis.NewClient(&redis.Options{ Addr: net.JoinHostPort(config.Host, config.Port), // Username: "", // username is only for redis 6.0 @@ -28,7 +26,7 @@ func NewRedisPersistenceService(config *bbgo.RedisPersistenceConfig) *RedisPersi } } -func (s *RedisPersistenceService) NewStore(id string, subIDs ...string) bbgo.Store { +func (s *RedisPersistenceService) NewStore(id string, subIDs ...string) Store { if len(subIDs) > 0 { id += ":" + strings.Join(subIDs, ":") } diff --git a/pkg/service/persistence_redis_test.go b/pkg/service/persistence_redis_test.go index fbd719a1a..ff1d0f0d6 100644 --- a/pkg/service/persistence_redis_test.go +++ b/pkg/service/persistence_redis_test.go @@ -5,12 +5,11 @@ import ( "github.com/stretchr/testify/assert" - "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" ) func TestRedisPersistentService(t *testing.T) { - redisService := NewRedisPersistenceService(&bbgo.RedisPersistenceConfig{ + redisService := NewRedisPersistenceService(&RedisPersistenceConfig{ Host: "127.0.0.1", Port: "6379", DB: 0,