diff --git a/services/app-registry/pkg/command/server.go b/services/app-registry/pkg/command/server.go index 0a85ce0346a..b37f29a7689 100644 --- a/services/app-registry/pkg/command/server.go +++ b/services/app-registry/pkg/command/server.go @@ -73,6 +73,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/audit/pkg/command/server.go b/services/audit/pkg/command/server.go index 442dbf01fa9..84bf206bc9d 100644 --- a/services/audit/pkg/command/server.go +++ b/services/audit/pkg/command/server.go @@ -10,12 +10,10 @@ 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/service/debug" - "github.com/owncloud/ocis/v2/ocis-pkg/version" "github.com/owncloud/ocis/v2/services/audit/pkg/config" "github.com/owncloud/ocis/v2/services/audit/pkg/config/parser" "github.com/owncloud/ocis/v2/services/audit/pkg/logging" + "github.com/owncloud/ocis/v2/services/audit/pkg/server/debug" svc "github.com/owncloud/ocis/v2/services/audit/pkg/service" "github.com/owncloud/ocis/v2/services/audit/pkg/types" ) @@ -58,25 +56,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/audit/pkg/server/debug/option.go b/services/audit/pkg/server/debug/option.go new file mode 100644 index 00000000000..9449006241f --- /dev/null +++ b/services/audit/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/audit/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/audit/pkg/server/debug/server.go b/services/audit/pkg/server/debug/server.go new file mode 100644 index 00000000000..85136a94de8 --- /dev/null +++ b/services/audit/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 +} diff --git a/services/auth-app/pkg/command/server.go b/services/auth-app/pkg/command/server.go index f70aa4e8c95..00b75288776 100644 --- a/services/auth-app/pkg/command/server.go +++ b/services/auth-app/pkg/command/server.go @@ -82,6 +82,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/auth-basic/pkg/command/server.go b/services/auth-basic/pkg/command/server.go index 084fce012ff..475f5333bf0 100644 --- a/services/auth-basic/pkg/command/server.go +++ b/services/auth-basic/pkg/command/server.go @@ -87,6 +87,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/auth-bearer/pkg/command/server.go b/services/auth-bearer/pkg/command/server.go index 00652d312f3..76877780e9f 100644 --- a/services/auth-bearer/pkg/command/server.go +++ b/services/auth-bearer/pkg/command/server.go @@ -74,6 +74,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/auth-machine/pkg/command/server.go b/services/auth-machine/pkg/command/server.go index d90e68b7630..06a315d1dea 100644 --- a/services/auth-machine/pkg/command/server.go +++ b/services/auth-machine/pkg/command/server.go @@ -74,6 +74,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/auth-service/pkg/command/server.go b/services/auth-service/pkg/command/server.go index 2f013c82959..35816e7806b 100644 --- a/services/auth-service/pkg/command/server.go +++ b/services/auth-service/pkg/command/server.go @@ -75,6 +75,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/clientlog/pkg/command/server.go b/services/clientlog/pkg/command/server.go index 0c130019eab..b2194f792b2 100644 --- a/services/clientlog/pkg/command/server.go +++ b/services/clientlog/pkg/command/server.go @@ -11,15 +11,14 @@ 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/registry" - "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/clientlog/pkg/config" "github.com/owncloud/ocis/v2/services/clientlog/pkg/config/parser" "github.com/owncloud/ocis/v2/services/clientlog/pkg/logging" "github.com/owncloud/ocis/v2/services/clientlog/pkg/metrics" + "github.com/owncloud/ocis/v2/services/clientlog/pkg/server/debug" "github.com/owncloud/ocis/v2/services/clientlog/pkg/service" ) @@ -117,25 +116,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/clientlog/pkg/server/debug/option.go b/services/clientlog/pkg/server/debug/option.go new file mode 100644 index 00000000000..1dafbb244ff --- /dev/null +++ b/services/clientlog/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/clientlog/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/clientlog/pkg/server/debug/server.go b/services/clientlog/pkg/server/debug/server.go new file mode 100644 index 00000000000..85136a94de8 --- /dev/null +++ b/services/clientlog/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 +} diff --git a/services/eventhistory/pkg/command/server.go b/services/eventhistory/pkg/command/server.go index b098ccb1975..7107f899756 100644 --- a/services/eventhistory/pkg/command/server.go +++ b/services/eventhistory/pkg/command/server.go @@ -11,8 +11,6 @@ import ( microstore "go-micro.dev/v4/store" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" - "github.com/owncloud/ocis/v2/ocis-pkg/handlers" - "github.com/owncloud/ocis/v2/ocis-pkg/service/debug" ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/ocis-pkg/version" @@ -20,6 +18,7 @@ import ( "github.com/owncloud/ocis/v2/services/eventhistory/pkg/config/parser" "github.com/owncloud/ocis/v2/services/eventhistory/pkg/logging" "github.com/owncloud/ocis/v2/services/eventhistory/pkg/metrics" + "github.com/owncloud/ocis/v2/services/eventhistory/pkg/server/debug" "github.com/owncloud/ocis/v2/services/eventhistory/pkg/server/grpc" ) @@ -94,25 +93,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/eventhistory/pkg/server/debug/option.go b/services/eventhistory/pkg/server/debug/option.go new file mode 100644 index 00000000000..a3966b315d1 --- /dev/null +++ b/services/eventhistory/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/eventhistory/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/eventhistory/pkg/server/debug/server.go b/services/eventhistory/pkg/server/debug/server.go new file mode 100644 index 00000000000..85136a94de8 --- /dev/null +++ b/services/eventhistory/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 +} diff --git a/services/frontend/pkg/command/server.go b/services/frontend/pkg/command/server.go index cd22a4be7f1..8f936935f34 100644 --- a/services/frontend/pkg/command/server.go +++ b/services/frontend/pkg/command/server.go @@ -78,6 +78,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/gateway/pkg/command/server.go b/services/gateway/pkg/command/server.go index 01b2742599c..bafbf7cd828 100644 --- a/services/gateway/pkg/command/server.go +++ b/services/gateway/pkg/command/server.go @@ -75,9 +75,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { - logger.Info(). - Str("server", cfg.Service.Name). - Msg("Shutting down debug erver") + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/groups/pkg/command/server.go b/services/groups/pkg/command/server.go index cdd3db0ec2b..f79bc953e13 100644 --- a/services/groups/pkg/command/server.go +++ b/services/groups/pkg/command/server.go @@ -87,6 +87,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/idp/pkg/command/server.go b/services/idp/pkg/command/server.go index 02bf30eaa0f..7f02e1bb2c7 100644 --- a/services/idp/pkg/command/server.go +++ b/services/idp/pkg/command/server.go @@ -94,7 +94,7 @@ func Server(cfg *config.Config) *cli.Command { } { - server, err := debug.Server( + debugServer, err := debug.Server( debug.Logger(logger), debug.Context(ctx), debug.Config(cfg), @@ -104,8 +104,8 @@ func Server(cfg *config.Config) *cli.Command { 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/invitations/pkg/command/server.go b/services/invitations/pkg/command/server.go index 0b2b358cda6..24f00038049 100644 --- a/services/invitations/pkg/command/server.go +++ b/services/invitations/pkg/command/server.go @@ -87,7 +87,7 @@ func Server(cfg *config.Config) *cli.Command { } { - server, err := debug.Server( + debugServer, err := debug.Server( debug.Logger(logger), debug.Context(ctx), debug.Config(cfg), @@ -97,9 +97,8 @@ func Server(cfg *config.Config) *cli.Command { return err } - gr.Add(server.ListenAndServe, func(err error) { - logger.Error().Err(err) - _ = server.Shutdown(ctx) + gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) } diff --git a/services/nats/pkg/command/server.go b/services/nats/pkg/command/server.go index f0ad160976d..c5401b2db98 100644 --- a/services/nats/pkg/command/server.go +++ b/services/nats/pkg/command/server.go @@ -11,12 +11,10 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" pkgcrypto "github.com/owncloud/ocis/v2/ocis-pkg/crypto" - "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" "github.com/owncloud/ocis/v2/services/nats/pkg/config" "github.com/owncloud/ocis/v2/services/nats/pkg/config/parser" "github.com/owncloud/ocis/v2/services/nats/pkg/logging" + "github.com/owncloud/ocis/v2/services/nats/pkg/server/debug" "github.com/owncloud/ocis/v2/services/nats/pkg/server/nats" ) @@ -38,25 +36,18 @@ func Server(cfg *config.Config) *cli.Command { defer cancel() { - 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("transport", "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/nats/pkg/server/debug/option.go b/services/nats/pkg/server/debug/option.go new file mode 100644 index 00000000000..977b85f3b9c --- /dev/null +++ b/services/nats/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/nats/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/nats/pkg/server/debug/server.go b/services/nats/pkg/server/debug/server.go new file mode 100644 index 00000000000..85136a94de8 --- /dev/null +++ b/services/nats/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 +} diff --git a/services/notifications/pkg/command/server.go b/services/notifications/pkg/command/server.go index 45fc693b831..8aa1e1bec75 100644 --- a/services/notifications/pkg/command/server.go +++ b/services/notifications/pkg/command/server.go @@ -12,17 +12,15 @@ import ( "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" - "github.com/owncloud/ocis/v2/ocis-pkg/handlers" "github.com/owncloud/ocis/v2/ocis-pkg/registry" - "github.com/owncloud/ocis/v2/ocis-pkg/service/debug" "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" - "github.com/owncloud/ocis/v2/ocis-pkg/version" settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0" "github.com/owncloud/ocis/v2/services/notifications/pkg/channels" "github.com/owncloud/ocis/v2/services/notifications/pkg/config" "github.com/owncloud/ocis/v2/services/notifications/pkg/config/parser" "github.com/owncloud/ocis/v2/services/notifications/pkg/logging" + "github.com/owncloud/ocis/v2/services/notifications/pkg/server/debug" "github.com/owncloud/ocis/v2/services/notifications/pkg/service" ) @@ -59,25 +57,18 @@ func Server(cfg *config.Config) *cli.Command { defer cancel() { - 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("transport", "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/notifications/pkg/server/debug/option.go b/services/notifications/pkg/server/debug/option.go new file mode 100644 index 00000000000..9ecb9364fef --- /dev/null +++ b/services/notifications/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/notifications/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/notifications/pkg/server/debug/server.go b/services/notifications/pkg/server/debug/server.go new file mode 100644 index 00000000000..85136a94de8 --- /dev/null +++ b/services/notifications/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 +} diff --git a/services/ocm/pkg/command/server.go b/services/ocm/pkg/command/server.go index 2e28cccedcd..7e7265b066d 100644 --- a/services/ocm/pkg/command/server.go +++ b/services/ocm/pkg/command/server.go @@ -75,6 +75,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/policies/pkg/command/server.go b/services/policies/pkg/command/server.go index 3a68b35125c..832909400ea 100644 --- a/services/policies/pkg/command/server.go +++ b/services/policies/pkg/command/server.go @@ -9,9 +9,7 @@ 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/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/ocis-pkg/version" @@ -19,6 +17,7 @@ import ( "github.com/owncloud/ocis/v2/services/policies/pkg/config" "github.com/owncloud/ocis/v2/services/policies/pkg/config/parser" "github.com/owncloud/ocis/v2/services/policies/pkg/engine/opa" + "github.com/owncloud/ocis/v2/services/policies/pkg/server/debug" svcEvent "github.com/owncloud/ocis/v2/services/policies/pkg/service/event" svcGRPC "github.com/owncloud/ocis/v2/services/policies/pkg/service/grpc" ) @@ -121,25 +120,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("transport", "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/policies/pkg/server/debug/option.go b/services/policies/pkg/server/debug/option.go new file mode 100644 index 00000000000..aaf0a8d7a86 --- /dev/null +++ b/services/policies/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/policies/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/policies/pkg/server/debug/server.go b/services/policies/pkg/server/debug/server.go new file mode 100644 index 00000000000..85136a94de8 --- /dev/null +++ b/services/policies/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 +} diff --git a/services/postprocessing/pkg/command/server.go b/services/postprocessing/pkg/command/server.go index a51289783b1..8527b71dc7b 100644 --- a/services/postprocessing/pkg/command/server.go +++ b/services/postprocessing/pkg/command/server.go @@ -11,13 +11,11 @@ import ( "github.com/urfave/cli/v2" microstore "go-micro.dev/v4/store" - "github.com/owncloud/ocis/v2/ocis-pkg/handlers" - "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/postprocessing/pkg/config" "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config/parser" "github.com/owncloud/ocis/v2/services/postprocessing/pkg/logging" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/server/debug" "github.com/owncloud/ocis/v2/services/postprocessing/pkg/service" ) @@ -87,25 +85,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("transport", "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/postprocessing/pkg/server/debug/option.go b/services/postprocessing/pkg/server/debug/option.go new file mode 100644 index 00000000000..da002867f78 --- /dev/null +++ b/services/postprocessing/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/postprocessing/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/postprocessing/pkg/server/debug/server.go b/services/postprocessing/pkg/server/debug/server.go new file mode 100644 index 00000000000..85136a94de8 --- /dev/null +++ b/services/postprocessing/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 +} diff --git a/services/proxy/pkg/command/server.go b/services/proxy/pkg/command/server.go index ae17b743a1b..7dd1673be86 100644 --- a/services/proxy/pkg/command/server.go +++ b/services/proxy/pkg/command/server.go @@ -214,7 +214,7 @@ func Server(cfg *config.Config) *cli.Command { } { - server, err := debug.Server( + debugServer, err := debug.Server( debug.Logger(logger), debug.Context(ctx), debug.Config(cfg), @@ -224,8 +224,8 @@ func Server(cfg *config.Config) *cli.Command { 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/search/pkg/command/server.go b/services/search/pkg/command/server.go index 1e823e42e4e..f098602a67b 100644 --- a/services/search/pkg/command/server.go +++ b/services/search/pkg/command/server.go @@ -70,7 +70,7 @@ func Server(cfg *config.Config) *cli.Command { cancel() }) - server, err := debug.Server( + debugServer, err := debug.Server( debug.Logger(logger), debug.Context(ctx), debug.Config(cfg), @@ -80,8 +80,8 @@ func Server(cfg *config.Config) *cli.Command { 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/settings/pkg/command/server.go b/services/settings/pkg/command/server.go index 5b11f00a56e..1e442ab6fd2 100644 --- a/services/settings/pkg/command/server.go +++ b/services/settings/pkg/command/server.go @@ -94,7 +94,11 @@ func Server(cfg *config.Config) *cli.Command { }) // prepare a debug server and add it to the group run. - debugServer, err := debug.Server(debug.Logger(logger), debug.Context(ctx), debug.Config(cfg)) + debugServer, err := debug.Server( + debug.Logger(logger), + debug.Context(ctx), + debug.Config(cfg), + ) if err != nil { logger.Error().Err(err).Str("server", "debug").Msg("Failed to initialize server") return err diff --git a/services/sse/pkg/command/server.go b/services/sse/pkg/command/server.go index cbac80f3e2f..722eb3970db 100644 --- a/services/sse/pkg/command/server.go +++ b/services/sse/pkg/command/server.go @@ -10,13 +10,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/sse/pkg/config" "github.com/owncloud/ocis/v2/services/sse/pkg/config/parser" + "github.com/owncloud/ocis/v2/services/sse/pkg/server/debug" "github.com/owncloud/ocis/v2/services/sse/pkg/server/http" ) @@ -77,25 +75,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("transport", "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/sse/pkg/server/debug/option.go b/services/sse/pkg/server/debug/option.go new file mode 100644 index 00000000000..03d14d82f64 --- /dev/null +++ b/services/sse/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/sse/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/sse/pkg/server/debug/server.go b/services/sse/pkg/server/debug/server.go new file mode 100644 index 00000000000..85136a94de8 --- /dev/null +++ b/services/sse/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 +} diff --git a/services/storage-publiclink/pkg/command/server.go b/services/storage-publiclink/pkg/command/server.go index 184254cb9ff..d15dac8ceca 100644 --- a/services/storage-publiclink/pkg/command/server.go +++ b/services/storage-publiclink/pkg/command/server.go @@ -74,6 +74,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/storage-shares/pkg/command/server.go b/services/storage-shares/pkg/command/server.go index bdeabf0e2d8..41757ff57d9 100644 --- a/services/storage-shares/pkg/command/server.go +++ b/services/storage-shares/pkg/command/server.go @@ -74,6 +74,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/storage-system/pkg/command/server.go b/services/storage-system/pkg/command/server.go index 5195280e416..2b50a59a0e9 100644 --- a/services/storage-system/pkg/command/server.go +++ b/services/storage-system/pkg/command/server.go @@ -74,6 +74,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/storage-users/pkg/command/server.go b/services/storage-users/pkg/command/server.go index df7fca8a4b6..f88ed590c9b 100644 --- a/services/storage-users/pkg/command/server.go +++ b/services/storage-users/pkg/command/server.go @@ -76,15 +76,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(err error) { - logger.Error().Err(err).Str("server", cfg.Service.Name). - Msg("Shutting down debug server") - if err := debugServer.Shutdown(context.Background()); err != nil { - logger.Error(). - Err(err). - Str("server", cfg.Service.Name). - Msg("Error during debug server shutdown") - } - + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/thumbnails/pkg/command/server.go b/services/thumbnails/pkg/command/server.go index cc671f74f25..4ad5ae0fdd7 100644 --- a/services/thumbnails/pkg/command/server.go +++ b/services/thumbnails/pkg/command/server.go @@ -74,6 +74,7 @@ func Server(cfg *config.Config) *cli.Command { server, err := debug.Server( debug.Logger(logger), debug.Config(cfg), + debug.Context(ctx), ) if err != nil { logger.Info().Err(err).Str("transport", "debug").Msg("Failed to initialize server") diff --git a/services/thumbnails/pkg/server/debug/option.go b/services/thumbnails/pkg/server/debug/option.go index 534a47f1ccd..fdc0fe6bc8e 100644 --- a/services/thumbnails/pkg/server/debug/option.go +++ b/services/thumbnails/pkg/server/debug/option.go @@ -1,6 +1,8 @@ package debug import ( + "context" + "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/thumbnails/pkg/config" ) @@ -13,6 +15,7 @@ type Options struct { Name string Address string Logger log.Logger + Context context.Context Config *config.Config } @@ -34,6 +37,13 @@ func Logger(val log.Logger) Option { } } +// 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) { diff --git a/services/userlog/pkg/command/server.go b/services/userlog/pkg/command/server.go index 50c134d7de7..566c5f76e7c 100644 --- a/services/userlog/pkg/command/server.go +++ b/services/userlog/pkg/command/server.go @@ -13,9 +13,7 @@ import ( microstore "go-micro.dev/v4/store" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" - "github.com/owncloud/ocis/v2/ocis-pkg/handlers" "github.com/owncloud/ocis/v2/ocis-pkg/registry" - "github.com/owncloud/ocis/v2/ocis-pkg/service/debug" ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/ocis-pkg/version" @@ -25,6 +23,7 @@ import ( "github.com/owncloud/ocis/v2/services/userlog/pkg/config/parser" "github.com/owncloud/ocis/v2/services/userlog/pkg/logging" "github.com/owncloud/ocis/v2/services/userlog/pkg/metrics" + "github.com/owncloud/ocis/v2/services/userlog/pkg/server/debug" "github.com/owncloud/ocis/v2/services/userlog/pkg/server/http" ) @@ -145,25 +144,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("transport", "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/userlog/pkg/server/debug/option.go b/services/userlog/pkg/server/debug/option.go new file mode 100644 index 00000000000..6e3607ac636 --- /dev/null +++ b/services/userlog/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/userlog/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/userlog/pkg/server/debug/server.go b/services/userlog/pkg/server/debug/server.go new file mode 100644 index 00000000000..85136a94de8 --- /dev/null +++ b/services/userlog/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 +} diff --git a/services/users/pkg/command/server.go b/services/users/pkg/command/server.go index 95a899873da..aed4caa147c 100644 --- a/services/users/pkg/command/server.go +++ b/services/users/pkg/command/server.go @@ -87,6 +87,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) cancel() }) diff --git a/services/web/pkg/command/server.go b/services/web/pkg/command/server.go index 5ba91343195..73ceba8cce1 100644 --- a/services/web/pkg/command/server.go +++ b/services/web/pkg/command/server.go @@ -93,7 +93,7 @@ func Server(cfg *config.Config) *cli.Command { } { - server, err := debug.Server( + debugServer, err := debug.Server( debug.Logger(logger), debug.Context(ctx), debug.Config(cfg), @@ -103,8 +103,8 @@ func Server(cfg *config.Config) *cli.Command { 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/webdav/pkg/command/server.go b/services/webdav/pkg/command/server.go index d7ed386242d..869b80f0ca1 100644 --- a/services/webdav/pkg/command/server.go +++ b/services/webdav/pkg/command/server.go @@ -82,7 +82,7 @@ func Server(cfg *config.Config) *cli.Command { } { - server, err := debug.Server( + debugServer, err := debug.Server( debug.Logger(logger), debug.Context(ctx), debug.Config(cfg), @@ -93,9 +93,8 @@ func Server(cfg *config.Config) *cli.Command { return err } - gr.Add(server.ListenAndServe, func(err error) { - logger.Error().Err(err) - _ = server.Shutdown(ctx) + gr.Add(debugServer.ListenAndServe, func(err error) { + _ = debugServer.Shutdown(ctx) cancel() }) } diff --git a/services/webfinger/pkg/command/server.go b/services/webfinger/pkg/command/server.go index 0ba9cbe1298..0088349687d 100644 --- a/services/webfinger/pkg/command/server.go +++ b/services/webfinger/pkg/command/server.go @@ -95,7 +95,7 @@ func Server(cfg *config.Config) *cli.Command { } { - server, err := debug.Server( + debugServer, err := debug.Server( debug.Logger(logger), debug.Context(ctx), debug.Config(cfg), @@ -106,9 +106,8 @@ func Server(cfg *config.Config) *cli.Command { return err } - gr.Add(server.ListenAndServe, func(err error) { - logger.Error().Err(err) - _ = server.Shutdown(ctx) + gr.Add(debugServer.ListenAndServe, func(err error) { + _ = debugServer.Shutdown(ctx) cancel() }) }