adapter

package module
v0.0.0-...-190d8ba Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2021 License: MIT Imports: 3 Imported by: 0

README

Gin Middleware Adapter

GoDoc Build Status

This package provides a simple adapter to make it easier to use common Go HTTP middleware packages with the Gin Web Framework.

For example, if you have some middleware that uses the common pattern of having a function that takes an http.Handler to wrap and returning another http.Handler, you can use adapter.Wrap.

Using the nosurf CSRF middleware:

engine.Use(adapter.Wrap(nosurf.NewPure)) 

If you need to pass in the wrapped handler explictly (eg. via configuration), or use a different signature, you can use adapter.New, which returns a handler that can be passed to the middleware, and a function that wraps the handler the middleware returns into a gin.HandlerFunc:

nextHandler, wrapper := adapter.New()
ns := nosurf.New(nextHandler)
ns.SetFailureHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    http.Error(w, "Custom error about invalid CSRF tokens", http.StatusBadRequest)
}))
engine.Use(wrapper(ns)) 

The wrapper will ensure that the request's context is correctly passed down the stack and that the next middleware in the chain is called.

It will additionally call the Abort method on Gin's context if the middleware does not call the wrapped handler.

Documentation

Overview

Package adapter provides a method of connecting standard Go middleware handlers to Gin's Engine.

The adapter uses the request context to pass Gin's context through the wrapped handler and ensure the next middlware on the chain is called correctly.

Example of using it with the nosurf CSRF package

engine.Use(adapter.Wrap(nosurf.NewPure))

If the middleware you're using doesn't comply with the f(http.Handler) http.Handler interface, or if extra configuration of the middleware is required, use New instead:

  nextHandler, wrapper := adapter.New()
  ns := nosurf.New(nextHandler)
  ns.SetFailureHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	  http.Error(w, "Custom error about invalid CSRF tokens", http.StatusBadRequest)
  }))
  engine.Use(wrapper(ns))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() (http.Handler, func(h http.Handler) gin.HandlerFunc)

New returns a handler to be passed to middleware which will ensure the next handler in the chain is called correctly, along with a function that wraps the middleware's handler into a gin.HandlerFunc to be passed to Engine.Use.

If the middleware does not call the handler it's wrapping, Abort is called on the Gin context.

func Wrap

func Wrap(f func(h http.Handler) http.Handler) gin.HandlerFunc

Wrap takes the common HTTP middleware function signature, calls it to generate a handler, and wraps it into a Gin middleware handler.

This is just a convenience wrapper around New.

Types

This section is empty.

Jump to

Keyboard shortcuts

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