Skip to content

Commit

Permalink
feat: expose server implementation on transport
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Aug 6, 2024
1 parent d6fca5e commit 71207f3
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 0 deletions.
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
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 @@ -20,6 +20,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 @@ -71,6 +72,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 @@ type Transporter interface {

// Transport is an HTTP transport.
type Transport struct {
server interface{}
endpoint string
operation string
reqHeader headerCarrier
Expand All @@ -37,6 +38,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 Expand Up @@ -71,6 +77,15 @@ func SetOperation(ctx context.Context, op string) {
}
}

// 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

0 comments on commit 71207f3

Please sign in to comment.