Documentation ¶
Index ¶
- Constants
- func AccessControl(allowOrigin, allowMethods, allowHeaders string) mux.MiddlewareFunc
- func Allow(next http.HandlerFunc, allowedMethod string) http.HandlerFunc
- func CaptureAuth(next http.HandlerFunc, logger *logrus.Entry, ...) http.HandlerFunc
- func CaptureIP() mux.MiddlewareFunc
- func RequestLogger(logger *logrus.Entry) mux.MiddlewareFunc
Constants ¶
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.