debugServer: Set a base context for the http server

to allow propagating context cancellation
This commit is contained in:
Ralf Haferkamp
2023-07-13 14:35:39 +02:00
committed by Ralf Haferkamp
parent 54664024c8
commit 2a53dd89f4
3 changed files with 20 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package debug
import (
"context"
"net/http"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
@@ -12,6 +13,7 @@ type Option func(o *Options)
// Options defines the available options for this package.
type Options struct {
Logger log.Logger
Context context.Context
Name string
Version string
Address string
@@ -45,6 +47,13 @@ func Logger(l log.Logger) Option {
}
}
// Context provides a function to set the context option.
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
}
}
// Name provides a function to set the name option.
func Name(n string) Option {
return func(o *Options) {

View File

@@ -1,6 +1,8 @@
package debug
import (
"context"
"net"
"net/http"
"net/http/pprof"
@@ -46,8 +48,16 @@ func NewService(opts ...Option) *http.Server {
mux.Handle("/debug", h)
}
baseCtx := dopts.Context
if baseCtx == nil {
baseCtx = context.Background()
}
return &http.Server{
Addr: dopts.Address,
BaseContext: func(_ net.Listener) context.Context {
return baseCtx
},
Handler: alice.New(
chimiddleware.RealIP,
chimiddleware.RequestID,

View File

@@ -15,6 +15,7 @@ func Server(opts ...Option) (*http.Server, error) {
return debug.NewService(
debug.Logger(options.Logger),
debug.Context(options.Context),
debug.Name(options.Config.Service.Name),
debug.Version(version.GetString()),
debug.Address(options.Config.Debug.Addr),