apimiddleware

package
v3.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2023 License: GPL-3.0 Imports: 21 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GrpcResponseIsEmpty

func GrpcResponseIsEmpty(grpcResponseBody []byte) bool

GrpcResponseIsEmpty determines whether the grpc-gateway's response body contains no data.

func WriteError

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

type ApiProxyMiddleware struct {
	GatewayAddress  string
	EndpointCreator EndpointFactory
	Timeout         time.Duration
	// 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

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

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

ProxyRequest proxies the request to grpc-gateway.

func (*ApiProxyMiddleware) Run

func (m *ApiProxyMiddleware) Run(gatewayRouter *mux.Router)

Run starts the proxy, registering all proxy endpoints.

func (*ApiProxyMiddleware) ServeHTTP

func (m *ApiProxyMiddleware) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP for the proxy middleware.

func (*ApiProxyMiddleware) WithMiddleware

func (m *ApiProxyMiddleware) WithMiddleware(path string) http.HandlerFunc

WithMiddleware wraps the given endpoint handler with the middleware logic.

type CustomHandler

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

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

func InternalServerError(err error) *DefaultErrorJson

InternalServerError returns a DefaultErrorJson with 500 code.

func InternalServerErrorWithMessage

func InternalServerErrorWithMessage(err error, message string) *DefaultErrorJson

InternalServerErrorWithMessage returns a DefaultErrorJson with 500 code and a custom message.

func TimeoutError

func TimeoutError() *DefaultErrorJson

func (*DefaultErrorJson) Msg

func (e *DefaultErrorJson) Msg() string

Msg returns the error's underlying message.

func (*DefaultErrorJson) SetCode

func (e *DefaultErrorJson) SetCode(code int)

SetCode sets the error's underlying error code.

func (*DefaultErrorJson) SetMsg

func (e *DefaultErrorJson) SetMsg(msg string)

SetMsg sets the error's underlying message.

func (*DefaultErrorJson) StatusCode

func (e *DefaultErrorJson) StatusCode() int

StatusCode returns the error's underlying error code.

type Endpoint

type Endpoint struct {
	Path               string          // The path of the HTTP endpoint.
	GetResponse        interface{}     // The struct corresponding to the JSON structure used in a GET response.
	PostRequest        interface{}     // The struct corresponding to the JSON structure used in a POST request.
	PostResponse       interface{}     // The struct corresponding to the JSON structure used in a POST response.
	DeleteRequest      interface{}     // The struct corresponding to the JSON structure used in a DELETE request.
	DeleteResponse     interface{}     // The struct corresponding to the JSON structure used in a DELETE response.
	RequestURLLiterals []string        // Names of URL parameters that should not be base64-encoded.
	RequestQueryParams []QueryParam    // Query parameters of the request.
	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.
	CustomHandlers     []CustomHandler // Functions that will be executed instead of the default request/response behaviour.
}

Endpoint is a representation of an API HTTP endpoint that should be proxied by the middleware.

func DefaultEndpoint

func DefaultEndpoint() Endpoint

DefaultEndpoint returns an Endpoint with default configuration, e.g. DefaultErrorJson for error handling.

type EndpointFactory

type EndpointFactory interface {
	Create(path string) (*Endpoint, error)
	Paths() []string
	IsNil() bool
}

EndpointFactory is responsible for creating new instances of Endpoint values.

type ErrorJson

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

ErrorJson describes common functionality of all JSON error representations.

func Cleanup

func Cleanup(grpcResponseBody io.ReadCloser) ErrorJson

Cleanup performs final cleanup on the initial response from grpc-gateway.

func DeserializeGrpcResponseBodyIntoContainer

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

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

func DeserializeRequestBodyIntoContainer

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

DeserializeRequestBodyIntoContainer deserializes the request's body into an endpoint-specific struct.

func HandleGrpcResponseError

func HandleGrpcResponseError(errJson ErrorJson, resp *http.Response, respBody []byte, w http.ResponseWriter) (bool, ErrorJson)

HandleGrpcResponseError acts on an error that resulted from a grpc-gateway's response. Whether there was an error is indicated by the bool return value. In case of an error, there is no need to write to the response because it's taken care of by the function.

func HandleQueryParameters

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

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

func ProcessMiddlewareResponseFields(responseContainer interface{}) ErrorJson

ProcessMiddlewareResponseFields processes fields of an endpoint-specific container according to field tags.

func ProcessRequestContainerFields

func ProcessRequestContainerFields(requestContainer interface{}) ErrorJson

ProcessRequestContainerFields processes fields of an endpoint-specific container according to field tags.

func ReadGrpcResponseBody

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

ReadGrpcResponseBody reads the body from the grpc-gateway's response.

func SerializeMiddlewareResponseIntoJson

func SerializeMiddlewareResponseIntoJson(responseContainer interface{}) (jsonResponse []byte, errJson ErrorJson)

SerializeMiddlewareResponseIntoJson serializes the endpoint-specific response struct into a JSON representation.

func SetRequestBodyToRequestContainer

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

SetRequestBodyToRequestContainer makes the endpoint-specific container the new body of the request.

func WriteMiddlewareResponseHeadersAndBody

func WriteMiddlewareResponseHeadersAndBody(grpcResp *http.Response, responseJson []byte, w http.ResponseWriter) ErrorJson

WriteMiddlewareResponseHeadersAndBody populates headers and the body of the final response.

type HookCollection

type HookCollection struct {
	OnPreDeserializeRequestBodyIntoContainer      func(endpoint *Endpoint, w http.ResponseWriter, req *http.Request) (RunDefault, ErrorJson)
	OnPostDeserializeRequestBodyIntoContainer     func(endpoint *Endpoint, w http.ResponseWriter, req *http.Request) ErrorJson
	OnPreDeserializeGrpcResponseBodyIntoContainer func([]byte, interface{}) (RunDefault, ErrorJson)
	OnPreSerializeMiddlewareResponseIntoJson      func(interface{}) (RunDefault, []byte, ErrorJson)
}

HookCollection contains hooks that can be used to amend the default request/response cycle with custom logic for a specific endpoint.

type QueryParam

type QueryParam struct {
	Name string
	Hex  bool
	Enum bool
}

QueryParam represents a single query parameter's metadata.

type RunDefault

type RunDefault bool

RunDefault expresses whether the default processing logic should be carried out after running a pre hook.

Jump to

Keyboard shortcuts

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