middleware

package
v0.0.0-...-d4aa6c0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2016 License: BSD-2-Clause Imports: 10 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AutoRecover = vertex.MiddlewareFunc(func(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (ret interface{}, err error) {

	defer func() {

		e := recover()
		if e != nil {
			logging.Critical("Caught panic: %v", e)

			err = vertex.NewErrorf("PANIC handling %s: %s", r.URL.Path, e)
			return
		}
	}()

	return next(w, r)

})

AutoRecover is a middleware that recovers automatically from panics inside request handlers

View Source
var DefaultMiddleware = []vertex.Middleware{
	AutoRecover,
	RequestLogger,
	NewCORS().Default(),
}

DefaultMiddleware is a quick set-up of the default middleware - logger, recover, CORS

View Source
var RequestLogger = vertex.MiddlewareFunc(func(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (interface{}, error) {

	logging.Info("Handling %s %s", r.Method, r.URL.String())

	ret, err := next(w, r)

	logging.Info("Return value was %v %v", ret, err)
	return ret, err
})

RequestLogger is a middleware that logs the paths and return values of all requests

Functions

This section is empty.

Types

type APIKeyValidator

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

APIKeyValidator is a simple request validator middleware that looks for an API key in the request form. If a key exists and it's in the list of allowed keys - the request is approved

func NewAPIKeyValidator

func NewAPIKeyValidator(paramName string, validKeys ...string) *APIKeyValidator

NewAPIKeyValidator creates a new validator middleware. paramName is the GET/POST parameter name we look for in requests. validKeys are a list of keys this filter will approve

func (*APIKeyValidator) Add

func (v *APIKeyValidator) Add(keys ...string)

Add a new key(s) to the validator

func (*APIKeyValidator) Handle

func (v *APIKeyValidator) Handle(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (interface{}, error)

type BasicAuth

type BasicAuth struct {
	User           string
	Password       string
	Realm          string
	BypassForLocal bool
}

BasicAuth is a middleware that forces basic auth user/pass authentication on requests.

When creating the auth middleware, give it a user/pass/realm config, and this is what it will validate

func (BasicAuth) Handle

func (b BasicAuth) Handle(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (interface{}, error)

type CORS

type CORS struct {
	AllowOrigin string
	// contains filtered or unexported fields
}

func NewCORS

func NewCORS() *CORS

func (*CORS) AllowCredentials

func (c *CORS) AllowCredentials(allow bool) *CORS

func (*CORS) AllowHeaders

func (c *CORS) AllowHeaders(headers ...string) *CORS

func (*CORS) AllowMethods

func (c *CORS) AllowMethods(methods ...string) *CORS

func (*CORS) Default

func (c *CORS) Default() *CORS

Default initializes a generic CORS configuration

func (*CORS) ExposeHeaders

func (c *CORS) ExposeHeaders(headers ...string) *CORS

func (*CORS) Handle

func (c *CORS) Handle(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (interface{}, error)

CORS is a middleware that injects Access-Control-Allow-Origin headers

type CacheMiddleware

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

CacheMiddleware is a middleware that caches responses for requests based on their url, method and params.

The cache uses an LRU cache with a given size, and tries to get/set resonses from and to it. The url of the request and an ancoded version of request.Form (GET + POST + path params) are used as the key. Headers do not play a part in the cache key.

Note: If the request contains a "Cache-Control: no-cache" header, the middleware will be bypassed

func NewCacheMiddleware

func NewCacheMiddleware(maxItems int, ttl time.Duration) *CacheMiddleware

NewCacheMiddleware creates a new Cache middleware

func (*CacheMiddleware) Handle

func (m *CacheMiddleware) Handle(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (interface{}, error)

type ConnectionLimiter

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

ConnectionLimiter limits the maximum allowed open connections (actually concurrent running requests) on an API.

If applied to the whole API, it limits the API as a whole. If an instance of the limiter is applied to a specific route, it limits the concurrent running requests of that route. A combination of the two can be applied - say 1000 concurrent requests on the whole API, and 100 concurrent on a specific route

func NewConnectionLimiter

func NewConnectionLimiter(max int32) *ConnectionLimiter

func (*ConnectionLimiter) Handle

func (b *ConnectionLimiter) Handle(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (interface{}, error)

type ForceSecure

type ForceSecure struct {
	AllowLocalInsecure bool
}

func (ForceSecure) Handle

func (f ForceSecure) Handle(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (interface{}, error)

ForceSecure validates that a request is sent over SSL regardless of the global API config

type IPRangeFilter

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

IPRangeFilter allows or denies ips based on a given set of IP ranges (CIDRs)

func NewIPRangeFilter

func NewIPRangeFilter(allowed ...string) *IPRangeFilter

NewIPRangeFilter creates a new filter with the given allowed CIDRs (e.g. 127.0.0.0/8 for local addresses)

func (*IPRangeFilter) Allow

func (f *IPRangeFilter) Allow(cidrs ...string) *IPRangeFilter

Allow allows traffic from the given allowed CIDRs

func (*IPRangeFilter) AllowPrivate

func (f *IPRangeFilter) AllowPrivate() *IPRangeFilter

AlloPrivate allows IP ranges from all private ranges according to RFC 1918

func (*IPRangeFilter) Deny

func (f *IPRangeFilter) Deny(cidrs ...string) *IPRangeFilter

Deny denies traffic from the given CIDRs (e.g. 127.0.0.0/8 for local addresses)

func (*IPRangeFilter) Handle

func (f *IPRangeFilter) Handle(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (interface{}, error)

Handle checks the current requests IP against the allowed and blocked IP ranges in the filter

Directories

Path Synopsis
This middleware package for OAauth is incomplete (although it's working).
This middleware package for OAauth is incomplete (although it's working).

Jump to

Keyboard shortcuts

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