http

package
v0.57.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2021 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Overview

Package http provides a HTTP component with included observability.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultAliveCheck return always live.
	DefaultAliveCheck = func() AliveStatus { return Alive }
	// DefaultReadyCheck return always ready.
	DefaultReadyCheck = func() ReadyStatus { return Ready }
)

Functions

func ExtractParams added in v0.42.0

func ExtractParams(r *http.Request) map[string]string

ExtractParams extracts dynamic URL parameters using httprouter's functionality

func MiddlewareChain

func MiddlewareChain(f http.Handler, mm ...MiddlewareFunc) http.Handler

MiddlewareChain chains middlewares to a handler func.

Types

type AliveCheckFunc

type AliveCheckFunc func() AliveStatus

AliveCheckFunc defines a function type for implementing a liveness check.

type AliveStatus

type AliveStatus int

AliveStatus type representing the liveness of the service via HTTP component.

const (
	// Alive represents a state defining a Alive state.
	Alive AliveStatus = 1
	// Unresponsive represents a state defining a Unresponsive state.
	Unresponsive AliveStatus = 2
)

type Builder

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

Builder gathers all required and optional properties, in order to construct an HTTP component.

func NewBuilder

func NewBuilder() *Builder

NewBuilder initiates the HTTP component builder chain. The builder instantiates the component using default values for HTTP Port, Alive/Ready check functions and Read/Write timeouts.

func (*Builder) Create

func (cb *Builder) Create() (*Component, error)

Create constructs the HTTP component by applying the gathered properties.

func (*Builder) WithAliveCheckFunc

func (cb *Builder) WithAliveCheckFunc(acf AliveCheckFunc) *Builder

WithAliveCheckFunc sets the AliveCheckFunc used by the HTTP component.

func (*Builder) WithDeflateLevel added in v0.49.0

func (cb *Builder) WithDeflateLevel(level int) *Builder

WithDeflateLevel sets the level of compression for Deflate; based on https://golang.org/pkg/compress/flate/ Levels range from 1 (BestSpeed) to 9 (BestCompression); higher levels typically run slower but compress more. Level 0 (NoCompression) does not attempt any compression; it only adds the necessary DEFLATE framing. Level -1 (DefaultCompression) uses the default compression level. Level -2 (HuffmanOnly) will use Huffman compression only, giving a very fast compression for all types of input, but sacrificing considerable compression efficiency.

func (*Builder) WithMiddlewares

func (cb *Builder) WithMiddlewares(mm ...MiddlewareFunc) *Builder

WithMiddlewares adds middlewares to the HTTP component.

func (*Builder) WithPort

func (cb *Builder) WithPort(p int) *Builder

WithPort sets the port used by the HTTP component.

func (*Builder) WithReadTimeout

func (cb *Builder) WithReadTimeout(rt time.Duration) *Builder

WithReadTimeout sets the Read Timeout for the HTTP component.

func (*Builder) WithReadyCheckFunc

func (cb *Builder) WithReadyCheckFunc(rcf ReadyCheckFunc) *Builder

WithReadyCheckFunc sets the ReadyCheckFunc used by the HTTP component.

func (*Builder) WithRoutesBuilder

func (cb *Builder) WithRoutesBuilder(rb *RoutesBuilder) *Builder

WithRoutesBuilder adds routes builder to the HTTP component.

func (*Builder) WithSSL

func (cb *Builder) WithSSL(c, k string) *Builder

WithSSL sets the filenames for the Certificate and Keyfile, in order to enable SSL.

func (*Builder) WithShutdownGracePeriod added in v0.43.1

func (cb *Builder) WithShutdownGracePeriod(gp time.Duration) *Builder

WithShutdownGracePeriod sets the Shutdown Grace Period for the HTTP component.

func (*Builder) WithUncompressedPaths added in v0.49.0

func (cb *Builder) WithUncompressedPaths(r ...string) *Builder

