go-api-boilerplate: github.com/vardius/go-api-boilerplate/pkg/http/middleware Index | Examples | Files | Directories

package middleware

import "github.com/vardius/go-api-boilerplate/pkg/http/middleware"

Package middleware provides http middleware

Index

Examples

Package Files

container.go doc.go firewall.go hsts.go logger.go metadata.go metrics.go nosniff.go rate_limiter.go recovery.go request_body.go

func GrantAccessFor Uses

func GrantAccessFor(role identity.Role) func(next http.Handler) http.Handler

GrantAccessFor returns Status Unauthorized if Identity is not set within request's context or user does not have required role

func HSTS Uses

func HSTS() gorouter.MiddlewareFunc

HSTS HTTP Strict Transport Security is an opt-in security enhancement that is specified by a web application through the use of a special response header

Code:

m := middleware.HSTS()
h := m(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)

h.ServeHTTP(w, req)

fmt.Printf("%s\n", w.Header().Get("Strict-Transport-Security"))

Output:

max-age=63072000; includeSubDomains

func LimitRequestBody Uses

func LimitRequestBody(n int64) gorouter.MiddlewareFunc

LimitRequestBody limits the request body

func Logger Uses

func Logger(logger *log.Logger) gorouter.MiddlewareFunc

Logger wraps http.Handler with a logger middleware

Code:

m := middleware.Logger(log.New("development"))
h := m(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)

h.ServeHTTP(w, req)

func Metrics Uses

func Metrics() gorouter.MiddlewareFunc

Metrics updates program counters.

func RateLimit Uses

func RateLimit(logger *log.Logger, r rate.Limit, b int, frequency time.Duration) gorouter.MiddlewareFunc

RateLimit returns a new HTTP middleware that allows request per visitor (IP) up to rate r and permits bursts of at most b tokens.

func Recover Uses

func Recover(logger *log.Logger) gorouter.MiddlewareFunc

Recover middleware recovers from panic

Code:

m := middleware.Recover(log.New("development"))
handler := m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    panic("error")
}))

// We will mock request for this example
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)

handler.ServeHTTP(w, req)

func WithContainer Uses

func WithContainer(requestContainer gocontainer.Container) gorouter.MiddlewareFunc

WithContainer wraps http.Handler with a container middleware

func WithMetadata Uses

func WithMetadata() gorouter.MiddlewareFunc

WithMetadata adds Metadata to requests context

func XSS Uses

func XSS() gorouter.MiddlewareFunc

XSS sets xss response header types

Code:

m := middleware.XSS()
h := m(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)

h.ServeHTTP(w, req)

fmt.Printf("%s\n", w.Header().Get("X-Content-Type-Options"))
fmt.Printf("%s\n", w.Header().Get("X-Frame-Options"))

Output:

nosniff
DENY

Directories

PathSynopsis
authenticatorPackage authenticator provides allows to authorize request

Package middleware imports 19 packages (graph) and is imported by 2 packages. Updated 2020-08-04. Refresh now. Tools for package owners.