diff --git a/services/activitylog/pkg/command/server.go b/services/activitylog/pkg/command/server.go index d2b4b8c9a..9c018ecdb 100644 --- a/services/activitylog/pkg/command/server.go +++ b/services/activitylog/pkg/command/server.go @@ -150,7 +150,6 @@ func Server(cfg *config.Config) *cli.Command { debug.Context(ctx), debug.Config(cfg), ) - if err != nil { logger.Info().Err(err).Str("server", "debug").Msg("Failed to initialize server") return err diff --git a/services/antivirus/pkg/command/server.go b/services/antivirus/pkg/command/server.go index d80355964..d259f0f5e 100644 --- a/services/antivirus/pkg/command/server.go +++ b/services/antivirus/pkg/command/server.go @@ -8,13 +8,11 @@ import ( "github.com/urfave/cli/v2" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" - "github.com/owncloud/ocis/v2/ocis-pkg/handlers" "github.com/owncloud/ocis/v2/ocis-pkg/log" - "github.com/owncloud/ocis/v2/ocis-pkg/service/debug" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" - "github.com/owncloud/ocis/v2/ocis-pkg/version" "github.com/owncloud/ocis/v2/services/antivirus/pkg/config" "github.com/owncloud/ocis/v2/services/antivirus/pkg/config/parser" + "github.com/owncloud/ocis/v2/services/antivirus/pkg/server/debug" "github.com/owncloud/ocis/v2/services/antivirus/pkg/service" ) @@ -56,25 +54,18 @@ func Server(cfg *config.Config) *cli.Command { } { - checkHandler := handlers.NewCheckHandler( - handlers.NewCheckHandlerConfiguration(). - WithLogger(logger), - ) - - server := debug.NewService( + debugServer, err := debug.Server( debug.Logger(logger), - debug.Name(cfg.Service.Name), - debug.Version(version.GetString()), - debug.Address(cfg.Debug.Addr), - debug.Token(cfg.Debug.Token), - debug.Pprof(cfg.Debug.Pprof), - debug.Zpages(cfg.Debug.Zpages), - debug.Health(checkHandler), - debug.Ready(checkHandler), + debug.Context(ctx), + debug.Config(cfg), ) + if err != nil { + logger.Info().Err(err).Str("server", "debug").Msg("Failed to initialize server") + return err + } - gr.Add(server.ListenAndServe, func(_ error) { - _ = server.Shutdown(ctx) + gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) } diff --git a/services/antivirus/pkg/server/debug/option.go b/services/antivirus/pkg/server/debug/option.go new file mode 100644 index 000000000..1118208c8 --- /dev/null +++ b/services/antivirus/pkg/server/debug/option.go @@ -0,0 +1,50 @@ +package debug + +import ( + "context" + + "github.com/owncloud/ocis/v2/ocis-pkg/log" + "github.com/owncloud/ocis/v2/services/antivirus/pkg/config" +) + +// Option defines a single option function. +type Option func(o *Options) + +// Options defines the available options for this package. +type Options struct { + Logger log.Logger + Context context.Context + Config *config.Config +} + +// newOptions initializes the available default options. +func newOptions(opts ...Option) Options { + opt := Options{} + + for _, o := range opts { + o(&opt) + } + + return opt +} + +// Logger provides a function to set the logger option. +func Logger(val log.Logger) Option { + return func(o *Options) { + o.Logger = val + } +} + +// Context provides a function to set the context option. +func Context(val context.Context) Option { + return func(o *Options) { + o.Context = val + } +} + +// Config provides a function to set the config option. +func Config(val *config.Config) Option { + return func(o *Options) { + o.Config = val + } +} diff --git a/services/antivirus/pkg/server/debug/server.go b/services/antivirus/pkg/server/debug/server.go new file mode 100644 index 000000000..85136a94d --- /dev/null +++ b/services/antivirus/pkg/server/debug/server.go @@ -0,0 +1,31 @@ +package debug + +import ( + "net/http" + + "github.com/owncloud/ocis/v2/ocis-pkg/handlers" + "github.com/owncloud/ocis/v2/ocis-pkg/service/debug" + "github.com/owncloud/ocis/v2/ocis-pkg/version" +) + +// Server initializes the debug service and server. +func Server(opts ...Option) (*http.Server, error) { + options := newOptions(opts...) + + checkHandler := handlers.NewCheckHandler( + handlers.NewCheckHandlerConfiguration(). + WithLogger(options.Logger), + ) + + return debug.NewService( + debug.Logger(options.Logger), + debug.Name(options.Config.Service.Name), + debug.Version(version.GetString()), + debug.Address(options.Config.Debug.Addr), + debug.Token(options.Config.Debug.Token), + debug.Pprof(options.Config.Debug.Pprof), + debug.Zpages(options.Config.Debug.Zpages), + debug.Health(checkHandler), + debug.Ready(checkHandler), + ), nil +}