httpgrpc

package
v0.0.0-...-0450f2b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2024 License: Apache-2.0 Imports: 21 Imported by: 47

README

What? Embedding HTTP requests and responses into a gRPC service; a service and client to translate back and forth between the two, so you can use them with your preferred mux.

Why? Get all the goodness of protobuf encoding, HTTP/2, snappy, load balancing, persistent connection and native Kubernetes load balancing with ~none of the effort.

To rebuild generated protobuf code, run:

protoc -I ./ --go_out=plugins=grpc:./ ./httpgrpc.proto

Follow the instructions here to get a working protoc: https://github.com/gogo/protobuf

Documentation

Index

Constants

View Source
const (
	MetadataMethod = "httpgrpc-method"
	MetadataURL    = "httpgrpc-url"
)

Variables

View Source
var (
	ErrInvalidLengthHttpgrpc = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowHttpgrpc   = fmt.Errorf("proto: integer overflow")
)

Functions

func AppendRequestMetadataToContext

func AppendRequestMetadataToContext(ctx context.Context, req *HTTPRequest) context.Context

AppendRequestMetadataToContext appends metadata of HTTPRequest into gRPC metadata.

func Error

func Error(code int, msg string) error

Error returns a HTTP gRPC error that is correctly forwarded over gRPC, and can eventually be converted back to a HTTP response with HTTPResponseFromError.

func ErrorFromHTTPResponse

func ErrorFromHTTPResponse(resp *HTTPResponse) error

ErrorFromHTTPResponse converts an HTTP response into a grpc error, and uses HTTP response body as an error message. Note that if HTTP response body contains non-utf8 string, then returned error cannot be marshalled by protobuf.

func ErrorFromHTTPResponseWithMessage

func ErrorFromHTTPResponseWithMessage(resp *HTTPResponse, msg string) error

ErrorFromHTTPResponseWithMessage converts an HTTP response into a grpc error, and uses supplied message for Error message.

func Errorf

func Errorf(code int, tmpl string, args ...interface{}) error

Errorf returns a HTTP gRPC error that is correctly forwarded over gRPC, and can eventually be converted back to a HTTP response with HTTPResponseFromError.

func RegisterHTTPServer

func RegisterHTTPServer(s *grpc.Server, srv HTTPServer)

func ToHTTPRequest

func ToHTTPRequest(ctx context.Context, r *HTTPRequest) (*http.Request, error)

ToHTTPRequest converts httpgrpc.HTTPRequest to http.Request.

func ToHeader

func ToHeader(hs []*Header, header http.Header)

func WriteError

func WriteError(w http.ResponseWriter, err error)

WriteError converts an httpgrpc error to an HTTP one

func WriteResponse

func WriteResponse(w http.ResponseWriter, resp *HTTPResponse) error

WriteResponse converts an httpgrpc response to an HTTP one

Types

type HTTPClient

type HTTPClient interface {
	Handle(ctx context.Context, in *HTTPRequest, opts ...grpc.CallOption) (*HTTPResponse, error)
}

HTTPClient is the client API for HTTP service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.

func NewHTTPClient

func NewHTTPClient(cc *grpc.ClientConn) HTTPClient

type HTTPRequest

type HTTPRequest struct {
	Method  string    `protobuf:"bytes,1,opt,name=method,proto3" json:"method,omitempty"`
	Url     string    `protobuf:"bytes,2,opt,name=url,proto3" json:"url,omitempty"`
	Headers []*Header `protobuf:"bytes,3,rep,name=headers,proto3" json:"headers,omitempty"`
	Body    []byte    `protobuf:"bytes,4,opt,name=body,proto3" json:"body,omitempty"`
}

func FromHTTPRequest

func FromHTTPRequest(r *http.Request) (*HTTPRequest, error)

FromHTTPRequest converts an ordinary http.Request into an httpgrpc.HTTPRequest

func (*HTTPRequest) Descriptor

func (*HTTPRequest) Descriptor() ([]byte, []int)

func (*HTTPRequest) Equal

func (this *HTTPRequest) Equal(that interface{}) bool

func (*HTTPRequest) GetBody

func (m *HTTPRequest) GetBody() []byte

func (*HTTPRequest) GetHeaders

func (m *HTTPRequest) GetHeaders() []*Header

func (*HTTPRequest) GetMethod

func (m *HTTPRequest) GetMethod() string

func (*HTTPRequest) GetUrl

func (m *HTTPRequest) GetUrl() string

func (*HTTPRequest) GoString

func (this *HTTPRequest) GoString() string

func (*HTTPRequest) Marshal

func (m *HTTPRequest) Marshal() (dAtA []byte, err error)

func (*HTTPRequest) MarshalTo

func (m *HTTPRequest) MarshalTo(dAtA []byte) (int, error)

func (*HTTPRequest) MarshalToSizedBuffer

func (m *HTTPRequest) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*HTTPRequest) ProtoMessage

func (*HTTPRequest) ProtoMessage()

func (*HTTPRequest) Reset

func (m *HTTPRequest) Reset()

func (*HTTPRequest) Size

func (m *HTTPRequest) Size() (n int)

