middlewares

package
v2.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AllowAllOrigins           string = "*"
	AllowAllMethods           string = "POST, GET, OPTIONS, PUT, DELETE"
	AllowAllHeaders           string = "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization"
	AllowHeaderAccept         string = "Accept"
	AllowHeaderContentType    string = "Content-Type"
	AllowHeaderContentLength  string = "Content-Length"
	AllowHeaderAcceptEncoding string = "Accept-Encoding"
	AllowHeaderCSRF           string = "X-CSRF-Token"
	AllowHeaderAuthorization  string = "Authorization"
)

Variables

This section is empty.

Functions

func AccessControl

func AccessControl(allowOrigin, allowMethods, allowHeaders string) mux.MiddlewareFunc

AccessControl wraps an HTTP mux with a middleware that sets headers for access control and allowed headers.

Example:

mux := nerdweb.NewServeMux()
mux.HandleFunc("/endpoint", handler)

mux.Use(middlewares.AccessControl(middlewares.AllowAllOrigins, middlewares.AllowAllMethods, middlewares.AllowAllHeaders)

func Allow

func Allow(next http.HandlerFunc, allowedMethod string) http.HandlerFunc

Allow verifies if the caller method matches the provided method.

Example:

mux := nerdweb.NewServeMux()
mux.HandleFunc("/endpoint", middlewares.Allow(myHandler, http.MethodPost))

If the caller's method does not match what is allowed, the string "method not allowed" is written back to the caller.

func CaptureAuth

func CaptureAuth(next http.HandlerFunc, logger *logrus.Entry, onInvalidHeader func(logger *logrus.Entry, w http.ResponseWriter)) http.HandlerFunc

CaptureAuth captures an authorization token from an Authorization header and stored it in a context variable named "authtoken". This middleware expect the header to be in the format of:

Authorization: Bearer <token here>

If the header format is invalid, the provided error method is called. Here is an example:

onInvalidHeader = func(logger *logrus.Entry, w http.ResponseWriter) {
  result := map[string]string{
    "error": "invalid JWT header!",
  }

  nerdweb.WriteJSON(logger, w, http.StatusBadRequest, result)
}

// Now, in your handler definition
http.HandleFunc("/endpoint", middlewares.CaptureAuth(handlerFunc, logger, onInvalidHeader))

func CaptureIP

func CaptureIP() mux.MiddlewareFunc

CaptureIP captures the caller's IP address and puts it into the context as "ip". Example:

mux := nerdweb.NewServeMux()
mux.HandleFunc("/endpoint", handler)

mux.Use(middlewares.CaptureIP())

func RequestLogger

func RequestLogger(logger *logrus.Entry) mux.MiddlewareFunc

RequestLogger returns a middleware for logging all requests.

Example:

mux := nerdweb.NewServeMux()
mux.HandleFunc("/endpoint", handler)

mux.Use(middlewares.RequestLogger(logger))

Types

This section is empty.

Jump to

Keyboard shortcuts

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