Documentation ¶
Index ¶
- Constants
- Variables
- func BasicAuth(realm string, creds map[string]string) func(next fchi.Handler) fchi.Handler
- func GetReqID(ctx context.Context) string
- func NextRequestID() uint64
- func NoCache(h fchi.Handler) fchi.Handler
- func PrintPrettyStack(rvr interface{})
- func Profiler() fchi.Handler
- func RealIP(next fchi.Handler) fchi.Handler
- func Recoverer(next fchi.Handler) fchi.Handler
- func RedirectSlashes(next fchi.Handler) fchi.Handler
- func RequestID(next fchi.Handler) fchi.Handler
- func StripSlashes(next fchi.Handler) fchi.Handler
- func Throttle(limit int) func(fchi.Handler) fchi.Handler
- func ThrottleBacklog(limit int, backlogLimit int, backlogTimeout time.Duration) func(fchi.Handler) fchi.Handler
- func ThrottleWithOpts(opts ThrottleOpts) func(fchi.Handler) fchi.Handler
- func Timeout(timeout time.Duration) func(next fchi.Handler) fchi.Handler
- type ThrottleOpts
Constants ¶
const RequestIDKey ctxKeyRequestID = 0
RequestIDKey is the key that holds the unique request ID in a request context.
Variables ¶
var IsTTY bool
var RequestIDHeader = "X-Request-Id"
RequestIDHeader is the name of the HTTP Header which contains the request id. Exported so that it can be changed by developers
Functions ¶
func BasicAuth ¶ added in v1.0.1
BasicAuth implements a simple middleware handler for adding basic http auth to a route.
func GetReqID ¶
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 NextRequestID ¶ added in v1.0.1
func NextRequestID() uint64
NextRequestID generates the next request ID in the sequence.
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 PrintPrettyStack ¶ added in v1.0.1
func PrintPrettyStack(rvr interface{})
func Profiler ¶
Profiler is a convenient subrouter used for mounting net/http/pprof. ie.
func MyService() http.Handler { r := chi.NewRouter() // ..middlewares r.Mount("/debug", middleware.Profiler()) // ..routes return r }
func RealIP ¶
RealIP is a middleware that sets a http.Request's RemoteAddr to the results of parsing either the True-Client-IP, X-Real-IP or the X-Forwarded-For headers (in that order).
This middleware should be inserted fairly early in the middleware stack to ensure that subsequent layers (e.g., request loggers) which examine the RemoteAddr will see the intended value.
You should only use this middleware if you can trust the headers passed to you (in particular, the two headers this middleware uses), for example because you have placed a reverse proxy like HAProxy or nginx in front of chi. If your reverse proxies are configured to pass along arbitrary header values from the client, or if you use this middleware without a reverse proxy, malicious clients will be able to make you very sad (or, depending on how you're using RemoteAddr, vulnerable to an attack of some sort).
func Recoverer ¶
Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recoverer prints a request ID if one is provided.
Alternatively, look at https://github.com/go-chi/httplog middleware pkgs.
func RedirectSlashes ¶
RedirectSlashes is a middleware that will match request paths with a trailing slash and redirect to the same path, less the trailing slash.
NOTE: RedirectSlashes middleware is *incompatible* with http.FileServer, see https://github.com/go-chi/chi/issues/343
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 StripSlashes ¶
StripSlashes is a middleware that will match request paths with a trailing slash, strip it from the path and continue routing through the mux, if a route matches, then it will serve the handler.
func Throttle ¶
Throttle is a middleware that limits number of currently processed requests at a time across all users. Note: Throttle is not a rate-limiter per user, instead it just puts a ceiling on the number of currentl in-flight requests being processed from the point from where the Throttle middleware is mounted.
func ThrottleBacklog ¶
func ThrottleBacklog(limit int, backlogLimit int, backlogTimeout time.Duration) func(fchi.Handler) fchi.Handler
ThrottleBacklog is a middleware that limits number of currently processed requests at a time and provides a backlog for holding a finite number of pending requests.
func ThrottleWithOpts ¶ added in v1.0.1
func ThrottleWithOpts(opts ThrottleOpts) func(fchi.Handler) fchi.Handler
ThrottleWithOpts is a middleware that limits number of currently processed requests using passed ThrottleOpts.
func Timeout ¶
Timeout is a middleware that cancels ctx after a given timeout and return 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.
ie. a route/handler may look like:
r.Get("/long", func(w http.ResponseWriter, r *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. } w.Write([]byte("done")) })