WithUncompressedPaths specifies which routes should be excluded from compression Any trailing slashes are trimmed, so we match both /metrics/ and /metrics?seconds=30

func (*Builder) WithWriteTimeout

func (cb *Builder) WithWriteTimeout(wt time.Duration) *Builder

WithWriteTimeout sets the Write Timeout for the HTTP component.

type Component

type Component struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Component implementation of HTTP.

func (*Component) Run

func (c *Component) Run(ctx context.Context) error

Run starts the HTTP server.

type Error

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

Error defines an abstract struct that can represent several types of HTTP errors.

func NewError

func NewError() *Error

NewError creates a new error with default Internal Server Error payload.

func NewErrorWithCodeAndPayload

func NewErrorWithCodeAndPayload(code int, payload interface{}) *Error

NewErrorWithCodeAndPayload creates a fully customizable error with the specified status code and payload.

func NewForbiddenError

func NewForbiddenError() *Error

NewForbiddenError creates a new forbidden error with default payload.

func NewForbiddenErrorWithPayload

func NewForbiddenErrorWithPayload(payload interface{}) *Error

NewForbiddenErrorWithPayload creates a new forbidden error with the specified payload.

func NewNotFoundError

func NewNotFoundError() *Error

NewNotFoundError creates a new not found error with default payload.

func NewNotFoundErrorWithPayload

func NewNotFoundErrorWithPayload(payload interface{}) *Error

NewNotFoundErrorWithPayload creates a new not found error with the specified payload.

func NewServiceUnavailableError

func NewServiceUnavailableError() *Error

NewServiceUnavailableError creates a new service unavailable error with default payload.

func NewServiceUnavailableErrorWithPayload

func NewServiceUnavailableErrorWithPayload(payload interface{}) *Error

NewServiceUnavailableErrorWithPayload creates a new service unavailable error with the specified payload.

func NewUnauthorizedError

func NewUnauthorizedError() *Error

NewUnauthorizedError creates a new validation error with default payload.

func NewUnauthorizedErrorWithPayload

func NewUnauthorizedErrorWithPayload(payload interface{}) *Error

NewUnauthorizedErrorWithPayload creates a new unauthorized error with the specified payload.

func NewValidationError

func NewValidationError() *Error

NewValidationError creates a new validation error with default payload.

func NewValidationErrorWithPayload

func NewValidationErrorWithPayload(payload interface{}) *Error

NewValidationErrorWithPayload creates a new validation error with the specified payload.

func (*Error) Error

func (e *Error) Error() string

Error returns the actual message of the error.

func (*Error) WithHeaders added in v0.56.0

func (e *Error) WithHeaders(headers map[string]string) *Error

WithHeaders adds headers to the error which will be added to the http response.

type Header map[string]string

Header is the http header representation as a map of strings

type MiddlewareFunc

type MiddlewareFunc func(next http.Handler) http.Handler

MiddlewareFunc type declaration of middleware func.

func NewAuthMiddleware

func NewAuthMiddleware(auth auth.Authenticator) MiddlewareFunc

NewAuthMiddleware creates a MiddlewareFunc that implements authentication using an Authenticator.

func NewCachingMiddleware added in v0.43.0

func NewCachingMiddleware(rc *cache.RouteCache) MiddlewareFunc

NewCachingMiddleware creates a cache layer as a middleware when used as part of a middleware chain any middleware later in the chain, will not be executed, but the headers it appends will be part of the cache

func NewCompressionMiddleware added in v0.49.0

func NewCompressionMiddleware(deflateLevel int, ignoreRoutes ...string) MiddlewareFunc

NewCompressionMiddleware initializes a compression middleware. As per Section 3.5 of the HTTP/1.1 RFC, GZIP and Deflate compression methods are supported. https://tools.ietf.org/html/rfc2616#section-14.3

func NewLoggingTracingMiddleware

func NewLoggingTracingMiddleware(path string, statusCodeLogger statusCodeLoggerHandler) MiddlewareFunc

