Merge pull request #36 from butonic/return-error
allow handler to return an error
This commit is contained in:
@@ -2,7 +2,6 @@ package command
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/owncloud/ocis-accounts/pkg/flagset"
|
"github.com/owncloud/ocis-accounts/pkg/flagset"
|
||||||
@@ -53,8 +52,14 @@ func Server(cfg *config.Config) *cli.Command {
|
|||||||
logger.Info().Str("service", service.Name()).Msg("Reporting settings bundle to account service")
|
logger.Info().Str("service", service.Name()).Msg("Reporting settings bundle to account service")
|
||||||
go svc.RegisterSettingsBundles(&logger)
|
go svc.RegisterSettingsBundles(&logger)
|
||||||
return service.Run()
|
return service.Run()
|
||||||
}, func(_ error) {
|
}, func(err error) {
|
||||||
fmt.Println("shutting down grpc server")
|
if err != nil {
|
||||||
|
logger.Error().Err(err).Msg("account service died")
|
||||||
|
} else {
|
||||||
|
logger.Info().
|
||||||
|
Str("service", service.Name()).
|
||||||
|
Msg("Shutting down server")
|
||||||
|
}
|
||||||
cancel()
|
cancel()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,13 @@ func NewService(opts ...Option) grpc.Service {
|
|||||||
grpc.Logger(options.Logger),
|
grpc.Logger(options.Logger),
|
||||||
)
|
)
|
||||||
|
|
||||||
hdlr := svc.New(options.Config)
|
var hdlr *svc.Service
|
||||||
if err := proto.RegisterAccountsServiceHandler(service.Server(), hdlr); err != nil {
|
var err error
|
||||||
|
|
||||||
|
if hdlr, err = svc.New(options.Config); err != nil {
|
||||||
|
options.Logger.Fatal().Err(err).Msg("could not initialize service handler")
|
||||||
|
}
|
||||||
|
if err = proto.RegisterAccountsServiceHandler(service.Server(), hdlr); err != nil {
|
||||||
options.Logger.Fatal().Err(err).Msg("could not register service handler")
|
options.Logger.Fatal().Err(err).Msg("could not register service handler")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+31
-22
@@ -34,57 +34,66 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// New returns a new instance of Service
|
// New returns a new instance of Service
|
||||||
func New(cfg *config.Config) Service {
|
// TODO pass in logger as options
|
||||||
|
func New(cfg *config.Config) (s *Service, err error) {
|
||||||
// read all user and group records
|
// read all user and group records
|
||||||
|
|
||||||
// for now recreate index on every start
|
// for now recreate index on every start
|
||||||
os.RemoveAll(filepath.Join(cfg.Server.AccountsDataPath, "index.bleve"))
|
if err = os.RemoveAll(filepath.Join(cfg.Server.AccountsDataPath, "index.bleve")); err != nil {
|
||||||
os.MkdirAll(filepath.Join(cfg.Server.AccountsDataPath, "accounts"), 0700)
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.MkdirAll(filepath.Join(cfg.Server.AccountsDataPath, "accounts"), 0700); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
mapping := bleve.NewIndexMapping()
|
mapping := bleve.NewIndexMapping()
|
||||||
|
// keep all symbols in terms to allow exact maching, eg. emails
|
||||||
mapping.DefaultAnalyzer = keyword.Name
|
mapping.DefaultAnalyzer = keyword.Name
|
||||||
|
|
||||||
// TODO don't bother to store fields as we will load the account from disk
|
// TODO don't bother to store fields as we will load the account from disk
|
||||||
index, err := bleve.New(filepath.Join(cfg.Server.AccountsDataPath, "index.bleve"), mapping)
|
|
||||||
if err != nil {
|
s = &Service{
|
||||||
panic(err)
|
Config: cfg,
|
||||||
}
|
}
|
||||||
f, err := os.Open(filepath.Join(cfg.Server.AccountsDataPath, "accounts"))
|
|
||||||
if err != nil {
|
if s.index, err = bleve.New(filepath.Join(cfg.Server.AccountsDataPath, "index.bleve"), mapping); err != nil {
|
||||||
log.Error().Err(err).Str("dir", filepath.Join(cfg.Server.AccountsDataPath, "accounts")).Msg("could not open acconts folder")
|
return
|
||||||
panic(err)
|
}
|
||||||
|
var f *os.File
|
||||||
|
if f, err = os.Open(filepath.Join(cfg.Server.AccountsDataPath, "accounts")); err != nil {
|
||||||
|
log.Error().Err(err).Str("dir", filepath.Join(cfg.Server.AccountsDataPath, "accounts")).Msg("could not open accounts folder")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
list, err := f.Readdir(-1)
|
list, err := f.Readdir(-1)
|
||||||
f.Close()
|
f.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Str("dir", filepath.Join(cfg.Server.AccountsDataPath, "accounts")).Msg("could not list accounts folder")
|
log.Error().Err(err).Str("dir", filepath.Join(cfg.Server.AccountsDataPath, "accounts")).Msg("could not list accounts folder")
|
||||||
panic(err)
|
return
|
||||||
}
|
}
|
||||||
|
var data []byte
|
||||||
for _, file := range list {
|
for _, file := range list {
|
||||||
path := filepath.Join(cfg.Server.AccountsDataPath, "accounts", file.Name())
|
path := filepath.Join(cfg.Server.AccountsDataPath, "accounts", file.Name())
|
||||||
data, err := ioutil.ReadFile(path)
|
if data, err = ioutil.ReadFile(path); err != nil {
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Str("path", path).Msg("could not read account")
|
log.Error().Err(err).Str("path", path).Msg("could not read account")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
a := proto.Account{}
|
a := proto.Account{}
|
||||||
err = json.Unmarshal(data, &a)
|
if err = json.Unmarshal(data, &a); err != nil {
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Str("path", path).Msg("could not unmarshal account")
|
log.Error().Err(err).Str("path", path).Msg("could not unmarshal account")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Debug().Interface("account", a).Msg("found account")
|
log.Debug().Interface("account", a).Msg("found account")
|
||||||
index.Index(a.Id, a)
|
if err = s.index.Index(a.Id, a); err != nil {
|
||||||
|
log.Error().Err(err).Str("path", path).Interface("account", a).Msg("could not index account")
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO watch folders for new records
|
// TODO watch folders for new records
|
||||||
|
|
||||||
s := Service{
|
s.Config = cfg
|
||||||
Config: cfg,
|
|
||||||
index: index,
|
|
||||||
}
|
|
||||||
|
|
||||||
return s
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Service implements the AccountsServiceHandler interface
|
// Service implements the AccountsServiceHandler interface
|
||||||
|
|||||||
Reference in New Issue
Block a user