From ad2a53cc0e5a197effcbaf0537e3787cb3eddfb6 Mon Sep 17 00:00:00 2001 From: Matthias Loibl Date: Mon, 8 Feb 2021 15:19:47 +0100 Subject: [PATCH] pkg/server/http: Allow passing http.ServeMux with a server option (#3769) Signed-off-by: Matthias Loibl --- pkg/server/http/http.go | 4 ++++ pkg/server/http/option.go | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/server/http/http.go b/pkg/server/http/http.go index a25fc63a40..3cc6afa94a 100644 --- a/pkg/server/http/http.go +++ b/pkg/server/http/http.go @@ -39,6 +39,10 @@ func New(logger log.Logger, reg *prometheus.Registry, comp component.Component, } mux := http.NewServeMux() + if options.mux != nil { + mux = options.mux + } + registerMetrics(mux, reg) registerProbes(mux, prober, logger) registerProfiler(mux) diff --git a/pkg/server/http/option.go b/pkg/server/http/option.go index 398cabd93d..ba4754acd3 100644 --- a/pkg/server/http/option.go +++ b/pkg/server/http/option.go @@ -4,12 +4,14 @@ package http import ( + "net/http" "time" ) type options struct { gracePeriod time.Duration listen string + mux *http.ServeMux } // Option overrides behavior of Server. @@ -38,3 +40,10 @@ func WithListen(s string) Option { o.listen = s }) } + +// WithMux overrides the the server's default mux. +func WithMux(mux *http.ServeMux) Option { + return optionFunc(func(o *options) { + o.mux = mux + }) +}