endpoint

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInternalServerError = errors.New(icInternalServerError, "internal server error")
	ErrBadRequest          = errors.New(icBadRequest, "bad request")
)

Functions

func ShouldContinue added in v0.0.5

func ShouldContinue(ctx context.Context) bool

ShouldContinue returns true if the underlying request has not been cancelled nor deadline exceeded.

Types

type Authorizer added in v0.0.9

type Authorizer interface {
	Authorization(ctx context.Context, PeekHeader func(key string) []byte) (int, error)
}

Authorizer defines request authentication.

type Config

type Config struct {
	LogConfig         log.Configer
	ErrorHandler      ErrorHandler
	RequestValidator  RequestValidator
	ResponseValidator ResponseValidator
	MimeTypeHandlers  MimeTypeHandlers
	Resources         map[string]interface{}
	Timeout           time.Duration
	Tracer            opentracing.Tracer
}

Config defines the behavior of an endpoint. Endpoint behavior is interface driven and can be completely modified by an application. The values in the config must never be modified by an endpoint.

type Endpoint

type Endpoint struct {
	Config *Config
	// contains filtered or unexported fields
}

Endpoint defines the behavior of a given handler.

func NewEndpoint

func NewEndpoint(ctx context.Context, config *Config, handler Handler) *Endpoint

Create a new endpoint that will run the given handler. This will be created by the Server during normal operations.

func (*Endpoint) Execute

func (self *Endpoint) Execute(ctx context.Context, requester Requester) (httpStatus int, responseBody []byte)

Execute the endpoint and run the endpoint handler.

func (*Endpoint) Name added in v0.0.4

func (self *Endpoint) Name() string

type ErrorHandler

type ErrorHandler interface {
	HandleError(ctx context.Context, httpStatus int, err error) (int, interface{})
}

type Handler

type Handler interface {
	// Handle business logic.
	// While not explicitly prevented, this function should
	//   not touch the request or do any post processing of
	//   any request data.
	Handle(ctx context.Context) (int, error)
}

Handler is the hook into the request handler. Handler struct should contain all data that should be

populated and validated before any business logic is run.

type HttpRequester added in v0.0.4

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

func NewHttpRequester added in v0.0.4

func NewHttpRequester(matchedPath string, request *http.Request) *HttpRequester

func (*HttpRequester) Accept added in v0.0.4

func (self *HttpRequester) Accept() []byte

func (*HttpRequester) ContentType added in v0.0.4

func (self *HttpRequester) ContentType() []byte

func (*HttpRequester) MatchedPath added in v0.0.4

func (self *HttpRequester) MatchedPath() string

func (*HttpRequester) Method added in v0.0.4

func (self *HttpRequester) Method() []byte

func (*HttpRequester) Path added in v0.0.4

func (self *HttpRequester) Path() []byte

func (*HttpRequester) PathParam added in v0.0.4

func (self *HttpRequester) PathParam(param string) (string, bool)

func (*HttpRequester) PeekHeader added in v0.0.6

func (self *HttpRequester) PeekHeader(key string) []byte

func (*HttpRequester) QueryParam added in v0.0.4

func (self *HttpRequester) QueryParam(param string) ([]byte, bool)

func (*HttpRequester) RequestBody added in v0.0.4

func (self *HttpRequester) RequestBody() []byte

func (*HttpRequester) RequestId added in v0.0.4

func (self *HttpRequester) RequestId() string

func (*HttpRequester) ResponseContentType added in v0.0.4

func (self *HttpRequester) ResponseContentType() string

func (*HttpRequester) ResponseHeaders added in v0.0.6

func (self *HttpRequester) ResponseHeaders() map[string]string

func (*HttpRequester) SetResponseContentType added in v0.0.4

func (self *HttpRequester) SetResponseContentType(contentType string)

func (*HttpRequester) SetResponseHeader added in v0.0.4

func (self *HttpRequester) SetResponseHeader(header string, value string)

func (*HttpRequester) VisitHeaders added in v0.0.4

func (self *HttpRequester) VisitHeaders(f func(key []byte, value []byte))

type Marshaler

type Marshaler func(v interface{}) ([]byte, error)

type MimeTypeHandler

type MimeTypeHandler struct {
	MimeType  string
	Marshal   Marshaler
	Unmarshal Unmarshaler
}

MimeTypeHandler defines how a mime type is used. This is used by the "mime" struct tag option.

type MimeTypeHandlers added in v0.0.3

type MimeTypeHandlers map[string]*MimeTypeHandler

func NewMimeTypeHandlers added in v0.0.3

func NewMimeTypeHandlers() MimeTypeHandlers

func (MimeTypeHandlers) Get added in v0.0.3

func (m MimeTypeHandlers) Get(contentType []byte, supportedMimeTypes []string) (*MimeTypeHandler, bool)

Get the MIME type handler for the request content type as well as checking that it is supported by the endpoint.

type RequestValidator

type RequestValidator interface {
	// ValidateRequest and return validation failures.
	// Errors returned are passed straight through to the ErrorHandler.
	// Returned status code is not used unless error is not nil.
	ValidateRequest(ctx context.Context, requestBody []byte) (int, error)
}

type Requester added in v0.0.4

type Requester interface {
	RequestId() string

	// HTTP Method.
	Method() []byte
	// Path of the actual request URL.
	Path() []byte

	ContentType() []byte
	Accept() []byte

	PeekHeader(key string) []byte
	VisitHeaders(f func(key []byte, value []byte))

	// MatchedPath returns the endpoint path that the request URL matches.
	// Ex: /some/path/{id}
	MatchedPath() string

	// PathParam returns the path parameter value for the given parameter name.
	// Returns false if parameter not found.
	PathParam(param string) (string, bool)
	QueryParam(param string) ([]byte, bool)

	RequestBody() []byte

	SetResponseHeader(header string, value string)
	SetResponseContentType(contentType string)
	ResponseContentType() string
	ResponseHeaders() map[string]string
}

type ResponseValidator

type ResponseValidator interface {
	// ValidateRequest and return validation failures.
	// Errors returned are passed straight through to the ErrorHandler.
	// Returned status code is not used unless error is not nil.
	ValidateResponse(ctx context.Context, httpStatus int, responseBody []byte) (int, error)
}

type Unmarshaler

type Unmarshaler func(data []byte, v interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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