rpcs

package
v0.0.0-...-2bb2ce4 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const HttpHeaderTraceID = "X-TRACE-ID"

Variables

This section is empty.

Functions

func ParseTraceID

func ParseTraceID(s string) int64

ParseTraceID parses a traceID from a string and in case of failure it returns a default -1

Types

type Decoder

type Decoder interface {
	// Decode decodes the provided payload with its format from the
	// provided reader. In case of failure it is possible a partial
	// read has occurred
	Decode(r io.Reader, v interface{}) error
}

Decoder for payloads

type DeserializeFunc

type DeserializeFunc func(r io.Reader) error

DeserializerFunc implementation of Deserializer for functions

func (DeserializeFunc) Deserialize

func (f DeserializeFunc) Deserialize(r io.Reader) error

Deserialize is the implementation of Deserialize for DeserializeFunc

type Deserializer

type Deserializer interface {
	// Deserialize the contents of the reader into an object owned
	// by the deserializer
	Deserialize(r io.Reader) error
}

Deserializer decodes payloads and keeps the state of the deserialized object

type Encoder

type Encoder interface {
	// Encode encodes the provided payload with its format to the
	// provided writer. In case of failure it is possible a partial
	// write of the serialization to the writer
	Encode(w io.Writer, v interface{}) error
}

Encoder for payloads

type EntityFactory

type EntityFactory interface {
	Create() interface{}
}

EntityFactory is an interface for types that build an object of some kind

type EntityFactoryFunc

type EntityFactoryFunc func() interface{}

EntityFactoryFunc is a type to allow functions to act as a Factory

func (EntityFactoryFunc) Create

func (f EntityFactoryFunc) Create() interface{}

Create is the implementation of Factory for FactoryFunc

type ErrMsgs

type ErrMsgs error
var (
	ErrHttpContentLengthMissing ErrMsgs = errors.New("content-length header missing in request")
	ErrHttpContentLengthExceeds ErrMsgs = errors.New("content-length value exceeds request limit")
	ErrHttpContentTypeNotJSON   ErrMsgs = errors.New("content-type has unexpected value")
	ErrHttpHandleExpectsNoBody  ErrMsgs = errors.New("http handle expects no request body")
	ErrHttpDecodeJSON           ErrMsgs = errors.New("error decoding body as json")
)

type Handler

type Handler interface {
	// Handle handlers an rpc request and returns a response or error if needed.
	// Implementations should ensure that if a context is cancelled the request
	// handling should be halt gracefully and return an appropriate error.
	Handle(ctx context.Context, req interface{}) (interface{}, error)
}

Handler is the handler for RPC requests.

type HandlerBinder

type HandlerBinder interface {
	// Bind binds a handler for a specific method and path, so that the handler
	// will be dispatched when method and path combination is provided
	Bind(method string, path string, handler Handler, factory EntityFactory)
}

HandlerBinder binds a handler to a specific method and path

type HandlerFunc

type HandlerFunc func(ctx context.Context, req interface{}) (interface{}, error)

HandlerFunc is the type definition for a function to be able to act as a Handler

func (HandlerFunc) Handle

func (h HandlerFunc) Handle(ctx context.Context, req interface{}) (interface{}, error)

Handle is the implementation of the Handler interface for a HandlerFunc

type HttpBinder

type HttpBinder struct {
	// contains filtered or unexported fields
}

HttpBinder is the binder for http. It is also the only mechanism to build HttpRouter's. This is done so that an HttpRouter cannot be modified after it has been created

func NewHttpBinder

func NewHttpBinder(properties HttpBinderProperties) *HttpBinder

NewHttpBinder creates a new instance of the HttpBinder. It will panic in case there are errors in the construction of the binder

func (*HttpBinder) AddPreProcessor

func (b *HttpBinder) AddPreProcessor(preProcessor HttpPreProcessor)

func (*HttpBinder) Bind

