Skip to content

Commit

Permalink
code cleanup & refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <[email protected]>
  • Loading branch information
dragonchaser committed Oct 10, 2024
1 parent f64ccd0 commit 221815c
Show file tree
Hide file tree
Showing 52 changed files with 871 additions and 204 deletions.
1 change: 1 addition & 0 deletions services/app-registry/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func Server(cfg *config.Config) *cli.Command {
}

gr.Add(debugServer.ListenAndServe, func(_ error) {
_ = debugServer.Shutdown(ctx)
cancel()
})

Expand Down
29 changes: 10 additions & 19 deletions services/audit/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
})
}
Expand Down
50 changes: 50 additions & 0 deletions services/audit/pkg/server/debug/option.go
Original file line number Diff line number Diff line change
@@ -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
}
}
31 changes: 31 additions & 0 deletions services/audit/pkg/server/debug/server.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions services/auth-app/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func Server(cfg *config.Config) *cli.Command {
}

gr.Add(debugServer.ListenAndServe, func(_ error) {
_ = debugServer.Shutdown(ctx)
cancel()
})

Expand Down
1 change: 1 addition & 0 deletions services/auth-basic/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func Server(cfg *config.Config) *cli.Command {
}

gr.Add(debugServer.ListenAndServe, func(_ error) {
_ = debugServer.Shutdown(ctx)
cancel()
})

Expand Down
1 change: 1 addition & 0 deletions services/auth-bearer/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func Server(cfg *config.Config) *cli.Command {
}

gr.Add(debugServer.ListenAndServe, func(_ error) {
_ = debugServer.Shutdown(ctx)
cancel()
})

Expand Down
1 change: 1 addition & 0 deletions services/auth-machine/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func Server(cfg *config.Config) *cli.Command {
}

gr.Add(debugServer.ListenAndServe, func(_ error) {
_ = debugServer.Shutdown(ctx)
cancel()
})

Expand Down
1 change: 1 addition & 0 deletions services/auth-service/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func Server(cfg *config.Config) *cli.Command {
}

gr.Add(debugServer.ListenAndServe, func(_ error) {
_ = debugServer.Shutdown(ctx)
cancel()
})

Expand Down
28 changes: 10 additions & 18 deletions services/clientlog/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
})
}
Expand Down
50 changes: 50 additions & 0 deletions services/clientlog/pkg/server/debug/option.go
Original file line number Diff line number Diff line change
@@ -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
}
}
31 changes: 31 additions & 0 deletions services/clientlog/pkg/server/debug/server.go
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 10 additions & 18 deletions services/eventhistory/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ 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"
"github.com/owncloud/ocis/v2/services/eventhistory/pkg/config"
"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"
)

Expand Down Expand Up @@ -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()
})
}
Expand Down
Loading

0 comments on commit 221815c

Please sign in to comment.