From f240d4e7cb70ac8536ee393f01a6fc92a2c4e419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Wed, 20 Mar 2024 18:45:03 +0100 Subject: [PATCH] feat: include debug server in collaboration --- services/collaboration/pkg/command/server.go | 35 ++++++----- services/collaboration/pkg/config/config.go | 2 +- .../pkg/config/defaults/defaultconfig.go | 6 ++ .../collaboration/pkg/server/debug/option.go | 50 +++++++++++++++ .../collaboration/pkg/server/debug/server.go | 63 +++++++++++++++++++ 5 files changed, 138 insertions(+), 18 deletions(-) create mode 100644 services/collaboration/pkg/server/debug/option.go create mode 100644 services/collaboration/pkg/server/debug/server.go diff --git a/services/collaboration/pkg/command/server.go b/services/collaboration/pkg/command/server.go index b118d64d7..0ccc2bfd0 100644 --- a/services/collaboration/pkg/command/server.go +++ b/services/collaboration/pkg/command/server.go @@ -13,6 +13,7 @@ import ( "github.com/owncloud/ocis/v2/services/collaboration/pkg/config/parser" "github.com/owncloud/ocis/v2/services/collaboration/pkg/connector" "github.com/owncloud/ocis/v2/services/collaboration/pkg/helpers" + "github.com/owncloud/ocis/v2/services/collaboration/pkg/server/debug" "github.com/owncloud/ocis/v2/services/collaboration/pkg/server/grpc" "github.com/owncloud/ocis/v2/services/collaboration/pkg/server/http" "github.com/urfave/cli/v2" @@ -98,31 +99,31 @@ func Server(cfg *config.Config) *cli.Command { cancel() }) - /* - server, err := debug.Server( - debug.Logger(logger), - debug.Context(ctx), - debug.Config(cfg), - ) - if err != nil { - logger.Info().Err(err).Str("transport", "debug").Msg("Failed to initialize server") - return err - } + // start debug server + debugServer, err := debug.Server( + debug.Logger(logger), + 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(debugServer.ListenAndServe, func(_ error) { + _ = debugServer.Shutdown(ctx) + cancel() + }) - gr.Add(server.ListenAndServe, func(_ error) { - _ = server.Shutdown(ctx) - cancel() - }) - */ // start HTTP server - server, err := http.Server( + httpServer, err := http.Server( http.Adapter(connector.NewHttpAdapter(gwc, cfg)), http.Logger(logger), http.Config(cfg), http.Context(ctx), http.TracerProvider(traceProvider), ) - gr.Add(server.Run, func(_ error) { + gr.Add(httpServer.Run, func(_ error) { cancel() }) diff --git a/services/collaboration/pkg/config/config.go b/services/collaboration/pkg/config/config.go index 5c3ca9dc5..7654a5e34 100644 --- a/services/collaboration/pkg/config/config.go +++ b/services/collaboration/pkg/config/config.go @@ -23,7 +23,7 @@ type Config struct { Tracing *Tracing `yaml:"tracing"` Log *Log `yaml:"log"` - //Debug Debug `yaml:"debug"` + Debug Debug `yaml:"debug"` Context context.Context `yaml:"-"` } diff --git a/services/collaboration/pkg/config/defaults/defaultconfig.go b/services/collaboration/pkg/config/defaults/defaultconfig.go index 872b05e5a..ef4edc91e 100644 --- a/services/collaboration/pkg/config/defaults/defaultconfig.go +++ b/services/collaboration/pkg/config/defaults/defaultconfig.go @@ -37,6 +37,12 @@ func DefaultConfig() *config.Config { Namespace: "com.owncloud.collaboration", Scheme: "https", }, + Debug: config.Debug{ + Addr: "127.0.0.1:9304", + Token: "", + Pprof: false, + Zpages: false, + }, WopiApp: config.WopiApp{ Addr: "https://127.0.0.1:8080", Insecure: false, diff --git a/services/collaboration/pkg/server/debug/option.go b/services/collaboration/pkg/server/debug/option.go new file mode 100644 index 000000000..283d90d4a --- /dev/null +++ b/services/collaboration/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/collaboration/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/collaboration/pkg/server/debug/server.go b/services/collaboration/pkg/server/debug/server.go new file mode 100644 index 000000000..abcbe9e8a --- /dev/null +++ b/services/collaboration/pkg/server/debug/server.go @@ -0,0 +1,63 @@ +package debug + +import ( + "io" + "net/http" + + "github.com/owncloud/ocis/v2/ocis-pkg/service/debug" + "github.com/owncloud/ocis/v2/ocis-pkg/version" + "github.com/owncloud/ocis/v2/services/collaboration/pkg/config" +) + +// Server initializes the debug service and server. +func Server(opts ...Option) (*http.Server, error) { + options := newOptions(opts...) + + 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(health(options.Config)), + debug.Ready(ready(options.Config)), + //debug.CorsAllowedOrigins(options.Config.HTTP.CORS.AllowedOrigins), + //debug.CorsAllowedMethods(options.Config.HTTP.CORS.AllowedMethods), + //debug.CorsAllowedHeaders(options.Config.HTTP.CORS.AllowedHeaders), + //debug.CorsAllowCredentials(options.Config.HTTP.CORS.AllowCredentials), + ), nil +} + +// health implements the health check. +func health(cfg *config.Config) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(http.StatusOK) + + // TODO: check if services are up and running + + _, err := io.WriteString(w, http.StatusText(http.StatusOK)) + // io.WriteString should not fail but if it does we want to know. + if err != nil { + panic(err) + } + } +} + +// ready implements the ready check. +func ready(cfg *config.Config) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(http.StatusOK) + + // TODO: check if services are up and running + + _, err := io.WriteString(w, http.StatusText(http.StatusOK)) + // io.WriteString should not fail but if it does we want to know. + if err != nil { + panic(err) + } + } +}