Skip to content

Commit

Permalink
Merge pull request #36 from butonic/return-error
Browse files Browse the repository at this point in the history
allow handler to return an error
  • Loading branch information
butonic authored Jun 18, 2020
2 parents 02e759f + 0cdeeb7 commit 3ca3bd4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
11 changes: 8 additions & 3 deletions pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package command

import (
"context"
"fmt"
"syscall"

"github.com/owncloud/ocis-accounts/pkg/flagset"
Expand Down Expand Up @@ -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()
})

Expand Down
9 changes: 7 additions & 2 deletions pkg/server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
53 changes: 31 additions & 22 deletions pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3ca3bd4

Please sign in to comment.