Git Product home page Git Product logo

gin-adapter's Introduction

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.

gin-adapter's People

Contributors

gwatts avatar housek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gin-adapter's Issues

Doesn't work for NYT GZip Middleware

gin-adapter doesn't compress the output, although WrapHH does...

import (
   "github.com/gin-gonic/gin"
   "github.com/NYTimes/gziphandler"
   "github.com/gwatts/gin-adapter"
)
oRouteHandler := gin.Default()
oRouteHandler.Use( adapter.Wrap( gziphandler.GzipHandler ) )
...

Expose swappedResponseWriter

Respectfully request that you expose swappedResponseWriter for better compatibility with authBoss. This might not be the only package with this type of issue.

In their design, they expect a http.Writer of a particular type (ClientStateResponseWriter) when writing responses. It's relatively easy to work around if swappedResponseWriter is exposed as SwappedResponseWriter (currently I have a local copy of adapter.go to make things work...):

import (
    // ...
    _ "github.com/volatiletech/authboss/v3"
)

func (w swappedResponseWriter) UnderlyingResponseWriter() http.ResponseWriter {
	return w.w
}

Custom middleware to get status code does not seem to work

we have a simple custom middleware to log the status code of the response like follow:

import (
      "github.com/urfave/negroni"
      adapter "github.com/gwatts/gin-adapter"
)
func statusCodeMiddleware() func(h http.Handler) http.Handler {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
			lrw := negroni.NewResponseWriter(rw) // this provides a Wrapper ResponseWrite that overrides the "WriteHeader(code int)" to populate it's own "statusCode" field 
			h.ServeHTTP(lrw, r)
			statusCode := lrw.Status()
			log.Printf("############# %d -%s ###########", statusCode, http.StatusText(statusCode))
		})
	}
}

router := gin.Default()
rotuer.Use(adapter.Wrap(statusCodeMiddleware()))

But the statusCode is being logged as 0 (default 0 value)

2021/04/24 16:45:03 ############# 0 -   ###########

go version go1.15.6 darwin/amd64

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.