From c0d9c342005aba19731ee4df98f71c6f21cbc120 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 7 Feb 2021 19:03:38 +0300 Subject: [PATCH] allow to have custom http.Server struct Signed-off-by: Vasiliy Tolstov --- http.go | 12 +++++++++++- options.go | 6 ++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/http.go b/http.go index 61bf18b..6d6e6b3 100644 --- a/http.go +++ b/http.go @@ -376,6 +376,8 @@ func (h *httpServer) Start() error { } fn := handler + var srvFunc func(net.Listener) error + if h.opts.Context != nil { if mwf, ok := h.opts.Context.Value(middlewareKey{}).([]func(http.Handler) http.Handler); ok && len(mwf) > 0 { // wrap the handler func @@ -383,9 +385,17 @@ func (h *httpServer) Start() error { fn = mwf[i-1](fn) } } + if hs, ok := h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil { + hs.Handler = fn + srvFunc = hs.Serve + } } - go http.Serve(ts, fn) + if srvFunc != nil { + go srvFunc(ts) + } else { + go http.Serve(ts, fn) + } go func() { t := new(time.Ticker) diff --git a/options.go b/options.go index e77dfde..eb645fc 100644 --- a/options.go +++ b/options.go @@ -33,3 +33,9 @@ type middlewareKey struct{} func Middleware(mw ...func(http.Handler) http.Handler) server.Option { return server.SetOption(middlewareKey{}, mw) } + +type serverKey struct{} + +func Server(hs *http.Server) server.Option { + return server.SetOption(serverKey{}, hs) +}