Documentation
¶
Overview ¶
The middleware package contains some essential middleware for use with Jett! https://github.com/saurabh0719/jett
Read https://github.com/saurabh0719/jett#middleware for more information
Index ¶
- Variables
- func BasicAuth(realm string, credentials map[string]string) func(next http.Handler) http.Handler
- func GetRequestID(ctx context.Context) string
- func Heartbeat(endpoint string) func(http.Handler) http.Handler
- func Logger(next http.Handler) http.Handler
- func NoCache(next http.Handler) http.Handler
- func Recoverer(next http.Handler) http.Handler
- func RequestID(next http.Handler) http.Handler
- func Timeout(timeout time.Duration) func(next http.Handler) http.Handler
Constants ¶
This section is empty.
Variables ¶
var RequestIDHeader = "X-Request-ID"
Functions ¶
func BasicAuth ¶
BasicAuth middleware - Implements middleware handler.
RFC 2617, Section 2. (https://www.rfc-editor.org/rfc/rfc2617.html#section-2)
Ref - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate
func GetRequestID ¶
GetReqID returns a request ID from the given context if one is present. Returns the empty string if a request ID cannot be found.
func Heartbeat ¶ added in v0.3.0
Heartbeat endpoint middleware useful to setting up a path like `/ping` that load balancers or uptime testing external services can make a request before hitting any routes. It's also convenient to place this above ACL middlewares as well.
func Logger ¶
A basic logger for Jett Logs
- RequestID (if available from RequestID middleware)
- Method and Path
- status code of response
- Duration of the request-response cycle
func NoCache ¶
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 Recoverer ¶
Simple recoverer middleware to recover from panics and print the debug stack. Also sets status 500 to the ResponseWriter so no more writes take place
func RequestID ¶
RequestID is a middleware that injects a request ID into the context of each request. A request ID is a string of the form "host.example.com/random-0001", where "random" is a base62 random string that uniquely identifies this go process, and where the last number is an atomically incremented request counter.
func Timeout ¶ added in v0.3.0
Timeout is a middleware that cancels context after a given timeout and returns a 504 Gateway Timeout error to the client.
It's required that you select the ctx.Done() channel to check for the signal if the context has reached its deadline and return, otherwise the timeout signal will be just ignored.
Example route may look like:
r.GET("/task", func(w http.ResponseWriter, req *http.Request) { ctx := r.Context() processTime := time.Duration(rand.Intn(4)+1) * time.Second select { case <-ctx.Done(): return case <-time.After(processTime): // The above channel simulates some hard work. } jett.JSON(w, "Done!", 200) })
Types ¶
This section is empty.