gateway

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: GPL-3.0 Imports: 28 Imported by: 0

Documentation

Overview

Package gateway defines a grpc-gateway server that serves HTTP-JSON traffic and acts a proxy between HTTP and gRPC.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GrpcResponseIsStatusCodeOnly added in v1.4.1

func GrpcResponseIsStatusCodeOnly(req *http.Request, responseContainer interface{}) bool

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

type DefaultErrorJson struct {
	Message string `json:"message"`
	Code    int    `json:"code"`
}

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

type ErrorJson interface {
	StatusCode() int
	SetCode(code int)
	Msg() string
}

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

func DeserializeGrpcResponseBodyIntoContainer(body []byte, responseContainer interface{}) ErrorJson

DeserializeGrpcResponseBodyIntoContainer deserializes the grpc-gateway's response body into an endpoint-specific struct.

func DeserializeGrpcResponseBodyIntoErrorJson added in v1.4.1

func DeserializeGrpcResponseBodyIntoErrorJson(errJson ErrorJson, body []byte) ErrorJson

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

func DeserializeRequestBodyIntoContainer(body io.Reader, requestContainer interface{}) ErrorJson

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

func HandleURLParameters(url string, req *http.Request, literals []string) ErrorJson

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

func ProxyRequest(req *http.Request) (*http.Response, ErrorJson)

ProxyRequest proxies the request to grpc-gateway.

func ReadGrpcResponseBody added in v1.4.1

func ReadGrpcResponseBody(r io.Reader) ([]byte, ErrorJson)

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

func SetRequestBodyToRequestContainer(requestContainer interface{}, req *http.Request) ErrorJson

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) Start

func (g *Gateway) Start()

Start the gateway service.

func (*Gateway) Status

func (g *Gateway) Status() error

Status of grpc gateway. Returns an error if this service is unhealthy.

func (*Gateway) Stop

func (g *Gateway) Stop() error

Stop the gateway with a graceful shutdown.

func (*Gateway) WithAllowedOrigins added in v1.4.1

func (g *Gateway) WithAllowedOrigins(origins []string) *Gateway

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

func (g *Gateway) WithMaxCallRecvMsgSize(size uint64) *Gateway

WithMaxCallRecvMsgSize allows specifying the maximum allowed gRPC message size.

func (*Gateway) WithMux added in v1.4.1

func (g *Gateway) WithMux(m *http.ServeMux) *Gateway

WithMux allows adding a custom http.ServeMux to the gateway.

func (*Gateway) WithRemoteCert added in v1.4.1

func (g *Gateway) WithRemoteCert(cert string) *Gateway

WithRemoteCert allows adding a custom certificate to the gateway,

type Hook added in v1.4.1

type Hook = func(endpoint Endpoint, w http.ResponseWriter, req *http.Request) ErrorJson

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

type MuxHandler func(http.Handler, http.ResponseWriter, *http.Request)

MuxHandler is a function that implements the mux handler functionality.

type PbHandlerRegistration added in v1.4.1

type PbHandlerRegistration func(context.Context, *gwruntime.ServeMux, *grpc.ClientConn) error

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

type QueryParam struct {
	Name string
	Hex  bool
	Enum bool
}

QueryParam represents a single query parameter's metadata.

Jump to

Keyboard shortcuts

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