mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-02-26 23:48:55 -06:00
debugServer: Set a base context for the http server
to allow propagating context cancellation
This commit is contained in:
committed by
Ralf Haferkamp
parent
54664024c8
commit
2a53dd89f4
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user