middleware

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 22 Imported by: 0

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

View Source
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

func GetCspNonce(c context.Context) string

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

func GetCsrfToken(c context.Context) string

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(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")

Types

type Opts

type Opts struct {
	// contains filtered or unexported fields
}

func NewOpts

func NewOpts(
	domain string,
	httpsPort uint16,
	allowedOrigins []string,
	allowedMethods []string,
	allowedHeaders []string,
	secretKey string,
	l log.Logger,
) Opts

NewOpts returns a new opts.

func WithOpts

func WithOpts(domain string, httpsPort uint16, secretKey string, l log.Logger) Opts

WithOpts returns a new opts that has sensible defaults.

Jump to

Keyboard shortcuts

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