func (*HTTPRequest) String

func (this *HTTPRequest) String() string

func (*HTTPRequest) Unmarshal

func (m *HTTPRequest) Unmarshal(dAtA []byte) error

func (*HTTPRequest) XXX_DiscardUnknown

func (m *HTTPRequest) XXX_DiscardUnknown()

func (*HTTPRequest) XXX_Marshal

func (m *HTTPRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*HTTPRequest) XXX_Merge

func (m *HTTPRequest) XXX_Merge(src proto.Message)

func (*HTTPRequest) XXX_Size

func (m *HTTPRequest) XXX_Size() int

func (*HTTPRequest) XXX_Unmarshal

func (m *HTTPRequest) XXX_Unmarshal(b []byte) error

type HTTPResponse

type HTTPResponse struct {
	Code    int32     `protobuf:"varint,1,opt,name=Code,proto3" json:"Code,omitempty"`
	Headers []*Header `protobuf:"bytes,2,rep,name=headers,proto3" json:"headers,omitempty"`
	Body    []byte    `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"`
}

func HTTPResponseFromError

func HTTPResponseFromError(err error) (*HTTPResponse, bool)

HTTPResponseFromError converts a grpc error into an HTTP response

func (*HTTPResponse) Descriptor

func (*HTTPResponse) Descriptor() ([]byte, []int)

func (*HTTPResponse) Equal

func (this *HTTPResponse) Equal(that interface{}) bool

func (*HTTPResponse) GetBody

func (m *HTTPResponse) GetBody() []byte

func (*HTTPResponse) GetCode

func (m *HTTPResponse) GetCode() int32

func (*HTTPResponse) GetHeaders

func (m *HTTPResponse) GetHeaders() []*Header

func (*HTTPResponse) GoString

func (this *HTTPResponse) GoString() string

func (*HTTPResponse) Marshal

func (m *HTTPResponse) Marshal() (dAtA []byte, err error)

func (*HTTPResponse) MarshalTo

func (m *HTTPResponse) MarshalTo(dAtA []byte) (int, error)

func (*HTTPResponse) MarshalToSizedBuffer

func (m *HTTPResponse) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*HTTPResponse) ProtoMessage

func (*HTTPResponse) ProtoMessage()

func (*HTTPResponse) Reset

func (m *HTTPResponse) Reset()

func (*HTTPResponse) Size

func (m *HTTPResponse) Size() (n int)

func (*HTTPResponse) String

func (this *HTTPResponse) String() string

func (*HTTPResponse) Unmarshal

func (m *HTTPResponse) Unmarshal(dAtA []byte) error

func (*HTTPResponse) XXX_DiscardUnknown

func (m *HTTPResponse) XXX_DiscardUnknown()

func (*HTTPResponse) XXX_Marshal

func (m *HTTPResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*HTTPResponse) XXX_Merge

func (m *HTTPResponse) XXX_Merge(src proto.Message)

func (*HTTPResponse) XXX_Size

func (m *HTTPResponse) XXX_Size() int

func (*HTTPResponse) XXX_Unmarshal

func (m *HTTPResponse) XXX_Unmarshal(b []byte) error

type HTTPServer

type HTTPServer interface {
	Handle(context.Context, *HTTPRequest) (*HTTPResponse, error)
}

HTTPServer is the server API for HTTP service.

type Header struct {
	Key    string   `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
	Values []string `protobuf:"bytes,2,rep,name=values,proto3" json:"values,omitempty"`
}

func FromHeader

func FromHeader(hs http.Header) []*Header

func (*Header) Descriptor

func (*Header) Descriptor() ([]byte, []int)

func (*Header) Equal

func (this *Header) Equal(that interface{}) bool

func (*Header) GetKey

func (m *Header) GetKey() string

func (*Header) GetValues

func (m *Header) GetValues() []string

func (*Header) GoString

func (this *Header) GoString() string

func (*Header) Marshal

func (m *Header) Marshal() (dAtA []byte, err error)

func (*Header) MarshalTo

func (m *Header) MarshalTo(dAtA []byte) (int, error)

func (*Header) MarshalToSizedBuffer

func (m *Header) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*Header) ProtoMessage

func (*Header) ProtoMessage()

func (*Header) Reset

func (m *Header) Reset()

func (*Header) Size

func (m *Header) Size() (n int)

func (*Header) String

func (this *Header) String() string

func (*Header) Unmarshal

func (m *Header) Unmarshal(dAtA []byte) error

func (*Header) XXX_DiscardUnknown

func (m *Header) XXX_DiscardUnknown()

func (*Header) XXX_Marshal

func (m *Header) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Header) XXX_Merge

func (m *Header) XXX_Merge(src proto.Message)

func (*Header) XXX_Size

func (m *Header) XXX_Size() int

func (*Header) XXX_Unmarshal

func (m *Header) XXX_Unmarshal(b []byte) error

type UnimplementedHTTPServer

type UnimplementedHTTPServer struct {
}

UnimplementedHTTPServer can be embedded to have forward compatible implementations.

func (*UnimplementedHTTPServer) Handle

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL