Compare commits

...

4 Commits

Author SHA1 Message Date
c0d9c34200 allow to have custom http.Server struct
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-07 19:03:38 +03:00
dc1e05bb18 allow to use http middlewares
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-07 13:27:28 +03:00
1e4d56d059 fix body parsing
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-06 18:53:25 +03:00
6565336c1a utilize native merging for map and struct without json
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-06 18:16:29 +03:00
5 changed files with 51 additions and 8 deletions

2
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/unistack-org/micro-server-http/v3
go 1.13
require (
github.com/unistack-org/micro/v3 v3.2.7
github.com/unistack-org/micro/v3 v3.2.8
golang.org/x/net v0.0.0-20201224014010-6772e930b67b
)

4
go.sum
View File

@@ -57,8 +57,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/unistack-org/micro/v3 v3.2.7 h1:+vLVmoQeE0z0cmIAKXSQXbxC4pxXpmkzckh9B9shogo=
github.com/unistack-org/micro/v3 v3.2.7/go.mod h1:J8XxJj4Pqa3Ee0a4biRRtut7UwTlfBq8QRe+s4PKGS0=
github.com/unistack-org/micro/v3 v3.2.8 h1:M1qgOz+qgSf/coFeIqDKa7Xc+b3s7du1Yin+i10o3aw=
github.com/unistack-org/micro/v3 v3.2.8/go.mod h1:J8XxJj4Pqa3Ee0a4biRRtut7UwTlfBq8QRe+s4PKGS0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=

View File

@@ -3,6 +3,7 @@ package http
import (
"context"
"fmt"
"io"
"net/http"
"reflect"
"strings"
@@ -12,7 +13,6 @@ import (
"github.com/unistack-org/micro/v3/metadata"
"github.com/unistack-org/micro/v3/register"
"github.com/unistack-org/micro/v3/server"
"github.com/unistack-org/micro/v3/util/qson"
rflutil "github.com/unistack-org/micro/v3/util/reflect"
rutil "github.com/unistack-org/micro/v3/util/router"
)
@@ -68,6 +68,8 @@ func (h *httpHandler) Options() server.HandlerOptions {
func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
defer r.Body.Close()
path := r.URL.Path
if !strings.HasPrefix(path, "/") {
h.errorHandler(ctx, h, w, r, fmt.Errorf("path must contains /"), http.StatusBadRequest)
@@ -126,8 +128,7 @@ func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// get fields from url values
if len(r.URL.RawQuery) > 0 {
umd := make(map[string]interface{})
err = qson.Unmarshal(&umd, r.URL.RawQuery)
umd, err := rflutil.URLMap(r.URL.RawQuery)
if err != nil {
h.errorHandler(ctx, h, w, r, err, http.StatusBadRequest)
}
@@ -158,6 +159,11 @@ func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//function := hldr.rcvr
var returnValues []reflect.Value
if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF {
h.errorHandler(ctx, h, w, r, err, http.StatusInternalServerError)
}
matches = rflutil.FlattenMap(matches)
if err = rflutil.MergeMap(argv.Interface(), matches); err != nil {
h.errorHandler(ctx, h, w, r, err, http.StatusBadRequest)
return

22
http.go
View File

@@ -375,7 +375,27 @@ func (h *httpServer) Start() error {
}
}
go http.Serve(ts, handler)
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
for i := len(mwf); i > 0; i-- {
fn = mwf[i-1](fn)
}
}
if hs, ok := h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil {
hs.Handler = fn
srvFunc = hs.Serve
}
}
if srvFunc != nil {
go srvFunc(ts)
} else {
go http.Serve(ts, fn)
}
go func() {
t := new(time.Ticker)

View File

@@ -1,6 +1,11 @@
package http
import "context"
import (
"context"
"net/http"
"github.com/unistack-org/micro/v3/server"
)
type rspCodeKey struct{}
type rspCodeVal struct {
@@ -22,3 +27,15 @@ func GetRspCode(ctx context.Context) int {
}
return code
}
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)
}