Documentation ¶
Overview ¶
Package middleware provides helpful functions that implement some common functionalities in http servers. A middleware is a func that returns a http.HandlerFunc
Index ¶
- Constants
- func All(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
- func BasicAuth(wrappedHandler http.HandlerFunc, user, passwd string) http.HandlerFunc
- func Cors(wrappedHandler http.HandlerFunc, allowedOrigins []string, ...) http.HandlerFunc
- func Csrf(wrappedHandler http.HandlerFunc, secretKey, domain string) http.HandlerFunc
- func Delete(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
- func Get(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
- func GetCspNonce(c context.Context) string
- func GetCsrfToken(c context.Context) string
- func Gzip(wrappedHandler http.HandlerFunc) http.HandlerFunc
- func Head(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
- func HttpsRedirector(wrappedHandler http.HandlerFunc, httpsPort uint16, domain string) http.HandlerFunc
- func LoadShedder(wrappedHandler http.HandlerFunc) http.HandlerFunc
- func Log(wrappedHandler http.HandlerFunc, domain string, l log.Logger) http.HandlerFunc
- func Panic(wrappedHandler http.HandlerFunc, l log.Logger) http.HandlerFunc
- func Post(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
- func Put(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
- func RateLimiter(wrappedHandler http.HandlerFunc) http.HandlerFunc
- func Security(wrappedHandler http.HandlerFunc, domain string) http.HandlerFunc
- type Opts
Constants ¶
const ( // CsrfTokenFormName is the name of the html form name attribute for csrf token. CsrfTokenFormName = "csrftoken" // named after what django uses. // CsrfHeader is the name of the http header that Ong uses to store csrf token. CsrfHeader = "X-Csrf-Token" // named after what fiber uses. )
Variables ¶
This section is empty.
Functions ¶
func All ¶
func All(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
All is a middleware that allows all http methods.
func BasicAuth ¶
func BasicAuth(wrappedHandler http.HandlerFunc, user, passwd string) http.HandlerFunc
BasicAuth is a middleware that protects wrappedHandler using basic authentication.
func Cors ¶
func Cors( wrappedHandler http.HandlerFunc, allowedOrigins []string, allowedMethods []string, allowedHeaders []string, ) http.HandlerFunc
Cors is a middleware to implement Cross-Origin Resource Sharing support. If allowedOrigins is nil, all origins are allowed. You can also use * to allow all. If allowedMethods is nil, "GET", "POST", "HEAD" are allowed. Use * to allow all. If allowedHeaders is nil, "Origin", "Accept", "Content-Type", "X-Requested-With" are allowed. Use * to allow all.
func Csrf ¶
func Csrf(wrappedHandler http.HandlerFunc, secretKey, domain string) http.HandlerFunc
Csrf is a middleware that provides protection against Cross Site Request Forgeries. If a csrf token is not provided(or is not valid), when it ought to have been; this middleware will issue a http GET redirect to the same url.
func Delete ¶
func Delete(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
Delete is a middleware that only allows http DELETE requests and http OPTIONS requests.
func Get ¶
func Get(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
Get is a middleware that only allows http GET requests and http OPTIONS requests.
func GetCspNonce ¶
GetCspNonce returns the Content-Security-Policy nonce that was set for that particular request.
example usage:
func myHandler(w http.ResponseWriter, r *http.Request) { cspNonce := middleware.GetCspNonce(r.Context()) _ = cspNonce }
func GetCsrfToken ¶
GetCsrfToken returns the csrf token was set for that particular request.
example usage:
func myHandler(w http.ResponseWriter, r *http.Request) { csrfToken := middleware.GetCsrfToken(r.Context()) _ = csrfToken }
func Gzip ¶
func Gzip(wrappedHandler http.HandlerFunc) http.HandlerFunc
Gzip is a middleware that transparently gzips the response body, for clients which support.
func Head ¶
func Head(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
Head is a middleware that only allows http HEAD requests and http OPTIONS requests.
func HttpsRedirector ¶
func HttpsRedirector(wrappedHandler http.HandlerFunc, httpsPort uint16, domain string) http.HandlerFunc
HttpsRedirector is a middleware that redirects http requests to https.
func LoadShedder ¶
func LoadShedder(wrappedHandler http.HandlerFunc) http.HandlerFunc
LoadShedder is a middleware that sheds load based on response latencies.
func Log ¶
func Log(wrappedHandler http.HandlerFunc, domain string, l log.Logger) http.HandlerFunc
Log is a middleware that logs requests/responses.
func Panic ¶
func Panic(wrappedHandler http.HandlerFunc, l log.Logger) http.HandlerFunc
Panic is a middleware that recovers from panics in wrappedHandler. It logs the stack trace and returns an InternalServerError response.
func Post ¶
func Post(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
Post is a middleware that only allows http POST requests and http OPTIONS requests.
func Put ¶
func Put(wrappedHandler http.HandlerFunc, o Opts) http.HandlerFunc
Put is a middleware that only allows http PUT requests and http OPTIONS requests.
func RateLimiter ¶
func RateLimiter(wrappedHandler http.HandlerFunc) http.HandlerFunc
RateLimiter is a middleware that limits requests by IP address.
func Security ¶
func Security(wrappedHandler http.HandlerFunc, domain string) http.HandlerFunc
Security is a middleware that adds some important HTTP security headers and assigns them sensible default values.
example usage:
middleware.Security(yourHandler(), "example.com")