NewLoggingTracingMiddleware creates a MiddlewareFunc that continues a tracing span and finishes it. It also logs the HTTP request on debug logging level

func NewRateLimitingMiddleware added in v0.51.0

func NewRateLimitingMiddleware(limiter *rate.Limiter) MiddlewareFunc

NewRateLimitingMiddleware creates a MiddlewareFunc that adds a rate limit to a route.

func NewRecoveryMiddleware

func NewRecoveryMiddleware() MiddlewareFunc

NewRecoveryMiddleware creates a MiddlewareFunc that ensures recovery and no panic.

type ProcessorFunc

type ProcessorFunc func(context.Context, *Request) (*Response, error)

ProcessorFunc definition of a function type for processing sync requests.

type ReadyCheckFunc

type ReadyCheckFunc func() ReadyStatus

ReadyCheckFunc defines a function type for implementing a readiness check.

type ReadyStatus

type ReadyStatus int

ReadyStatus type.

const (
	// Ready represents a state defining a Ready state.
	Ready ReadyStatus = 1
	// NotReady represents a state defining a NotReady state.
	NotReady ReadyStatus = 2
)

type Request

type Request struct {
	Fields  map[string]string
	Raw     io.Reader
	Headers Header
	// contains filtered or unexported fields
}

Request definition of the sync request model.

func NewRequest

func NewRequest(f map[string]string, r io.Reader, h map[string]string, d encoding.DecodeFunc) *Request

NewRequest creates a new request.

func (*Request) Decode

func (r *Request) Decode(v interface{}) error

Decode the raw data by using the provided decoder.

type Response

type Response struct {
	Payload interface{}
	Header  Header
}

Response definition of the sync Response model.

func NewResponse

func NewResponse(p interface{}) *Response

NewResponse creates a new Response.

type Route

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

Route definition of a HTTP route.

func (Route) Handler added in v0.40.1

func (r Route) Handler() http.HandlerFunc

Handler returns route handler function.

func (Route) Method added in v0.40.1

func (r Route) Method() string

Method returns route method value (GET/POST/...).

func (Route) Middlewares added in v0.40.1

func (r Route) Middlewares() []MiddlewareFunc

Middlewares returns route middlewares.

func (Route) Path added in v0.40.1

func (r Route) Path() string

Path returns route path value.

type RouteBuilder

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

RouteBuilder for building a route.

func NewConnectRouteBuilder added in v0.47.0

func NewConnectRouteBuilder(path string, processor ProcessorFunc) *RouteBuilder

NewConnectRouteBuilder constructor.

func NewDeleteRouteBuilder added in v0.47.0

func NewDeleteRouteBuilder(path string, processor ProcessorFunc) *RouteBuilder

NewDeleteRouteBuilder constructor.

func NewFileServer added in v0.54.0

func NewFileServer(path string, assetsDir string, fallbackPath string) *RouteBuilder

NewFileServer constructor.

func NewGetRouteBuilder added in v0.47.0

func NewGetRouteBuilder(path string, processor ProcessorFunc) *RouteBuilder

NewGetRouteBuilder constructor

func NewHeadRouteBuilder added in v0.47.0

func NewHeadRouteBuilder(path string, processor ProcessorFunc) *RouteBuilder

NewHeadRouteBuilder constructor.

func NewOptionsRouteBuilder added in v0.47.0

func NewOptionsRouteBuilder(path string, processor ProcessorFunc) *RouteBuilder

NewOptionsRouteBuilder constructor.

func NewPatchRouteBuilder added in v0.47.0

func NewPatchRouteBuilder(path string, processor ProcessorFunc) *RouteBuilder

NewPatchRouteBuilder constructor.

func NewPostRouteBuilder added in v0.47.0

func NewPostRouteBuilder(path string, processor ProcessorFunc) *RouteBuilder

NewPostRouteBuilder constructor.

func NewPutRouteBuilder added in v0.47.0

