From 31e996fc2e20fb3550aa48cb6ecb2599913e95c6 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 23 Jan 2022 02:00:15 +0300 Subject: [PATCH] combine native and micro http handlers Signed-off-by: Vasiliy Tolstov --- go.sum | 1 + handler.go | 7 ++++++- http.go | 11 ++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/go.sum b/go.sum index e2c5c0a..21d0591 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,7 @@ github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzw github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= diff --git a/handler.go b/handler.go index 124bb06..d697a92 100644 --- a/handler.go +++ b/handler.go @@ -124,7 +124,12 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - if !match { + if !match && h.hd != nil { + if hdlr, ok := h.hd.Handler().(http.Handler); ok { + hdlr.ServeHTTP(w, r) + return + } + } else if !match { h.errorHandler(ctx, nil, w, r, fmt.Errorf("not matching route found"), http.StatusNotFound) return } diff --git a/http.go b/http.go index 1eb4047..8f34da6 100644 --- a/http.go +++ b/http.go @@ -22,6 +22,8 @@ import ( "golang.org/x/net/netutil" ) +var _ server.Server = &httpServer{} + type httpServer struct { hd server.Handler rsvc *register.Service @@ -448,12 +450,15 @@ func (h *httpServer) Start() error { } } - if handler == nil && h.hd == nil { + switch { + case handler == nil && h.hd == nil: handler = h - } else if handler == nil && h.hd != nil { + case handler == nil && h.hd != nil: if hdlr, ok := h.hd.Handler().(http.Handler); ok { handler = hdlr } + case len(h.handlers) > 0 && h.hd != nil: + handler = h } if handler == nil { @@ -585,7 +590,7 @@ func (h *httpServer) Name() string { return h.opts.Name } -func NewServer(opts ...server.Option) server.Server { +func NewServer(opts ...server.Option) *httpServer { options := server.NewOptions(opts...) return &httpServer{ opts: options, -- 2.45.2