From b574230285bcfb5fc322d8e846d223faf8921312 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Fri, 31 Jan 2020 16:27:13 +0100 Subject: [PATCH] move store initialization to root and run only once --- pkg/command/root.go | 2 +- pkg/registry/registry.go | 19 +++++++++++++++---- pkg/service/v0/service.go | 10 ++++------ pkg/store/filesystem/store.go | 24 ++++++++++-------------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/pkg/command/root.go b/pkg/command/root.go index aa04c62d9..972cdd2cb 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -4,12 +4,12 @@ import ( "os" "github.com/micro/cli" + _ "github.com/owncloud/ocis-accounts/pkg/registry" "github.com/owncloud/ocis-hello/pkg/version" ) // Execute is the entry point for the ocis-accounts command. func Execute() error { - app := &cli.App{ Name: "ocis-accounts", Version: version.String, diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index 47b701034..1dae9ba8f 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -2,9 +2,20 @@ package registry import ( - "github.com/micro/go-micro/v2/store" + "sync" + + mstore "github.com/micro/go-micro/v2/store" + store "github.com/owncloud/ocis-accounts/pkg/store/filesystem" ) -type Registry { - Store store.Store -} \ No newline at end of file +var ( + once *sync.Once = &sync.Once{} + // Store is a micro store implementation + Store mstore.Store +) + +func init() { + once.Do(func() { + Store = store.New() + }) +} diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 8de33434e..469778e4d 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -6,7 +6,8 @@ import ( mstore "github.com/micro/go-micro/v2/store" "github.com/owncloud/ocis-accounts/pkg/proto/v0" - store "github.com/owncloud/ocis-accounts/pkg/store/filesystem" + + "github.com/owncloud/ocis-accounts/pkg/registry" ) // New returns a new instance of Service @@ -19,8 +20,6 @@ type Service struct{} // Set implements the SettingsServiceHandler interface generated on accounts.pb.micro.go func (s Service) Set(c context.Context, req *proto.Record, res *proto.Record) error { - st := store.New() - settingsJSON, err := json.Marshal(req.Payload) if err != nil { return err @@ -31,13 +30,12 @@ func (s Service) Set(c context.Context, req *proto.Record, res *proto.Record) er Value: settingsJSON, } - return st.Write(&record) + return registry.Store.Write(&record) } // Get implements the SettingsServiceHandler interface generated on accounts.pb.micro.go func (s Service) Get(c context.Context, req *proto.Query, res *proto.Record) error { - st := store.New() - contents, _ := st.Read(req.Key) + contents, _ := registry.Store.Read(req.Key) if len(contents) > 0 { r := &proto.Payload{} diff --git a/pkg/store/filesystem/store.go b/pkg/store/filesystem/store.go index 992357ff0..ffd14920a 100644 --- a/pkg/store/filesystem/store.go +++ b/pkg/store/filesystem/store.go @@ -21,9 +21,8 @@ type Store struct { Logger olog.Logger } -// New returns a new file system store manager // TODO add mountPath as a flag. Accept a *config argument -func New() Store { +func New() *Store { s := Store{ Logger: olog.NewLogger(), } @@ -41,21 +40,23 @@ func New() Store { } s.mountPath = dest - return s + return &s } // Init implements the store interface // TODO it could prepare the destination path, for instance -func (s *Store) Init(...mstore.Options) {} +func (s Store) Init(...mstore.Option) error { + return nil +} // List implements the store interface -func (s *Store) List() ([]*mstore.Record, error) { +func (s Store) List() ([]*mstore.Record, error) { return nil, nil } // Read implements the store interface // this implementation only reads by id. -func (s *Store) Read(key string, opts ...mstore.ReadOption) ([]*mstore.Record, error) { +func (s Store) Read(key string, opts ...mstore.ReadOption) ([]*mstore.Record, error) { contents, err := ioutil.ReadFile(path.Join(s.mountPath, key)) if err != nil { s.Logger.Err(err).Msgf("error reading contents of key %v: file not found", key) @@ -71,7 +72,7 @@ func (s *Store) Read(key string, opts ...mstore.ReadOption) ([]*mstore.Record, e } // Write implements the store interface -func (s *Store) Write(rec *mstore.Record) error { +func (s Store) Write(rec *mstore.Record) error { path := filepath.Join(s.mountPath, rec.Key) if len(rec.Key) < 1 { @@ -88,16 +89,11 @@ func (s *Store) Write(rec *mstore.Record) error { } // Delete implements the store interface -func (s *Store) Delete(key string) error { +func (s Store) Delete(key string) error { return nil } // String implements the store interface, and the stringer interface -func (s *Store) String() string { +func (s Store) String() string { return "store" } - -// creates the default container for the ocis-store if it doesn't exist -func init() { - -}