middleware

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var RequestIDHeader = "X-Request-ID"

Functions

func GetRequestID

func GetRequestID(ctx context.Context) string

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

func Heartbeat(endpoint string) func(http.Handler) http.Handler

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

func Logger(next http.Handler) http.Handler

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

func NoCache(next http.Handler) http.Handler

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

func Recoverer(next http.Handler) http.Handler

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

func RequestID(next http.Handler) http.Handler

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

func Timeout(timeout time.Duration) func(next http.Handler) http.Handler

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.

Jump to

Keyboard shortcuts

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