func NewPutRouteBuilder(path string, processor ProcessorFunc) *RouteBuilder

NewPutRouteBuilder constructor.

func NewRawRouteBuilder

func NewRawRouteBuilder(path string, handler http.HandlerFunc) *RouteBuilder

NewRawRouteBuilder constructor.

func NewRouteBuilder

func NewRouteBuilder(path string, processor ProcessorFunc) *RouteBuilder

NewRouteBuilder constructor.

func NewTraceRouteBuilder added in v0.47.0

func NewTraceRouteBuilder(path string, processor ProcessorFunc) *RouteBuilder

NewTraceRouteBuilder constructor.

func (*RouteBuilder) Build

func (rb *RouteBuilder) Build() (Route, error)

Build a route.

func (*RouteBuilder) MethodConnect

func (rb *RouteBuilder) MethodConnect() *RouteBuilder

MethodConnect HTTP method.

func (*RouteBuilder) MethodDelete

func (rb *RouteBuilder) MethodDelete() *RouteBuilder

MethodDelete HTTP method.

func (*RouteBuilder) MethodGet

func (rb *RouteBuilder) MethodGet() *RouteBuilder

MethodGet HTTP method.

func (*RouteBuilder) MethodHead

func (rb *RouteBuilder) MethodHead() *RouteBuilder

MethodHead HTTP method.

func (*RouteBuilder) MethodOptions

func (rb *RouteBuilder) MethodOptions() *RouteBuilder

MethodOptions HTTP method.

func (*RouteBuilder) MethodPatch

func (rb *RouteBuilder) MethodPatch() *RouteBuilder

MethodPatch HTTP method.

func (*RouteBuilder) MethodPost

func (rb *RouteBuilder) MethodPost() *RouteBuilder

MethodPost HTTP method.

func (*RouteBuilder) MethodPut

func (rb *RouteBuilder) MethodPut() *RouteBuilder

MethodPut HTTP method.

func (*RouteBuilder) MethodTrace

func (rb *RouteBuilder) MethodTrace() *RouteBuilder

MethodTrace HTTP method.

func (*RouteBuilder) WithAuth

func (rb *RouteBuilder) WithAuth(auth auth.Authenticator) *RouteBuilder

WithAuth adds authenticator.

func (*RouteBuilder) WithMiddlewares

func (rb *RouteBuilder) WithMiddlewares(mm ...MiddlewareFunc) *RouteBuilder

WithMiddlewares adds middlewares.

func (*RouteBuilder) WithRateLimiting added in v0.51.0

func (rb *RouteBuilder) WithRateLimiting(limit float64, burst int) *RouteBuilder

WithRateLimiting enables route rate limiting.

func (*RouteBuilder) WithRouteCache added in v0.43.0

func (rb *RouteBuilder) WithRouteCache(cache cache.TTLCache, ageBounds httpcache.Age) *RouteBuilder

WithRouteCache adds a cache to the corresponding route

func (*RouteBuilder) WithTrace

func (rb *RouteBuilder) WithTrace() *RouteBuilder

WithTrace enables route tracing.

type RoutesBuilder

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

RoutesBuilder creates a list of routes.

func NewRoutesBuilder

func NewRoutesBuilder() *RoutesBuilder

NewRoutesBuilder constructor.

func (*RoutesBuilder) Append

func (rb *RoutesBuilder) Append(builder *RouteBuilder) *RoutesBuilder

Append a route to the list.

func (*RoutesBuilder) Build

func (rb *RoutesBuilder) Build() ([]Route, error)

Build the routes.

Directories

Path Synopsis
Package auth provides abstractions for concrete implementations.
Package auth provides abstractions for concrete implementations.
apikey
Package apikey is a concrete implementation of the auth abstractions.
Package apikey is a concrete implementation of the auth abstractions.
Package cache provides a cache control and implementation components for http routes.
Package cache provides a cache control and implementation components for http routes.

Jump to

Keyboard shortcuts

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