Setting up file store in constructor and not in init which is o… (#1499)

This commit is contained in:
Janos Dobronszki 2020-04-07 16:43:43 +02:00 committed by GitHub
parent 6aaad7d63f
commit 038b936ce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View File

@ -10,7 +10,7 @@ import (
gcli "github.com/micro/go-micro/v2/client/grpc" gcli "github.com/micro/go-micro/v2/client/grpc"
memTrace "github.com/micro/go-micro/v2/debug/trace/memory" memTrace "github.com/micro/go-micro/v2/debug/trace/memory"
gsrv "github.com/micro/go-micro/v2/server/grpc" 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() { func init() {
@ -19,7 +19,7 @@ func init() {
// default server // default server
server.DefaultServer = gsrv.NewServer() server.DefaultServer = gsrv.NewServer()
// default store // default store
store.DefaultStore = fileStore.NewStore() store.DefaultStore = memoryStore.NewStore()
// set default trace // set default trace
trace.DefaultTracer = memTrace.NewTracer() trace.DefaultTracer = memTrace.NewTracer()
} }

View File

@ -32,9 +32,7 @@ func NewStore(opts ...store.Option) store.Store {
s := &fileStore{ s := &fileStore{
options: store.Options{}, options: store.Options{},
} }
for _, o := range opts { s.init(opts...)
o(&s.options)
}
return s return s
} }
@ -46,6 +44,10 @@ type fileStore struct {
} }
func (m *fileStore) Init(opts ...store.Option) error { func (m *fileStore) Init(opts ...store.Option) error {
return m.init(opts...)
}
func (m *fileStore) init(opts ...store.Option) error {
for _, o := range opts { for _, o := range opts {
o(&m.options) o(&m.options)
} }
@ -58,6 +60,9 @@ func (m *fileStore) Init(opts ...store.Option) error {
} }
dir := filepath.Join(DefaultDir, "micro") dir := filepath.Join(DefaultDir, "micro")
fname := m.options.Database + ".db" 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) _ = os.Mkdir(dir, 0700)
m.dir = dir m.dir = dir
m.fileName = fname m.fileName = fname