Documentation ¶
Index ¶
- func GrpcResponseIsEmpty(grpcResponseBody []byte) bool
- func WriteError(w http.ResponseWriter, errJson ErrorJson, responseHeader http.Header)
- type ApiProxyMiddleware
- func (m *ApiProxyMiddleware) PrepareRequestForProxying(endpoint Endpoint, req *http.Request) ErrorJson
- func (m *ApiProxyMiddleware) ProxyRequest(req *http.Request) (*http.Response, ErrorJson)
- func (m *ApiProxyMiddleware) Run(gatewayRouter *mux.Router)
- func (m *ApiProxyMiddleware) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (m *ApiProxyMiddleware) WithMiddleware(path string) http.HandlerFunc
- type CustomHandler
- type DefaultErrorJson
- type Endpoint
- type EndpointFactory
- type ErrorJson
- func Cleanup(grpcResponseBody io.ReadCloser) ErrorJson
- func DeserializeGrpcResponseBodyIntoContainer(body []byte, responseContainer interface{}) ErrorJson
- func DeserializeRequestBodyIntoContainer(body io.Reader, requestContainer interface{}) ErrorJson
- func HandleGrpcResponseError(errJson ErrorJson, resp *http.Response, respBody []byte, w http.ResponseWriter) (bool, 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 ReadGrpcResponseBody(r io.Reader) ([]byte, ErrorJson)
- func SerializeMiddlewareResponseIntoJson(responseContainer interface{}) (jsonResponse []byte, errJson ErrorJson)
- func SetRequestBodyToRequestContainer(requestContainer interface{}, req *http.Request) ErrorJson
- func WriteMiddlewareResponseHeadersAndBody(grpcResp *http.Response, responseJson []byte, w http.ResponseWriter) ErrorJson
- type HookCollection
- type QueryParam
- type RunDefault
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GrpcResponseIsEmpty ¶
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 ¶
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 ¶
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 ¶
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 ¶
DeserializeGrpcResponseBodyIntoContainer deserializes the grpc-gateway's response body into an endpoint-specific struct.
func DeserializeRequestBodyIntoContainer ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.