move store initialization to root and run only once

This commit is contained in:
A.Unger
2020-01-31 16:27:13 +01:00
parent 11029b5e17
commit b574230285
4 changed files with 30 additions and 25 deletions
+1 -1
View File
@@ -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,
+15 -4
View File
@@ -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
}
var (
once *sync.Once = &sync.Once{}
// Store is a micro store implementation
Store mstore.Store
)
func init() {
once.Do(func() {
Store = store.New()
})
}
+4 -6
View File
@@ -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{}
+10 -14
View File
@@ -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() {
}