write op working, sort of. encodes b64

This commit is contained in:
A.Unger
2020-01-31 09:36:17 +01:00
parent 31023b25b8
commit d02be70d52
2 changed files with 37 additions and 7 deletions
+18 -4
View File
@@ -2,9 +2,12 @@ package service
import (
"context"
"encoding/json"
"log"
"github.com/google/uuid"
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"
)
// New returns a new instance of Service
@@ -17,9 +20,20 @@ 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 {
res.Id = uuid.New().String()
res.Theme = "dark"
return nil
// uses a store manager to persist account information
st := store.New()
data, err := json.Marshal([]byte(`{"theme": "dark"}`))
if err != nil {
// deal with this accordingly and not panicking
log.Panic(err)
}
record := mstore.Record{
Key: req.Id,
Value: data,
}
return st.Write(&record)
}
// Get implements the SettingsServiceHandler interface generated on accounts.pb.micro.go
+19 -3
View File
@@ -2,15 +2,24 @@
package store
import (
"fmt"
"io/ioutil"
"log"
mstore "github.com/micro/go-micro/v2/store"
)
// Store interacts with the filesystem to manage account information
type Store struct{}
type Store struct {
mountPath string
}
// New returns a new file system store manager
// TODO add mountPath as an option argument
func New() Store {
return Store{}
return Store{
mountPath: "/var/tmp/ocis-store",
}
}
// Init implements the store interface
@@ -27,7 +36,14 @@ func (s *Store) Read(key string, opts ...mstore.ReadOption) ([]*mstore.Record, e
}
// Write implements the store interface
func (s *Store) Write(*mstore.Record) error {
func (s *Store) Write(rec *mstore.Record) error {
if rec.Key == "" {
return fmt.Errorf("%v", "key is empty")
}
if err := ioutil.WriteFile(s.mountPath+"/"+rec.Key, rec.Value, 0644); err != nil {
log.Panic(err)
}
return nil
}