Documentation ¶
Overview ¶
Package rest provides common middlewares and helpers for rest services
Index ¶
- func AppInfo(app, author, version string) func(http.Handler) http.Handler
- func BasicAuth(checker func(user, passwd string) bool) func(http.Handler) http.Handler
- func BlackWords(words ...string) func(http.Handler) http.Handler
- func BlackWordsFn(fn func() []string) func(http.Handler) http.Handler
- func CacheControl(expiration time.Duration, version string) func(http.Handler) http.Handler
- func CacheControlDynamic(expiration time.Duration, versionFn func(r *http.Request) string) func(http.Handler) http.Handler
- func Deprecation(version string, date time.Time) func(http.Handler) http.Handler
- func FileServer(public, local string) (http.Handler, error)
- func GetTraceID(r *http.Request) string
- func Gzip(contentTypes ...string) func(http.Handler) http.Handler
- func Headers(headers ...string) func(http.Handler) http.Handler
- func Metrics(onlyIps ...string) func(http.Handler) http.Handler
- func NoCache(h http.Handler) http.Handler
- func OnlyFrom(onlyIps ...string) func(http.Handler) http.Handler
- func Ping(next http.Handler) http.Handler
- func Recoverer(l logger.Backend) func(http.Handler) http.Handler
- func RenderJSON(w http.ResponseWriter, data interface{})
- func RenderJSONFromBytes(w http.ResponseWriter, r *http.Request, data []byte) error
- func RenderJSONWithHTML(w http.ResponseWriter, r *http.Request, v interface{}) error
- func Rewrite(from, to string) func(http.Handler) http.Handler
- func SendErrorJSON(w http.ResponseWriter, r *http.Request, l logger.Backend, code int, err error, ...)
- func SizeLimit(size int64) func(http.Handler) http.Handler
- func Trace(next http.Handler) http.Handler
- func Wrap(handler http.Handler, mws ...func(http.Handler) http.Handler) http.Handler
- type ErrorLogger
- type JSON
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BasicAuth ¶ added in v1.5.4
BasicAuth middleware requires basic auth and matches user & passwd with client-provided checker
func BlackWords ¶ added in v1.1.0
BlackWords middleware doesn't allow some words in the request body
func BlackWordsFn ¶ added in v1.3.0
BlackWordsFn middleware uses func to get the list and doesn't allow some words in the request body
func CacheControl ¶ added in v1.5.4
CacheControl is a middleware setting cache expiration. Using url+version for etag
func CacheControlDynamic ¶ added in v1.5.4
func CacheControlDynamic(expiration time.Duration, versionFn func(r *http.Request) string) func(http.Handler) http.Handler
CacheControlDynamic is a middleware setting cache expiration. Using url+ func(r) for etag
func Deprecation ¶ added in v1.5.4
Deprecation adds a header 'Deprecation: version="version", date="date" header' see https://tools.ietf.org/id/draft-dalal-deprecation-header-00.html
func FileServer ¶ added in v1.5.4
FileServer returns http.FileServer handler to serve static files from a http.FileSystem, prevents directory listing. - public defines base path of the url, i.e. for http://example.com/static/* it should be /static - local for the local path to the root of the served directory
func GetTraceID ¶ added in v1.3.0
GetTraceID returns request id from the context
func NoCache ¶ added in v1.5.4
NoCache is a simple piece of middleware that sets a number of HTTP headers to prevent a router (or subrouter) from being cached by an upstream proxy and/or client.
As per http://wiki.nginx.org/HttpProxyModule - NoCache sets:
Expires: Thu, 01 Jan 1970 00:00:00 UTC Cache-Control: no-cache, private, max-age=0 X-Accel-Expires: 0 Pragma: no-cache (for HTTP/1.0 proxies/clients)
func OnlyFrom ¶ added in v1.1.0
OnlyFrom middleware allows access for limited list of source IPs. Such IPs can be defined as complete ip (like 192.168.1.12), prefix (129.168.) or CIDR (192.168.0.0/16)
func Recoverer ¶
Recoverer is a middleware that recovers from panics, logs the panic and returns a HTTP 500 status if possible.
func RenderJSON ¶ added in v1.1.0
func RenderJSON(w http.ResponseWriter, data interface{})
RenderJSON sends data as json
func RenderJSONFromBytes ¶
RenderJSONFromBytes sends binary data as json
func RenderJSONWithHTML ¶
func RenderJSONWithHTML(w http.ResponseWriter, r *http.Request, v interface{}) error
RenderJSONWithHTML allows html tags and forces charset=utf-8
func Rewrite ¶ added in v1.5.4
Rewrite middleware with from->to rule. Supports regex (like nginx) and prevents multiple rewrites example: Rewrite(`^/sites/(.*)/settings/$`, `/sites/settings/$1`
func SendErrorJSON ¶
func SendErrorJSON(w http.ResponseWriter, r *http.Request, l logger.Backend, code int, err error, msg string)
SendErrorJSON sends {error: msg} with error code and logging error and caller
func SizeLimit ¶ added in v1.4.0
SizeLimit middleware checks if body size is above the limit and returns StatusRequestEntityTooLarge (413)
Types ¶
type ErrorLogger ¶ added in v1.5.0
type ErrorLogger struct {
// contains filtered or unexported fields
}
ErrorLogger wraps logger.Backend
func NewErrorLogger ¶ added in v1.5.0
func NewErrorLogger(l logger.Backend) *ErrorLogger
NewErrorLogger creates ErrorLogger for given Backend
func (*ErrorLogger) Log ¶ added in v1.5.0
func (e *ErrorLogger) Log(w http.ResponseWriter, r *http.Request, httpCode int, err error, msg ...string)
Log sends json error message {error: msg} with error code and logging error and caller