api/router/registry: extract path based parameters from url to req (#1530)

* api/router/registry: extract path based parameters from url to req
* api/handler/rpc: fix empty body request parsing
* bundle grpc-gateway util funcs

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-04-15 17:50:51 +03:00
parent f911bf794f
commit 6db825838a

View File

@ -8,20 +8,19 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/httprule"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/micro/go-micro/v2/api" "github.com/micro/go-micro/v2/api"
"github.com/micro/go-micro/v2/api/router" "github.com/micro/go-micro/v2/api/router"
"github.com/micro/go-micro/v2/api/router/util"
"github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/metadata" "github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/registry"
util "github.com/micro/go-micro/v2/util/registry" rutil "github.com/micro/go-micro/v2/util/registry"
) )
type endpoint struct { type endpoint struct {
apiep *api.Endpoint apiep *api.Endpoint
hostregs []*regexp.Regexp hostregs []*regexp.Regexp
pathregs []runtime.Pattern pathregs []util.Pattern
} }
// router is the default router // router is the default router
@ -93,7 +92,7 @@ func (r *staticRouter) Register(ep *api.Endpoint) error {
return err return err
} }
var pathregs []runtime.Pattern var pathregs []util.Pattern
var hostregs []*regexp.Regexp var hostregs []*regexp.Regexp
for _, h := range ep.Host { for _, h := range ep.Host {
@ -108,12 +107,12 @@ func (r *staticRouter) Register(ep *api.Endpoint) error {
} }
for _, p := range ep.Path { for _, p := range ep.Path {
rule, err := httprule.Parse(p) rule, err := util.Parse(p)
if err != nil { if err != nil {
return err return err
} }
tpl := rule.Compile() tpl := rule.Compile()
pathreg, err := runtime.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, "") pathreg, err := util.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, "")
if err != nil { if err != nil {
return err return err
} }
@ -164,7 +163,7 @@ func (r *staticRouter) Endpoint(req *http.Request) (*api.Service, error) {
// hack for stream endpoint // hack for stream endpoint
if ep.apiep.Stream { if ep.apiep.Stream {
svcs := util.Copy(services) svcs := rutil.Copy(services)
for _, svc := range svcs { for _, svc := range svcs {
if len(svc.Endpoints) == 0 { if len(svc.Endpoints) == 0 {
e := &registry.Endpoint{} e := &registry.Endpoint{}