diff --git a/services/settings/pkg/command/server.go b/services/settings/pkg/command/server.go index 98f55f3f6b..cd48ba639e 100644 --- a/services/settings/pkg/command/server.go +++ b/services/settings/pkg/command/server.go @@ -15,6 +15,7 @@ import ( "github.com/owncloud/ocis/v2/services/settings/pkg/server/debug" "github.com/owncloud/ocis/v2/services/settings/pkg/server/grpc" "github.com/owncloud/ocis/v2/services/settings/pkg/server/http" + svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0" "github.com/owncloud/ocis/v2/services/settings/pkg/tracing" "github.com/urfave/cli/v2" ) @@ -51,6 +52,8 @@ func Server(cfg *config.Config) *cli.Command { mtrcs := metrics.New() mtrcs.BuildInfo.WithLabelValues(version.GetString()).Set(1) + handle := svc.NewService(cfg, logger) + // prepare an HTTP server and add it to the group run. httpServer, err := http.Server( http.Name(cfg.Service.Name), @@ -58,6 +61,7 @@ func Server(cfg *config.Config) *cli.Command { http.Context(ctx), http.Config(cfg), http.Metrics(mtrcs), + http.ServiceHandler(handle), ) if err != nil { logger.Error(). @@ -71,7 +75,14 @@ func Server(cfg *config.Config) *cli.Command { }) // prepare a gRPC server and add it to the group run. - grpcServer := grpc.Server(grpc.Name(cfg.Service.Name), grpc.Logger(logger), grpc.Context(ctx), grpc.Config(cfg), grpc.Metrics(mtrcs)) + grpcServer := grpc.Server( + grpc.Name(cfg.Service.Name), + grpc.Logger(logger), + grpc.Context(ctx), + grpc.Config(cfg), + grpc.Metrics(mtrcs), + grpc.ServiceHandler(handle), + ) servers.Add(grpcServer.Run, func(_ error) { logger.Info().Str("server", "grpc").Msg("Shutting down server") cancel() diff --git a/services/settings/pkg/server/grpc/option.go b/services/settings/pkg/server/grpc/option.go index b87b02813d..afcc6a39b8 100644 --- a/services/settings/pkg/server/grpc/option.go +++ b/services/settings/pkg/server/grpc/option.go @@ -6,6 +6,7 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/settings/pkg/config" "github.com/owncloud/ocis/v2/services/settings/pkg/metrics" + svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0" "github.com/urfave/cli/v2" ) @@ -14,12 +15,13 @@ type Option func(o *Options) // Options defines the available options for this package. type Options struct { - Name string - Logger log.Logger - Context context.Context - Config *config.Config - Metrics *metrics.Metrics - Flags []cli.Flag + Name string + Logger log.Logger + Context context.Context + Config *config.Config + Metrics *metrics.Metrics + ServiceHandler svc.Service + Flags []cli.Flag } // newOptions initializes the available default options. @@ -74,3 +76,10 @@ func Flags(val []cli.Flag) Option { o.Flags = append(o.Flags, val...) } } + +// ServiceHandler provides a function to set the ServiceHandler option +func ServiceHandler(val svc.Service) Option { + return func(o *Options) { + o.ServiceHandler = val + } +} diff --git a/services/settings/pkg/server/grpc/server.go b/services/settings/pkg/server/grpc/server.go index 7e448ee62d..4cad0fdd15 100644 --- a/services/settings/pkg/server/grpc/server.go +++ b/services/settings/pkg/server/grpc/server.go @@ -7,7 +7,6 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/version" settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0" - svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0" "go-micro.dev/v4/api" "go-micro.dev/v4/server" ) @@ -34,7 +33,7 @@ func Server(opts ...Option) grpc.Service { options.Logger.Fatal().Err(err).Msg("Error creating settings service") } - handle := svc.NewService(options.Config, options.Logger) + handle := options.ServiceHandler if err := settingssvc.RegisterBundleServiceHandler(service.Server(), handle); err != nil { options.Logger.Fatal().Err(err).Msg("could not register Bundle service handler") } diff --git a/services/settings/pkg/server/http/option.go b/services/settings/pkg/server/http/option.go index e4537ffb12..f3e3fdcb71 100644 --- a/services/settings/pkg/server/http/option.go +++ b/services/settings/pkg/server/http/option.go @@ -6,6 +6,7 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/settings/pkg/config" "github.com/owncloud/ocis/v2/services/settings/pkg/metrics" + svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0" "github.com/urfave/cli/v2" ) @@ -14,12 +15,13 @@ type Option func(o *Options) // Options defines the available options for this package. type Options struct { - Name string - Logger log.Logger - Context context.Context - Config *config.Config - Metrics *metrics.Metrics - Flags []cli.Flag + Name string + Logger log.Logger + Context context.Context + Config *config.Config + Metrics *metrics.Metrics + ServiceHandler svc.Service + Flags []cli.Flag } // newOptions initializes the available default options. @@ -74,3 +76,10 @@ func Flags(val []cli.Flag) Option { o.Flags = append(o.Flags, val...) } } + +// ServiceHandler provides a function to set the ServiceHandler option +func ServiceHandler(val svc.Service) Option { + return func(o *Options) { + o.ServiceHandler = val + } +} diff --git a/services/settings/pkg/server/http/server.go b/services/settings/pkg/server/http/server.go index c6d76dd646..3fcb4a4691 100644 --- a/services/settings/pkg/server/http/server.go +++ b/services/settings/pkg/server/http/server.go @@ -37,7 +37,7 @@ func Server(opts ...Option) (ohttp.Service, error) { return ohttp.Service{}, fmt.Errorf("could not initialize http service: %w", err) } - handle := svc.NewService(options.Config, options.Logger) + handle := options.ServiceHandler { handle = svc.NewInstrument(handle, options.Metrics) diff --git a/services/settings/pkg/store/metadata/cache.go b/services/settings/pkg/store/metadata/cache.go index 5b5242a16b..50072606fa 100644 --- a/services/settings/pkg/store/metadata/cache.go +++ b/services/settings/pkg/store/metadata/cache.go @@ -11,10 +11,6 @@ import ( var ( cachettl = 0 - // these need to be global instances for now as the `Service` (and therefore the `Store`) are instantiated twice (for grpc and http) - // therefore caches need to cover both instances - dircache = initCache(cachettl) - filescache = initCache(cachettl) ) // CachedMDC is cache for the metadataclient @@ -99,8 +95,8 @@ func (c *CachedMDC) MakeDirIfNotExist(ctx context.Context, id string) error { // Init instantiates the caches func (c *CachedMDC) Init(ctx context.Context, id string) error { - c.dirs = dircache - c.files = filescache + c.dirs = initCache(cachettl) + c.files = initCache(cachettl) return c.next.Init(ctx, id) }