Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transport): expose server implementation on transport #3279

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/metadata/metadata_http.pb.go

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

1 change: 1 addition & 0 deletions cmd/protoc-gen-go-http/httpTemplate.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv {{$svrType}}HTTPServer) fu
return err
}
{{- end}}
http.SetServer(ctx, srv)
http.SetOperation(ctx,Operation{{$svrType}}{{.OriginalName}})
h := ctx.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.{{.Name}}(ctx, req.(*{{.Request}}))
Expand Down
9 changes: 1 addition & 8 deletions encoding/form/proto_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ func EncodeValues(msg interface{}) (url.Values, error) {
if v, ok := msg.(proto.Message); ok {
u := make(url.Values)
err := encodeByField(u, "", v.ProtoReflect())
if err != nil {
return nil, err
}
return u, nil
return u, err
}
return encoder.Encode(msg)
}
Expand Down Expand Up @@ -56,7 +53,6 @@ func encodeByField(u url.Values, path string, m protoreflect.Message) (finalErr
list, err := encodeRepeatedField(fd, v.List())
if err != nil {
finalErr = err
return false
}
for _, item := range list {
u.Add(newPath, item)
Expand All @@ -67,7 +63,6 @@ func encodeByField(u url.Values, path string, m protoreflect.Message) (finalErr
m, err := encodeMapField(fd, v.Map())
if err != nil {
finalErr = err
return false
}
for k, value := range m {
u.Set(fmt.Sprintf("%s[%s]", newPath, k), value)
Expand All @@ -81,13 +76,11 @@ func encodeByField(u url.Values, path string, m protoreflect.Message) (finalErr
}
if err = encodeByField(u, newPath, v.Message()); err != nil {
finalErr = err
return false
}
default:
value, err := EncodeField(fd, v)
if err != nil {
finalErr = err
return false
}
u.Set(newPath, value)
}
Expand Down
1 change: 1 addition & 0 deletions internal/testdata/helloworld/helloworld_http.pb.go

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

4 changes: 4 additions & 0 deletions middleware/auth/jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (tr *Transport) Endpoint() string {
return tr.endpoint
}

func (tr *Transport) Server() interface{} {
return nil
}

func (tr *Transport) Operation() string {
return tr.operation
}
Expand Down
4 changes: 4 additions & 0 deletions middleware/circuitbreaker/circuitbreaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (tr *transportMock) Endpoint() string {
return tr.endpoint
}

func (tr *transportMock) Server() interface{} {
return nil
}

func (tr *transportMock) Operation() string {
return tr.operation
}
Expand Down
4 changes: 4 additions & 0 deletions middleware/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (tr *Transport) Endpoint() string {
return tr.endpoint
}

func (tr *Transport) Server() interface{} {
return nil
}

func (tr *Transport) Operation() string {
return tr.operation
}
Expand Down
1 change: 1 addition & 0 deletions middleware/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type testTransport struct{ header headerCarrier }

func (tr *testTransport) Kind() transport.Kind { return transport.KindHTTP }
func (tr *testTransport) Endpoint() string { return "" }
func (tr *testTransport) Server() interface{} { return nil }
func (tr *testTransport) Operation() string { return "" }
func (tr *testTransport) RequestHeader() transport.Header { return tr.header }
func (tr *testTransport) ReplyHeader() transport.Header { return tr.header }
Expand Down
4 changes: 4 additions & 0 deletions middleware/selector/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (tr *Transport) Endpoint() string {
return tr.endpoint
}

func (tr *Transport) Server() interface{} {
return nil
}

func (tr *Transport) Operation() string {
return tr.operation
}
Expand Down
1 change: 1 addition & 0 deletions middleware/tracing/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type mockTransport struct {

func (tr *mockTransport) Kind() transport.Kind { return tr.kind }
func (tr *mockTransport) Endpoint() string { return tr.endpoint }
func (tr *mockTransport) Server() interface{} { return nil }
func (tr *mockTransport) Operation() string { return tr.operation }
func (tr *mockTransport) RequestHeader() transport.Header { return tr.header }
func (tr *mockTransport) ReplyHeader() transport.Header { return tr.header }
Expand Down
2 changes: 2 additions & 0 deletions transport/grpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (s *Server) unaryServerInterceptor() grpc.UnaryServerInterceptor {
replyHeader := grpcmd.MD{}
tr := &Transport{
operation: info.FullMethod,
server: info.Server,
reqHeader: headerCarrier(md),
replyHeader: headerCarrier(replyHeader),
}
Expand Down Expand Up @@ -75,6 +76,7 @@ func (s *Server) streamServerInterceptor() grpc.StreamServerInterceptor {
replyHeader := grpcmd.MD{}
ctx = transport.NewServerContext(ctx, &Transport{
endpoint: s.endpoint.String(),
server: srv,
operation: info.FullMethod,
reqHeader: headerCarrier(md),
replyHeader: headerCarrier(replyHeader),
Expand Down
6 changes: 6 additions & 0 deletions transport/grpc/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var _ transport.Transporter = (*Transport)(nil)
// Transport is a gRPC transport.
type Transport struct {
endpoint string
server interface{}
operation string
reqHeader headerCarrier
replyHeader headerCarrier
Expand All @@ -28,6 +29,11 @@ func (tr *Transport) Endpoint() string {
return tr.endpoint
}

// Server returns the transport server.
func (tr *Transport) Server() interface{} {
return tr.server
}

// Operation returns the transport operation.
func (tr *Transport) Operation() string {
return tr.operation
Expand Down
8 changes: 8 additions & 0 deletions transport/grpc/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ func TestTransport_Endpoint(t *testing.T) {
}
}

func TestTransport_Server(t *testing.T) {
v := struct{}{}
o := &Transport{server: v}
if !reflect.DeepEqual(v, o.Server()) {
t.Errorf("expect %v, got %v", v, o.Server())
}
}

func TestTransport_Operation(t *testing.T) {
v := "hello"
o := &Transport{operation: v}
Expand Down
15 changes: 15 additions & 0 deletions transport/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

// Transport is an HTTP transport.
type Transport struct {
server interface{}
endpoint string
operation string
reqHeader headerCarrier
Expand All @@ -37,6 +38,11 @@
return tr.endpoint
}

// Server returns the transport server.
func (tr *Transport) Server() interface{} {
return tr.server

Check warning on line 43 in transport/http/transport.go

View check run for this annotation

Codecov / codecov/patch

transport/http/transport.go#L42-L43

Added lines #L42 - L43 were not covered by tests
}

// Operation returns the transport operation.
func (tr *Transport) Operation() string {
return tr.operation
Expand Down Expand Up @@ -71,6 +77,15 @@
}
}

// SetServer sets the transport server.
func SetServer(ctx context.Context, srv interface{}) {
if tr, ok := transport.FromServerContext(ctx); ok {
if tr, ok := tr.(*Transport); ok {
tr.server = srv
}
}
}

// SetCookie adds a Set-Cookie header to the provided [ResponseWriter]'s headers.
// The provided cookie must have a valid Name. Invalid cookies may be
// silently dropped.
Expand Down
10 changes: 10 additions & 0 deletions transport/http/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ func TestHeaderCarrier_Keys(t *testing.T) {
}
}

func TestSetServer(t *testing.T) {
tr := &Transport{}
ctx := transport.NewServerContext(context.Background(), tr)
srv := struct{}{}
SetServer(ctx, srv)
if !reflect.DeepEqual(tr.server, srv) {
t.Errorf("expect %v, got %v", srv, tr.server)
}
}

func TestSetOperation(t *testing.T) {
tr := &Transport{}
ctx := transport.NewServerContext(context.Background(), tr)
Expand Down
2 changes: 2 additions & 0 deletions transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Transporter interface {
// Server Transport: grpc://127.0.0.1:9000
// Client Transport: discovery:///provider-demo
Endpoint() string
// Server return server implementation
Server() interface{}
// Operation Service full method selector generated by protobuf
// example: /helloworld.Greeter/SayHello
Operation() string
Expand Down
5 changes: 5 additions & 0 deletions transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func (tr *mockTransport) Endpoint() string {
return tr.endpoint
}

// Server returns the transport server.
func (tr *mockTransport) Server() interface{} {
return nil
}

// Operation returns the transport operation.
func (tr *mockTransport) Operation() string {
return tr.operation
Expand Down
Loading