Skip to content

Commit

Permalink
fix http middleware (#710)
Browse files Browse the repository at this point in the history
* fix http middleware
  • Loading branch information
tonybase authored Feb 18, 2021
1 parent 4c67fa1 commit c7827ff
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 67 deletions.
2 changes: 1 addition & 1 deletion cmd/protoc-gen-go-http/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2
go 1.15

require (
github.com/go-kratos/kratos/v2 v2.0.0-20210217083752-d86d233d93ce
github.com/go-kratos/kratos/v2 v2.0.0-20210218084408-cf599c68a65f
github.com/golang/protobuf v1.4.3
google.golang.org/genproto v0.0.0-20210202153253-cf70463f6119
google.golang.org/grpc v1.35.0
Expand Down
2 changes: 2 additions & 0 deletions cmd/protoc-gen-go-http/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-kratos/kratos/v2 v2.0.0-20210217083752-d86d233d93ce h1:LfOsLN9s8tAxR8xIZGWQvEVWxHfipTnBSE0dvG4h3k8=
github.com/go-kratos/kratos/v2 v2.0.0-20210217083752-d86d233d93ce/go.mod h1:oLvFyDBJkkWN8TPqb+NmpvRrSy9uM/K+XQubVRc11a8=
github.com/go-kratos/kratos/v2 v2.0.0-20210218084408-cf599c68a65f h1:ocHPvNS53zBT6NiGmgOWV2SuAmcNyHbJZk1t2cqEjIU=
github.com/go-kratos/kratos/v2 v2.0.0-20210218084408-cf599c68a65f/go.mod h1:oLvFyDBJkkWN8TPqb+NmpvRrSy9uM/K+XQubVRc11a8=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
9 changes: 4 additions & 5 deletions cmd/protoc-gen-go-http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
)

const (
contextPackage = protogen.GoImportPath("context")
httpPackage = protogen.GoImportPath("net/http")
transportPackage = protogen.GoImportPath("github.com/go-kratos/kratos/v2/transport/http")
middlewarePackage = protogen.GoImportPath("github.com/go-kratos/kratos/v2/middleware")
contextPackage = protogen.GoImportPath("context")
httpPackage = protogen.GoImportPath("net/http")
transportPackage = protogen.GoImportPath("github.com/go-kratos/kratos/v2/transport/http")
)

var methodSets = make(map[string]int)
Expand All @@ -41,7 +40,7 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
}
g.P("// This is a compile-time assertion to ensure that this generated file")
g.P("// is compatible with the kratos package it is being compiled against.")
g.P("// ", contextPackage.Ident(""), "/", httpPackage.Ident(""), "/", middlewarePackage.Ident(""))
g.P("// ", contextPackage.Ident(""), "/", httpPackage.Ident(""))
g.P("const _ = ", transportPackage.Ident("SupportPackageIsVersion1"))
g.P()

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 17 additions & 42 deletions cmd/protoc-gen-go-http/internal/testproto/echo_service_http.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions cmd/protoc-gen-go-http/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Register{{.ServiceType}}HTTPServer(s http1.ServiceRegistrar, srv {{.Service
s.RegisterService(&_HTTP_{{.ServiceType}}_serviceDesc, srv)
}
{{range .Methods}}
func _HTTP_{{$.ServiceType}}_{{.Name}}_{{.Num}}(srv interface{}, ctx context.Context, req *http.Request, dec func(interface{}) error, m middleware.Middleware) (interface{}, error) {
func _HTTP_{{$.ServiceType}}_{{.Name}}_{{.Num}}(srv interface{}, ctx context.Context, req *http.Request, dec func(interface{}) error) (interface{}, error) {
var in {{.Request}}
{{if ne (len .Vars) 0}}
if err := http1.BindVars(req, &in); err != nil {
Expand All @@ -36,10 +36,7 @@ func _HTTP_{{$.ServiceType}}_{{.Name}}_{{.Num}}(srv interface{}, ctx context.Con
return nil, err
}
{{end}}
h := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.({{$.ServiceType}}Server).{{.Name}}(ctx, &in)
}
out, err := m(h)(ctx, &in)
out, err := srv.({{$.ServiceType}}Server).{{.Name}}(ctx, &in)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion transport/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (t *baseTransport) RoundTrip(req *http.Request) (*http.Response, error) {
defer cancel()

h := func(ctx context.Context, in interface{}) (interface{}, error) {
return t.base.RoundTrip(req)
return t.base.RoundTrip(in.(*http.Request))
}
if t.middleware != nil {
h = t.middleware(h)
Expand Down
10 changes: 9 additions & 1 deletion transport/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,15 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
defer cancel()
ctx = transport.NewContext(ctx, transport.Transport{Kind: "HTTP"})
ctx = NewServerContext(ctx, ServerInfo{Request: req, Response: res})
s.router.ServeHTTP(res, req.WithContext(ctx))

h := func(ctx context.Context, req interface{}) (interface{}, error) {
s.router.ServeHTTP(res, req.(*http.Request))
return res, nil
}
if s.middleware != nil {
h = s.middleware(h)
}
h(ctx, req.WithContext(ctx))
}

// Endpoint return a real address to registry endpoint.
Expand Down
6 changes: 2 additions & 4 deletions transport/http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package http
import (
"context"
"net/http"

"github.com/go-kratos/kratos/v2/middleware"
)

// SupportPackageIsVersion1 These constants should not be referenced from any other code.
const SupportPackageIsVersion1 = true

type methodHandler func(srv interface{}, ctx context.Context, req *http.Request, dec func(interface{}) error, m middleware.Middleware) (out interface{}, err error)
type methodHandler func(srv interface{}, ctx context.Context, req *http.Request, dec func(interface{}) error) (out interface{}, err error)

// MethodDesc represents a Proto service's method specification.
type MethodDesc struct {
Expand Down Expand Up @@ -38,7 +36,7 @@ func (s *Server) RegisterService(desc *ServiceDesc, impl interface{}) {
s.router.HandleFunc(m.Path, func(res http.ResponseWriter, req *http.Request) {
out, err := h(impl, req.Context(), req, func(v interface{}) error {
return s.requestDecoder(req, v)
}, s.middleware)
})
if err != nil {
s.errorEncoder(res, req, err)
return
Expand Down
8 changes: 2 additions & 6 deletions transport/http/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/go-kratos/kratos/v2/internal/host"
"github.com/go-kratos/kratos/v2/middleware"
)

type testRequest struct {
Expand All @@ -26,15 +25,12 @@ func (s *testService) SayHello(ctx context.Context, req *testRequest) (*testRepl
}

func TestService(t *testing.T) {
h := func(srv interface{}, ctx context.Context, req *http.Request, dec func(interface{}) error, m middleware.Middleware) (interface{}, error) {
h := func(srv interface{}, ctx context.Context, req *http.Request, dec func(interface{}) error) (interface{}, error) {
var in testRequest
if err := dec(&in); err != nil {
return nil, err
}
h := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(*testService).SayHello(ctx, &in)
}
out, err := m(h)(ctx, &in)
out, err := srv.(*testService).SayHello(ctx, &in)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c7827ff

Please sign in to comment.