Documentation ¶
Overview ¶
Package gateway defines a grpc-gateway server that serves HTTP-JSON traffic and acts a proxy between HTTP and gRPC.
Index ¶
- func GrpcResponseIsStatusCodeOnly(req *http.Request, responseContainer interface{}) bool
- func HandleGrpcResponseError(errJson ErrorJson, resp *http.Response, w http.ResponseWriter)
- func SwaggerServer() http.HandlerFunc
- func WriteError(w http.ResponseWriter, errJson ErrorJson, responseHeader http.Header)
- type ApiProxyMiddleware
- type CustomHandler
- type DefaultErrorJson
- type Endpoint
- type EndpointFactory
- type ErrorJson
- func Cleanup(grpcResponseBody io.ReadCloser) ErrorJson
- func DeserializeGrpcResponseBodyIntoContainer(body []byte, responseContainer interface{}) ErrorJson
- func DeserializeGrpcResponseBodyIntoErrorJson(errJson ErrorJson, body []byte) ErrorJson
- func DeserializeRequestBodyIntoContainer(body io.Reader, requestContainer interface{}) ErrorJson
- func HandleQueryParameters(req *http.Request, params []QueryParam) ErrorJson
- func HandleURLParameters(url string, req *http.Request, literals []string) ErrorJson
- func ProcessMiddlewareResponseFields(responseContainer interface{}) ErrorJson
- func ProcessRequestContainerFields(requestContainer interface{}) ErrorJson
- func ProxyRequest(req *http.Request) (*http.Response, ErrorJson)
- func ReadGrpcResponseBody(r io.Reader) ([]byte, ErrorJson)
- func SerializeMiddlewareResponseIntoJson(responseContainer interface{}) (jsonResponse []byte, errJson ErrorJson)
- func SetRequestBodyToRequestContainer(requestContainer interface{}, req *http.Request) ErrorJson
- func WriteMiddlewareResponseHeadersAndBody(req *http.Request, grpcResp *http.Response, responseJson []byte, ...) ErrorJson
- type Gateway
- func (g *Gateway) Start()
- func (g *Gateway) Status() error
- func (g *Gateway) Stop() error
- func (g *Gateway) WithAllowedOrigins(origins []string) *Gateway
- func (g *Gateway) WithApiMiddleware(address string, endpointFactory EndpointFactory) *Gateway
- func (g *Gateway) WithMaxCallRecvMsgSize(size uint64) *Gateway
- func (g *Gateway) WithMux(m *http.ServeMux) *Gateway
- func (g *Gateway) WithRemoteCert(cert string) *Gateway
- type Hook
- type HookCollection
- type MuxHandler
- type PbHandlerRegistration
- type PbMux
- type QueryParam
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GrpcResponseIsStatusCodeOnly ¶ added in v1.4.1
GrpcResponseIsStatusCodeOnly checks whether a grpc-gateway's response contained no body.
func HandleGrpcResponseError ¶ added in v1.4.1
func HandleGrpcResponseError(errJson ErrorJson, resp *http.Response, w http.ResponseWriter)
HandleGrpcResponseError acts on an error that resulted from a grpc-gateway's response.
func SwaggerServer ¶
func SwaggerServer() http.HandlerFunc
SwaggerServer returns swagger specification files located under "/swagger/"
func WriteError ¶ added in v1.4.1
func WriteError(w http.ResponseWriter, errJson ErrorJson, responseHeader http.Header)
WriteError writes the error by manipulating headers and the body of the final response.
Types ¶
type ApiProxyMiddleware ¶ added in v1.4.1
type ApiProxyMiddleware struct { GatewayAddress string ProxyAddress string EndpointCreator EndpointFactory // contains filtered or unexported fields }
ApiProxyMiddleware is a proxy between an Ethereum consensus API HTTP client and grpc-gateway. The purpose of the proxy is to handle HTTP requests and gRPC responses in such a way that:
- Ethereum consensus API requests can be handled by grpc-gateway correctly
- gRPC responses can be returned as spec-compliant Ethereum consensus API responses
func (*ApiProxyMiddleware) PrepareRequestForProxying ¶ added in v1.4.1
func (m *ApiProxyMiddleware) PrepareRequestForProxying(endpoint Endpoint, req *http.Request) ErrorJson
PrepareRequestForProxying applies additional logic to the request so that it can be correctly proxied to grpc-gateway.
func (*ApiProxyMiddleware) Run ¶ added in v1.4.1
func (m *ApiProxyMiddleware) Run() error
Run starts the proxy, registering all proxy endpoints on ApiProxyMiddleware.ProxyAddress.
type CustomHandler ¶ added in v1.4.1
type CustomHandler = func(m *ApiProxyMiddleware, endpoint Endpoint, w http.ResponseWriter, req *http.Request) (handled bool)
CustomHandler is a function that can be invoked at the very beginning of the request, essentially replacing the whole default request/response logic with custom logic for a specific endpoint.
type DefaultErrorJson ¶ added in v1.4.1
DefaultErrorJson is a JSON representation of a simple error value, containing only a message and an error code.
func InternalServerError ¶ added in v1.4.1
func InternalServerError(err error) *DefaultErrorJson
InternalServerError returns a DefaultErrorJson with 500 code.
func InternalServerErrorWithMessage ¶ added in v1.4.1
func InternalServerErrorWithMessage(err error, message string) *DefaultErrorJson
InternalServerErrorWithMessage returns a DefaultErrorJson with 500 code and a custom message.
func (*DefaultErrorJson) Msg ¶ added in v1.4.1
func (e *DefaultErrorJson) Msg() string
Msg returns the error's underlying message.
func (*DefaultErrorJson) SetCode ¶ added in v1.4.1
func (e *DefaultErrorJson) SetCode(code int)
SetCode sets the error's underlying error code.
func (*DefaultErrorJson) StatusCode ¶ added in v1.4.1
func (e *DefaultErrorJson) StatusCode() int
StatusCode returns the error's underlying error code.
type Endpoint ¶ added in v1.4.1
type Endpoint struct { Path string // The path of the HTTP endpoint. PostRequest interface{} // The struct corresponding to the JSON structure used in a POST request. GetRequestURLLiterals []string // Names of URL parameters that should not be base64-encoded. GetRequestQueryParams []QueryParam // Query parameters of the GET request. GetResponse interface{} // The struct corresponding to the JSON structure used in a GET response. Err ErrorJson // The struct corresponding to the error that should be returned in case of a request failure. Hooks HookCollection // A collection of functions that can be invoked at various stages of the request/response cycle. }
Endpoint is a representation of an API HTTP endpoint that should be proxied by the middleware.
type EndpointFactory ¶ added in v1.4.1
type EndpointFactory interface { Create(path string) (*Endpoint, error) Paths() []string IsNil() bool }
EndpointFactory is responsible for creating new instances of Endpoint values.
type ErrorJson ¶ added in v1.4.1
ErrorJson describes common functionality of all JSON error representations.
func Cleanup ¶ added in v1.4.1
func Cleanup(grpcResponseBody io.ReadCloser) ErrorJson
Cleanup performs final cleanup on the initial response from grpc-gateway.
func DeserializeGrpcResponseBodyIntoContainer ¶ added in v1.4.1
DeserializeGrpcResponseBodyIntoContainer deserializes the grpc-gateway's response body into an endpoint-specific struct.
func DeserializeGrpcResponseBodyIntoErrorJson ¶ added in v1.4.1
DeserializeGrpcResponseBodyIntoErrorJson deserializes the body from the grpc-gateway's response into an error struct. The struct can be later examined to check if the request resulted in an error.
func DeserializeRequestBodyIntoContainer ¶ added in v1.4.1
DeserializeRequestBodyIntoContainer deserializes the request's body into an endpoint-specific struct.
func HandleQueryParameters ¶ added in v1.4.1
func HandleQueryParameters(req *http.Request, params []QueryParam) ErrorJson
HandleQueryParameters processes query parameters, allowing them to be safely and correctly proxied to grpc-gateway.
func HandleURLParameters ¶ added in v1.4.1
HandleURLParameters processes URL parameters, allowing parameterized URLs to be safely and correctly proxied to grpc-gateway.
func ProcessMiddlewareResponseFields ¶ added in v1.4.1
func ProcessMiddlewareResponseFields(responseContainer interface{}) ErrorJson
ProcessMiddlewareResponseFields processes fields of an endpoint-specific container according to field tags.
func ProcessRequestContainerFields ¶ added in v1.4.1
func ProcessRequestContainerFields(requestContainer interface{}) ErrorJson
ProcessRequestContainerFields processes fields of an endpoint-specific container according to field tags.
func ProxyRequest ¶ added in v1.4.1
ProxyRequest proxies the request to grpc-gateway.
func ReadGrpcResponseBody ¶ added in v1.4.1
ReadGrpcResponseBody reads the body from the grpc-gateway's response.
func SerializeMiddlewareResponseIntoJson ¶ added in v1.4.1
func SerializeMiddlewareResponseIntoJson(responseContainer interface{}) (jsonResponse []byte, errJson ErrorJson)
SerializeMiddlewareResponseIntoJson serializes the endpoint-specific response struct into a JSON representation.
func SetRequestBodyToRequestContainer ¶ added in v1.4.1
SetRequestBodyToRequestContainer makes the endpoint-specific container the new body of the request.
func WriteMiddlewareResponseHeadersAndBody ¶ added in v1.4.1
func WriteMiddlewareResponseHeadersAndBody(req *http.Request, grpcResp *http.Response, responseJson []byte, w http.ResponseWriter) ErrorJson
WriteMiddlewareResponseHeadersAndBody populates headers and the body of the final response.
type Gateway ¶
type Gateway struct {
// contains filtered or unexported fields
}
Gateway is the gRPC gateway to serve HTTP JSON traffic as a proxy and forward it to the gRPC server.
func New ¶ added in v1.4.1
func New( ctx context.Context, pbHandlers []PbMux, muxHandler MuxHandler, remoteAddr, gatewayAddress string, ) *Gateway
New returns a new instance of the Gateway.
func (*Gateway) WithAllowedOrigins ¶ added in v1.4.1
WithAllowedOrigins allows adding a set of allowed origins to the gateway.
func (*Gateway) WithApiMiddleware ¶ added in v1.4.1
func (g *Gateway) WithApiMiddleware(address string, endpointFactory EndpointFactory) *Gateway
WithApiMiddleware allows adding API Middleware proxy to the gateway.
func (*Gateway) WithMaxCallRecvMsgSize ¶ added in v1.4.1
WithMaxCallRecvMsgSize allows specifying the maximum allowed gRPC message size.
func (*Gateway) WithMux ¶ added in v1.4.1
WithMux allows adding a custom http.ServeMux to the gateway.
func (*Gateway) WithRemoteCert ¶ added in v1.4.1
WithRemoteCert allows adding a custom certificate to the gateway,
type Hook ¶ added in v1.4.1
Hook is a function that can be invoked at various stages of the request/response cycle, leading to custom behaviour for a specific endpoint.
type HookCollection ¶ added in v1.4.1
type HookCollection struct { CustomHandlers []CustomHandler OnPostStart []Hook OnPostDeserializeRequestBodyIntoContainer []Hook }
HookCollection contains handlers/hooks that can be used to amend the default request/response cycle with custom logic for a specific endpoint.
type MuxHandler ¶ added in v1.4.1
MuxHandler is a function that implements the mux handler functionality.
type PbHandlerRegistration ¶ added in v1.4.1
PbHandlerRegistration is a function that registers a protobuf handler.
type PbMux ¶ added in v1.4.1
type PbMux struct { Registrations []PbHandlerRegistration // Protobuf registrations to be registered in Mux. Patterns []string // URL patterns that will be handled by Mux. Mux *gwruntime.ServeMux // The mux that will be used for grpc-gateway requests. }
PbMux serves grpc-gateway requests for selected patterns using registered protobuf handlers.
type QueryParam ¶ added in v1.4.1
QueryParam represents a single query parameter's metadata.