Documentation ¶
Index ¶
- Variables
- type APIKeyValidator
- type BasicAuth
- type CORS
- func (c *CORS) AllowCredentials(allow bool) *CORS
- func (c *CORS) AllowHeaders(headers ...string) *CORS
- func (c *CORS) AllowMethods(methods ...string) *CORS
- func (c *CORS) Default() *CORS
- func (c *CORS) ExposeHeaders(headers ...string) *CORS
- func (c *CORS) Handle(w http.ResponseWriter, r *vertex.Request, next vertex.HandlerFunc) (interface{}, error)
- type CacheMiddleware
- type ConnectionLimiter
- type ForceSecure
- type IPRangeFilter
Constants ¶
This section is empty.
Variables ¶
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
var DefaultMiddleware = []vertex.Middleware{ AutoRecover, RequestLogger, NewCORS().Default(), }
DefaultMiddleware is a quick set-up of the default middleware - logger, recover, CORS
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 ¶
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 (*CORS) AllowCredentials ¶
func (*CORS) AllowHeaders ¶
func (*CORS) AllowMethods ¶
func (*CORS) ExposeHeaders ¶
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