Merge pull request #87 from unistack-org/combine_handler

combine native and micro http handlers
This commit is contained in:
Василий Толстов 2022-01-23 02:00:33 +03:00 committed by GitHub
commit 68b6f6904b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 4 deletions

1
go.sum
View File

@ -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/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 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 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/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 h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=

View File

@ -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) h.errorHandler(ctx, nil, w, r, fmt.Errorf("not matching route found"), http.StatusNotFound)
return return
} }

11
http.go
View File

@ -22,6 +22,8 @@ import (
"golang.org/x/net/netutil" "golang.org/x/net/netutil"
) )
var _ server.Server = &httpServer{}
type httpServer struct { type httpServer struct {
hd server.Handler hd server.Handler
rsvc *register.Service 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 handler = h
} else if handler == nil && h.hd != nil { case handler == nil && h.hd != nil:
if hdlr, ok := h.hd.Handler().(http.Handler); ok { if hdlr, ok := h.hd.Handler().(http.Handler); ok {
handler = hdlr handler = hdlr
} }
case len(h.handlers) > 0 && h.hd != nil:
handler = h
} }
if handler == nil { if handler == nil {
@ -585,7 +590,7 @@ func (h *httpServer) Name() string {
return h.opts.Name return h.opts.Name
} }
func NewServer(opts ...server.Option) server.Server { func NewServer(opts ...server.Option) *httpServer {
options := server.NewOptions(opts...) options := server.NewOptions(opts...)
return &httpServer{ return &httpServer{
opts: options, opts: options,