diff --git a/pkg/command/server.go b/pkg/command/server.go index 4c05977d652..223cc25501d 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -2,7 +2,6 @@ package command import ( "context" - "fmt" "syscall" "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") go svc.RegisterSettingsBundles(&logger) return service.Run() - }, func(_ error) { - fmt.Println("shutting down grpc server") + }, func(err error) { + if err != nil { + logger.Error().Err(err).Msg("account service died") + } else { + logger.Info(). + Str("service", service.Name()). + Msg("Shutting down server") + } cancel() }) diff --git a/pkg/server/grpc/server.go b/pkg/server/grpc/server.go index b2031ef998c..16e6471ffbe 100644 --- a/pkg/server/grpc/server.go +++ b/pkg/server/grpc/server.go @@ -18,8 +18,13 @@ func NewService(opts ...Option) grpc.Service { grpc.Logger(options.Logger), ) - hdlr := svc.New(options.Config) - if err := proto.RegisterAccountsServiceHandler(service.Server(), hdlr); err != nil { + var hdlr *svc.Service + 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") } diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 3b6c9a6f45c..a37b480e61f 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -34,57 +34,66 @@ import ( ) // 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 // for now recreate index on every start - os.RemoveAll(filepath.Join(cfg.Server.AccountsDataPath, "index.bleve")) - os.MkdirAll(filepath.Join(cfg.Server.AccountsDataPath, "accounts"), 0700) + if err = os.RemoveAll(filepath.Join(cfg.Server.AccountsDataPath, "index.bleve")); err != nil { + return nil, err + } + + if err = os.MkdirAll(filepath.Join(cfg.Server.AccountsDataPath, "accounts"), 0700); err != nil { + return nil, err + } mapping := bleve.NewIndexMapping() + // keep all symbols in terms to allow exact maching, eg. emails mapping.DefaultAnalyzer = keyword.Name - // 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 { - panic(err) + + s = &Service{ + Config: cfg, } - f, err := os.Open(filepath.Join(cfg.Server.AccountsDataPath, "accounts")) - if err != nil { - log.Error().Err(err).Str("dir", filepath.Join(cfg.Server.AccountsDataPath, "accounts")).Msg("could not open acconts folder") - panic(err) + + if s.index, err = bleve.New(filepath.Join(cfg.Server.AccountsDataPath, "index.bleve"), mapping); err != nil { + return + } + 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) f.Close() if err != nil { 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 { path := filepath.Join(cfg.Server.AccountsDataPath, "accounts", file.Name()) - data, err := ioutil.ReadFile(path) - if err != nil { + if data, err = ioutil.ReadFile(path); err != nil { log.Error().Err(err).Str("path", path).Msg("could not read account") continue } a := proto.Account{} - err = json.Unmarshal(data, &a) - if err != nil { + if err = json.Unmarshal(data, &a); err != nil { log.Error().Err(err).Str("path", path).Msg("could not unmarshal account") continue } 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 - s := Service{ - Config: cfg, - index: index, - } + s.Config = cfg - return s + return } // Service implements the AccountsServiceHandler interface