Compare commits

...

8 Commits

Author SHA1 Message Date
e2efc934b3 Merge branch 'master' into v3 2022-01-24 20:31:05 +03:00
2d5fb587a0 ignore net.ErrClosed
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2022-01-24 20:29:52 +03:00
6a30a4f408 Merge pull request #90 from unistack-org/master
merge master
2022-01-23 02:41:15 +03:00
f80e6c1a66 Merge pull request #89 from unistack-org/reorder
reorder path checking, priority in internal routes
2022-01-23 02:40:40 +03:00
2aa8768aed reorder path checking, priority in internal routes
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2022-01-23 02:40:24 +03:00
0ab763b505 Merge pull request #88 from unistack-org/master
merge master
2022-01-23 02:01:01 +03:00
68b6f6904b Merge pull request #87 from unistack-org/combine_handler
combine native and micro http handlers
2022-01-23 02:00:33 +03:00
31e996fc2e combine native and micro http handlers
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2022-01-23 02:00:15 +03:00
3 changed files with 18 additions and 6 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/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=

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)
return
}

16
http.go
View File

@@ -4,6 +4,7 @@ package http // import "go.unistack.org/micro-server-http/v3"
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
@@ -22,6 +23,8 @@ import (
"golang.org/x/net/netutil"
)
var _ server.Server = &httpServer{}
type httpServer struct {
hd server.Handler
rsvc *register.Service
@@ -448,9 +451,12 @@ 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 len(h.handlers) > 0 && h.hd != nil:
handler = h
case handler == nil && h.hd != nil:
if hdlr, ok := h.hd.Handler().(http.Handler); ok {
handler = hdlr
}
@@ -491,13 +497,13 @@ func (h *httpServer) Start() error {
if srvFunc != nil {
go func() {
if cerr := srvFunc(ts); cerr != nil && !strings.Contains(cerr.Error(), "use of closed network connection") {
if cerr := srvFunc(ts); cerr != nil && !errors.Is(cerr, net.ErrClosed) {
h.opts.Logger.Error(h.opts.Context, cerr)
}
}()
} else {
go func() {
if cerr := http.Serve(ts, fn); cerr != nil && !strings.Contains(cerr.Error(), "use of closed network connection") {
if cerr := http.Serve(ts, fn); cerr != nil && !errors.Is(cerr, net.ErrClosed) {
h.opts.Logger.Error(h.opts.Context, cerr)
}
}()
@@ -585,7 +591,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,