Skip to content

Commit

Permalink
refactor(storage): extract Init func
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Aug 17, 2022
1 parent 121462a commit 981a3c9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
24 changes: 6 additions & 18 deletions app/bot/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,11 @@ func Run(cfg string) {
}
color.Blue("I18n templates loaded")

_kv, err := storage.NewKV(config.C.Storage.KV.Driver, config.C.Storage.KV.Options)
search, kv, cache, err := storage.Init(config.C.Storage)
if err != nil {
slog.Fatalw("init kv database failed", "err", err, "options", config.C.Storage.KV.Options)
slog.Fatalw("init storage failed", "err", err)
}
color.Blue("KV database initialized")

_search, err := storage.NewSearch(config.C.Storage.Search.Driver, config.C.Storage.Search.Options)
if err != nil {
slog.Fatalw("init search engine database failed", "err", err, "options", config.C.Storage.Search.Options)
}
color.Blue("Search engine initialized")

_cache, err := storage.New(config.C.Storage.Cache.Driver, config.C.Storage.Cache.Options)
if err != nil {
slog.Fatalw("init cache failed", "err", err, "options", config.C.Storage.Cache.Options)
}
color.Blue("Cache initialized")
color.Blue("Storage initialized")

dialer, err := utils.ProxyFromURL(config.C.Proxy)
if err != nil {
Expand All @@ -72,9 +60,9 @@ func Run(cfg string) {

scope := &model.Scope{
Storage: &model.Storage{
KV: _kv,
Search: _search,
Cache: _cache,
KV: kv,
Search: search,
Cache: cache,
},
Log: slog.Named("bot"),
}
Expand Down
18 changes: 3 additions & 15 deletions app/usr/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,11 @@ func Run(ctx context.Context, cfg string, _login bool) error {
}
color.Blue("I18n templates loaded")

kv, err := storage.NewKV(config.C.Storage.KV.Driver, config.C.Storage.KV.Options)
search, kv, cache, err := storage.Init(config.C.Storage)
if err != nil {
slog.Fatalw("init kv database failed", "err", err, "options", config.C.Storage.KV.Options)
slog.Fatalw("init storage failed", "err", err)
}
color.Blue("KV database initialized")

search, err := storage.NewSearch(config.C.Storage.Search.Driver, config.C.Storage.Search.Options)
if err != nil {
slog.Fatalw("init search engine database failed", "err", err, "options", config.C.Storage.Search.Options)
}
color.Blue("Search engine initialized")

cache, err := storage.New(config.C.Storage.Cache.Driver, config.C.Storage.Cache.Options)
if err != nil {
slog.Fatalw("init cache failed", "err", err, "options", config.C.Storage.Cache.Options)
}
color.Blue("Cache initialized")
color.Blue("Storage initialized")

dialer, err := utils.ProxyFromURL(config.C.Proxy)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Cache interface {
Set(key string, val interface{})
}

func New(name string, options map[string]interface{}) (Cache, error) {
func NewCache(name string, options map[string]interface{}) (Cache, error) {
switch name {
case "gocache":
return gocache.New(options)
Expand Down
16 changes: 16 additions & 0 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ type Config struct {
Options map[string]interface{} `mapstructure:"options"`
} `mapstructure:"cache"`
}

func Init(conf Config) (s Search, k KV, c Cache, err error) {
if s, err = NewSearch(conf.Search.Driver, conf.Search.Options); err != nil {
return
}

if k, err = NewKV(conf.KV.Driver, conf.KV.Options); err != nil {
return
}

if c, err = NewCache(conf.Cache.Driver, conf.Cache.Options); err != nil {
return
}

return
}

0 comments on commit 981a3c9

Please sign in to comment.