func (b *HttpBinder) Bind(method string, uri string, handler Handler, factory EntityFactory)

Bind is the implementation of HandlerBinder for HttpBinder

func (*HttpBinder) Build

func (b *HttpBinder) Build() *HttpRouter

Build creates a new HttpRouter and clears the handler map of the HttpBinder, so if new instances of HttpRouters need to be build Bind needs to be used again

type HttpBinderProperties

type HttpBinderProperties struct {
	Encoder        Encoder
	Logger         logs.Logger
	HandlerFactory HttpHandlerFactory
}

HttpBinderProperties are the properties used to create a new instance of an HttpBinder

type HttpCorsPreProcessor

type HttpCorsPreProcessor struct {
	// contains filtered or unexported fields
}

HttpCorsPreProcessor handles CORS https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS for requests

func NewHttpCorsPreProcessor

func NewHttpCorsPreProcessor(props HttpCorsPreProcessorProps) *HttpCorsPreProcessor

NewHttpCorsPreProcessor creates a new instance of a Cors Http PreProcessor

func (*HttpCorsPreProcessor) ServeHTTP

ServeHTTP is the implementation of HttpMiddleware for HttpCorsHandler

type HttpCorsPreProcessorProps

type HttpCorsPreProcessorProps struct {
	// Enabled if true the HttpCorsHandler will verify requests, if false
	// the handler will just pass on a request to the next middleware
	Enabled bool

	// AllowedOrigins is a list of origins a cross-domain request can be executed from.
	// If the special "*" value is present in the list, all origins will be allowed.
	// An origin may contain a wildcard (*) to replace 0 or more characters
	// (i.e.: http://*.domain.com). Usage of wildcards implies a small performance penalty.
	// Only one wildcard can be used per origin.
	// Default value is ["*"]
	AllowedOrigins []string

	// AllowedMethods is a list of methods the client is allowed to use with
	// cross-domain requests. Default value is simple methods (HEAD, GET and POST).
	AllowedMethods []string

	// AllowedHeaders is list of non simple headers the client is allowed to use with
	// cross-domain requests.
	// If the special "*" value is present in the list, all headers will be allowed.
	// Default value is [] but "Origin" is always appended to the list.
	AllowedHeaders []string

	// ExposedHeaders indicates which headers are safe to expose to the API of a CORS
	// API specification
	ExposedHeaders []string

	// MaxAge indicates how long (in seconds) the results of a preflight request
	// can be cached
	MaxAge int

	// AllowCredentials indicates whether the request can include user credentials like
	// cookies, HTTP authentication or client side SSL certificates.
	AllowCredentials bool
}

HttpCorsPreProcessorProps properties used to define the behaviour of the CORS implementation

type HttpError

type HttpError struct {
	// Cause of the creation of this HttpError instance
	Cause error

	// StatusCode is the HTTP status code that defines the error cause
	StatusCode int

	// Message is the human-readable string that defines the error cause
	Message string
}

HttpError holds the necessary information to return an error when using the http protocol

func HttpBadRequest

func HttpBadRequest(ctx context.Context, err error) *HttpError

HttpBadRequest returns an HTTP bad request error

func HttpBadRequestWithMessage

func HttpBadRequestWithMessage(ctx context.Context, err error, msg string) *HttpError

HttpBadRequestWithMessage returns an HTTP bad request error with a custom message

func HttpForbidden

func HttpForbidden(ctx context.Context, err error) *HttpError

HttpForbidden returns an HTTP not found error

func HttpInternalServerError

func HttpInternalServerError(ctx context.Context, err error) *HttpError

HttpInternalServerError returns an HTTP internal server error

func HttpMethodNotAllowed

func HttpMethodNotAllowed(ctx context.Context, err error) *HttpError

HttpMethodNotAllowed returns an HTTP not found error

func HttpNotFound

func HttpNotFound(ctx context.Context, err error) *HttpError

