From 038b936ce982129cb1f3528435866c3854e9a1c1 Mon Sep 17 00:00:00 2001 From: Janos Dobronszki Date: Tue, 7 Apr 2020 16:43:43 +0200 Subject: [PATCH] =?UTF-8?q?Setting=20up=20file=20store=20in=20constructor?= =?UTF-8?q?=20and=20not=20in=20init=20which=20is=20o=E2=80=A6=20(#1499)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- defaults.go | 4 ++-- store/file/file.go | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/defaults.go b/defaults.go index 10408f90..1e991e4d 100644 --- a/defaults.go +++ b/defaults.go @@ -10,7 +10,7 @@ import ( gcli "github.com/micro/go-micro/v2/client/grpc" memTrace "github.com/micro/go-micro/v2/debug/trace/memory" gsrv "github.com/micro/go-micro/v2/server/grpc" - fileStore "github.com/micro/go-micro/v2/store/file" + memoryStore "github.com/micro/go-micro/v2/store/memory" ) func init() { @@ -19,7 +19,7 @@ func init() { // default server server.DefaultServer = gsrv.NewServer() // default store - store.DefaultStore = fileStore.NewStore() + store.DefaultStore = memoryStore.NewStore() // set default trace trace.DefaultTracer = memTrace.NewTracer() } diff --git a/store/file/file.go b/store/file/file.go index cf061fd7..26df09ea 100644 --- a/store/file/file.go +++ b/store/file/file.go @@ -32,9 +32,7 @@ func NewStore(opts ...store.Option) store.Store { s := &fileStore{ options: store.Options{}, } - for _, o := range opts { - o(&s.options) - } + s.init(opts...) return s } @@ -46,6 +44,10 @@ type fileStore struct { } func (m *fileStore) Init(opts ...store.Option) error { + return m.init(opts...) +} + +func (m *fileStore) init(opts ...store.Option) error { for _, o := range opts { o(&m.options) } @@ -58,6 +60,9 @@ func (m *fileStore) Init(opts ...store.Option) error { } dir := filepath.Join(DefaultDir, "micro") fname := m.options.Database + ".db" + // Ignoring this as the folder might exist. + // Reads/Writes updates will return with sensible error messages + // about the dir not existing in case this cannot create the path anyway _ = os.Mkdir(dir, 0700) m.dir = dir m.fileName = fname