diff --git a/pkg/command/server.go b/pkg/command/server.go index 4ea529872..cea54cf3c 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -43,6 +43,11 @@ func Server(cfg *config.Config) *cli.Command { defer cancel() + handler, err := svc.New(svc.Logger(logger), svc.Config(cfg)) + if err != nil { + logger.Fatal().Err(err).Msg("could not initialize service handler") + } + { server := http.Server( http.Logger(logger), @@ -52,6 +57,7 @@ func Server(cfg *config.Config) *cli.Command { http.Metrics(mtrcs), http.Flags(flagset.RootWithConfig(cfg)), http.Flags(flagset.ServerWithConfig(cfg)), + http.Handler(handler), ) gr.Add(server.Run, func(_ error) { @@ -70,6 +76,7 @@ func Server(cfg *config.Config) *cli.Command { grpc.Context(ctx), grpc.Config(cfg), grpc.Metrics(mtrcs), + grpc.Handler(handler), ) gr.Add(func() error { diff --git a/pkg/server/grpc/option.go b/pkg/server/grpc/option.go index 06e8538e9..7e6a5a52e 100644 --- a/pkg/server/grpc/option.go +++ b/pkg/server/grpc/option.go @@ -6,6 +6,7 @@ import ( "github.com/micro/cli/v2" "github.com/owncloud/ocis-accounts/pkg/config" "github.com/owncloud/ocis-accounts/pkg/metrics" + svc "github.com/owncloud/ocis-accounts/pkg/service/v0" "github.com/owncloud/ocis-pkg/v2/log" ) @@ -20,6 +21,7 @@ type Options struct { Config *config.Config Metrics *metrics.Metrics Flags []cli.Flag + Handler *svc.Service } // newOptions initializes the available default options. @@ -74,3 +76,10 @@ func Flags(val []cli.Flag) Option { o.Flags = append(o.Flags, val...) } } + +// Handler provides a function to set the handler option. +func Handler(val *svc.Service) Option { + return func(o *Options) { + o.Handler = val + } +} diff --git a/pkg/server/grpc/server.go b/pkg/server/grpc/server.go index 22717a3d8..af5b0c602 100644 --- a/pkg/server/grpc/server.go +++ b/pkg/server/grpc/server.go @@ -1,19 +1,14 @@ package grpc import ( - "time" - - mclient "github.com/micro/go-micro/v2/client" "github.com/owncloud/ocis-accounts/pkg/proto/v0" - svc "github.com/owncloud/ocis-accounts/pkg/service/v0" - "github.com/owncloud/ocis-pkg/v2/roles" "github.com/owncloud/ocis-pkg/v2/service/grpc" - settings "github.com/owncloud/ocis-settings/pkg/proto/v0" ) // Server initializes a new go-micro service ready to run func Server(opts ...Option) grpc.Service { options := newOptions(opts...) + handler := options.Handler service := grpc.NewService( grpc.Name(options.Config.Server.Name), @@ -24,31 +19,10 @@ func Server(opts ...Option) grpc.Service { grpc.Flags(options.Flags...), ) - var hdlr *svc.Service - var err error - - // TODO this won't work with a registry other than mdns. Look into Micro's client initialization. - // https://github.com/owncloud/ocis-proxy/issues/38 - rs := settings.NewRoleService("com.owncloud.api.settings", mclient.DefaultClient) - roleManager := roles.NewManager( - roles.CacheSize(1024), - roles.CacheTTL(time.Hour*24*7), - roles.Logger(options.Logger), - roles.RoleService(rs), - ) - - if hdlr, err = svc.New( - svc.Logger(options.Logger), - svc.Config(options.Config), - svc.RoleManager(&roleManager), - svc.RoleService(rs), - ); err != nil { - options.Logger.Fatal().Err(err).Msg("could not initialize service handler") - } - if err = proto.RegisterAccountsServiceHandler(service.Server(), hdlr); err != nil { + if err := proto.RegisterAccountsServiceHandler(service.Server(), handler); err != nil { options.Logger.Fatal().Err(err).Msg("could not register service handler") } - if err = proto.RegisterGroupsServiceHandler(service.Server(), hdlr); err != nil { + if err := proto.RegisterGroupsServiceHandler(service.Server(), handler); err != nil { options.Logger.Fatal().Err(err).Msg("could not register groups handler") } diff --git a/pkg/server/http/option.go b/pkg/server/http/option.go index 3687b735b..069b0136e 100644 --- a/pkg/server/http/option.go +++ b/pkg/server/http/option.go @@ -6,6 +6,7 @@ import ( "github.com/micro/cli/v2" "github.com/owncloud/ocis-accounts/pkg/config" "github.com/owncloud/ocis-accounts/pkg/metrics" + svc "github.com/owncloud/ocis-accounts/pkg/service/v0" "github.com/owncloud/ocis-pkg/v2/log" ) @@ -20,6 +21,7 @@ type Options struct { Config *config.Config Metrics *metrics.Metrics Flags []cli.Flag + Handler *svc.Service } // newOptions initializes the available default options. @@ -74,3 +76,10 @@ func Flags(val []cli.Flag) Option { o.Flags = append(o.Flags, val...) } } + +// Handler provides a function to set the handler option. +func Handler(val *svc.Service) Option { + return func(o *Options) { + o.Handler = val + } +} diff --git a/pkg/server/http/server.go b/pkg/server/http/server.go index 64fef9dd0..e931bcebc 100644 --- a/pkg/server/http/server.go +++ b/pkg/server/http/server.go @@ -1,24 +1,19 @@ package http import ( - "time" - "github.com/go-chi/chi" - mclient "github.com/micro/go-micro/v2/client" "github.com/owncloud/ocis-accounts/pkg/assets" "github.com/owncloud/ocis-accounts/pkg/proto/v0" - svc "github.com/owncloud/ocis-accounts/pkg/service/v0" "github.com/owncloud/ocis-accounts/pkg/version" "github.com/owncloud/ocis-pkg/v2/account" "github.com/owncloud/ocis-pkg/v2/middleware" - "github.com/owncloud/ocis-pkg/v2/roles" "github.com/owncloud/ocis-pkg/v2/service/http" - settings "github.com/owncloud/ocis-settings/pkg/proto/v0" ) // Server initializes the http service and server. func Server(opts ...Option) http.Service { options := newOptions(opts...) + handler := options.Handler service := http.NewService( http.Logger(options.Logger), @@ -30,25 +25,6 @@ func Server(opts ...Option) http.Service { http.Flags(options.Flags...), ) - // TODO this won't work with a registry other than mdns. Look into Micro's client initialization. - // https://github.com/owncloud/ocis-proxy/issues/38 - rs := settings.NewRoleService("com.owncloud.api.settings", mclient.DefaultClient) - roleManager := roles.NewManager( - roles.CacheSize(1024), - roles.CacheTTL(time.Hour*24*7), - roles.Logger(options.Logger), - roles.RoleService(rs), - ) - handler, err := svc.New( - svc.Logger(options.Logger), - svc.Config(options.Config), - svc.RoleManager(&roleManager), - svc.RoleService(rs), - ) - if err != nil { - options.Logger.Fatal().Err(err).Msg("could not initialize service handler") - } - mux := chi.NewMux() mux.Use(middleware.RealIP) diff --git a/pkg/service/v0/option.go b/pkg/service/v0/option.go index 559f992a2..86144ad08 100644 --- a/pkg/service/v0/option.go +++ b/pkg/service/v0/option.go @@ -3,8 +3,6 @@ package service import ( "github.com/owncloud/ocis-accounts/pkg/config" "github.com/owncloud/ocis-pkg/v2/log" - "github.com/owncloud/ocis-pkg/v2/roles" - settings "github.com/owncloud/ocis-settings/pkg/proto/v0" ) // Option defines a single option function. @@ -14,8 +12,6 @@ type Option func(o *Options) type Options struct { Logger log.Logger Config *config.Config - RoleService settings.RoleService - RoleManager *roles.Manager } func newOptions(opts ...Option) Options { @@ -41,17 +37,3 @@ func Config(val *config.Config) Option { o.Config = val } } - -// RoleService provides a function to set the role service option. -func RoleService(val settings.RoleService) Option { - return func(o *Options) { - o.RoleService = val - } -} - -// RoleManager provides a function to set the roles manager option. -func RoleManager(val *roles.Manager) Option { - return func(o *Options) { - o.RoleManager = val - } -} diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 1e967ff46..a255c7022 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -5,10 +5,12 @@ import ( "encoding/json" "errors" "fmt" + mclient "github.com/micro/go-micro/v2/client" "io/ioutil" "os" "path/filepath" "strings" + "time" "github.com/blevesearch/bleve" "github.com/blevesearch/bleve/analysis/analyzer/custom" @@ -30,10 +32,18 @@ func New(opts ...Option) (s *Service, err error) { options := newOptions(opts...) logger := options.Logger cfg := options.Config - roleService := options.RoleService - roleManager := options.RoleManager - // read all user and group records + // TODO this won't work with a registry other than mdns. Look into Micro's client initialization. + // https://github.com/owncloud/ocis-proxy/issues/38 + roleService := settings.NewRoleService("com.owncloud.api.settings", mclient.DefaultClient) + roleManager := roles.NewManager( + roles.CacheSize(1024), + roles.CacheTTL(time.Hour*24*7), + roles.Logger(options.Logger), + roles.RoleService(roleService), + ) + + // read all user and group records accountsDir := filepath.Join(cfg.Server.AccountsDataPath, "accounts") { // check if accounts exist @@ -326,7 +336,7 @@ func New(opts ...Option) (s *Service, err error) { log: logger, Config: cfg, RoleService: roleService, - RoleManager: roleManager, + RoleManager: &roleManager, } indexDir := filepath.Join(cfg.Server.AccountsDataPath, "index.bleve")