HttpNotFound returns an HTTP not found error

func HttpNotImplemented

func HttpNotImplemented(ctx context.Context, err error) *HttpError

HttpNotFound returns an HTTP not found error

func HttpTooManyRequests

func HttpTooManyRequests(ctx context.Context, err error) *HttpError

HttpTooMayRequests return an HTTP too many requests error

func MakeHttpError

func MakeHttpError(ctx context.Context, err error, statusCode int, msg string) *HttpError

MakeHttpError makes a new http error

func (*HttpError) Error

func (e *HttpError) Error() string

Error is the implementation of go's error interface for Error

func (*HttpError) Log

func (e *HttpError) Log(fields logs.Fields)

Log implementation of logs.Loggable

type HttpHandlerFactory

type HttpHandlerFactory interface {
	Make(factory EntityFactory, handler Handler) HttpMiddleware
}

HttpHandlerFactory converts an rpc Handler into HttpMiddleware that can be plugged into a router

type HttpHandlerFactoryFunc

type HttpHandlerFactoryFunc func(factory EntityFactory, handler Handler) HttpMiddleware

HttpHandlerFactoryFunc to allow functions to act as an HttpHandlerFactory

func (HttpHandlerFactoryFunc) Make

Make is the implementation of HttpHandlerFactory for HttpHandlerFactoryFunc

type HttpJsonHandler

type HttpJsonHandler struct {
	// contains filtered or unexported fields
}

HttpJsonHandler handles requests that expect a body in the JSON format, handles the body and executes the final handler with the expected type

func NewHttpJsonHandler

func NewHttpJsonHandler(properties HttpJsonHandlerProperties) *HttpJsonHandler

NewHttpJsonHandlerFactory creates a new instance of an rpc handler that deserializes json objects into Go objects

func (*HttpJsonHandler) ServeHTTP

func (h *HttpJsonHandler) ServeHTTP(req *http.Request) (interface{}, error)

ServeHTTP is the implementation of HttpMiddleware for HttpJsonHandler

type HttpJsonHandlerProperties

type HttpJsonHandlerProperties struct {
	// Limit is the maximum number of bytes an Http body can have. Bodies
	// with a higher limit will fail to deserialize and be rejected
	Limit uint

	// Handler is the rpc handler that will be used to handle the request
	Handler Handler

	// Logger
	Logger logs.Logger

	// Factory for creating new instances of objects to which the Http body
	// will be deserialized. Those instances will be passed to the handler
	Factory EntityFactory
}

type HttpMiddleware

type HttpMiddleware interface {
	// ServeHTTP allows to handle an http request. The response will be serialized
	// by an HttpRouter
	ServeHTTP(req *http.Request) (interface{}, error)
}

HttpMiddleware are the handlers that offer extra functionality to a request and that in success will forward the request to another handler

type HttpMiddlewareFunc

type HttpMiddlewareFunc func(req *http.Request) (interface{}, error)

HttpMiddlewareFunc allows functions to implement the HttpMiddleware interface

func (HttpMiddlewareFunc) ServeHTTP

func (f HttpMiddlewareFunc) ServeHTTP(req *http.Request) (interface{}, error)

type HttpPreProcessor

type HttpPreProcessor interface {
	// ServeHTTP is a similar interface to http.Handler with the difference that
	// it returns true parameters. The boolean parameter indicates in case of its
	// value being true that the request can be further processed by another handler.
	// In case that it's false, no further processing of the request is required.
	// The *http.Request returned is a potentially modified request resulting of
	// mutating the original *http.Request
	ServeHTTP(w http.ResponseWriter, req *http.Request) (HttpPreProcessorResult, error)
}

HttpPreProcessor processes a request and can directly write a response to the writer if required.

type HttpPreProcessorResult

type HttpPreProcessorResult struct {
	Request  *http.Request
	Continue bool
}

