Skip to content

Commit

Permalink
[WIP] add debug server to activitylog & check web availability
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <[email protected]>
  • Loading branch information
dragonchaser committed Oct 7, 2024
1 parent 7ed5b9f commit 746f19f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
50 changes: 50 additions & 0 deletions services/activitylog/pkg/server/debug/options.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/idm/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/activitylog/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
}
18 changes: 17 additions & 1 deletion services/web/pkg/server/debug/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package debug

import (
"context"
"fmt"
"net"
"net/http"

"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
Expand All @@ -14,7 +17,20 @@ func Server(opts ...Option) (*http.Server, error) {

checkHandler := handlers.NewCheckHandler(
handlers.NewCheckHandlerConfiguration().
WithLogger(options.Logger),
WithLogger(options.Logger).WithCheck("web reachability", func(ctx context.Context) error {
conn, err := net.Dial("tcp", options.Config.HTTP.Addr)
defer func(conn net.Conn) {
err := conn.Close()
if err != nil {
return
}
}(conn)
if err != nil {
return fmt.Errorf("could not connect to web server: %v", err)
}

return nil
}),
)

return debug.NewService(
Expand Down

0 comments on commit 746f19f

Please sign in to comment.