type HttpRoute

type HttpRoute struct {
	// contains filtered or unexported fields
}

HttpRoute multiplexes the handling of a request to the handler that expects a particular method

func NewHttpRoute

func NewHttpRoute(props HttpRouteProps) *HttpRoute

NewHttpRoute creates a new route instance

func (*HttpRoute) HasHandler

func (h *HttpRoute) HasHandler(method string) bool

HasHandler returns true if the route has a handler that would handle the provided method

func (*HttpRoute) ServeHTTP

func (h *HttpRoute) ServeHTTP(res http.ResponseWriter, req *http.Request)

HttpRoute implementation of HttpMiddleware

type HttpRouteProps

type HttpRouteProps struct {
	Logger        logs.Logger
	Encoder       Encoder
	Handlers      MethodHandlers
	PreProcessors []HttpPreProcessor
}

HttpRouteProps are the required properties to create a new HttpRoute instance

type HttpRouter

type HttpRouter struct {
	// contains filtered or unexported fields
}

HttpRouter multiplexes the handling of server request amongst the different handlers

func (*HttpRouter) HasHandler

func (h *HttpRouter) HasHandler(path, method string) bool

HasHandler returns true if the router has a handle to handle a request to the path and method

func (*HttpRouter) HasRoute

func (h *HttpRouter) HasRoute(path string) bool

HasRoute returns true if the router has a route to handle a request to the path

func (*HttpRouter) ServeHTTP

func (h *HttpRouter) ServeHTTP(res http.ResponseWriter, req *http.Request)

HttpRouter implementation of http.Handler

type JsonDecoder

type JsonDecoder struct{}

JsonEncoder is a payload encoder that serializes to JSON

func (JsonDecoder) Decode

func (e JsonDecoder) Decode(reader io.Reader, v interface{}) error

Decode is the implementation of Decoder for JsonDecoder

func (JsonDecoder) DecodeWithLimit

func (e JsonDecoder) DecodeWithLimit(reader io.Reader, v interface{}, props readwrite.ReadLimitProps) error

DecodeWithLimit decodes the payload in the reader making sure not to exceed the limit provided

type JsonEncoder

type JsonEncoder struct{}

JsonEncoder is a payload encoder that serializes to JSON

func (JsonEncoder) Encode

func (e JsonEncoder) Encode(writer io.Writer, v interface{}) error

Encode is the implementation of Encoder for JsonEncoder

type MethodHandlers

type MethodHandlers map[string]HttpMiddleware

MethodHandlers keeps the handlers for each of the methods

func (MethodHandlers) Add

func (h MethodHandlers) Add(method string, middleware HttpMiddleware)

Add a new handler to the set

type SerializeFunc

type SerializeFunc func(w io.Writer) error

SerializerFunc implementation of Serializer for functions

func (SerializeFunc) Serialize

func (f SerializeFunc) Serialize(w io.Writer) error

Serialize is the implementation of Serialize for SerializeFunc

type Serializer

type Serializer interface {
	// Serialize an object into the provided writer
	Serialize(w io.Writer) error
}

Serializer encodes payloads and keeps the state of the serialized object

type SimpleJsonDeserializer

type SimpleJsonDeserializer struct {
	O interface{}
}

SimpleJsonDeserializer deserializes a json payload into an object

func (*SimpleJsonDeserializer) Deserialize

func (s *SimpleJsonDeserializer) Deserialize(r io.Reader) error

Deserialize is the implementation of Deserialize for SimpleJsonDeserializer

type SimpleJsonSerializer

type SimpleJsonSerializer struct {
	O interface{}
}

SimpleJsonSerializer deserializes a json payload into an object

func (*SimpleJsonSerializer) Serialize

func (s *SimpleJsonSerializer) Serialize(w io.Writer) error

Serialize is the implementation of Serialize for SimpleJsonSerializer

Jump to

Keyboard shortcuts

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