通过中间件配置时,如何允许Buffalo(gobuffalo)中间件的skip()方法?

I'm trying to create a custom Buffalo (gobuffalo) middleware that accepts config which is working. The issue is I lose the ability to skip the middleware function with this error:

actions/app.go:63:22: cannot use myMiddlewareFunc (type func(myConfig) buffalo.MiddlewareFunc) as type buffalo.MiddlewareFunc in argument to app.Middleware.Skip

The code I've got thus far is:

package actions

import (
    "github.com/gobuffalo/buffalo"
    "github.com/gobuffalo/buffalo/middleware"
    "github.com/gobuffalo/envy"

    "github.com/gobuffalo/buffalo/middleware/csrf"
    "github.com/gobuffalo/buffalo/middleware/i18n"
    "github.com/gobuffalo/packr"
)

// ENV is used to help switch settings based on where the
// application is being run. Default is "development".
var ENV = envy.Get("GO_ENV", "development")
var app *buffalo.App
var T *i18n.Translator

type myConfig struct {
    value string
}

// App is where all routes and middleware for buffalo
// should be defined. This is the nerve center of your
// application.
func App() *buffalo.App {
    if app == nil {
        app = buffalo.New(buffalo.Options{
            Env:         ENV,
            SessionName: "_myapp_session",
        })
        // Automatically redirect to SSL
        app.Use(ssl.ForceSSL(secure.Options{
            SSLRedirect:     ENV == "production",
            SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
        }))

        if ENV == "development" {
            app.Use(middleware.ParameterLogger)
        }

        // Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
        // Remove to disable this.
        app.Use(csrf.New)

        // Wraps each request in a transaction.
        //  c.Value("tx").(*pop.PopTransaction)
        // Remove to disable this.
        app.Use(middleware.PopTransaction(models.DB))

        // Setup and use translations:
        var err error
        if T, err = i18n.New(packr.NewBox("../locales"), "en-US"); err != nil {
            app.Stop(err)
        }
        app.Use(T.Middleware())
        app.Use(myMiddlewareFunc(myConfig{
            value: "test value",
        }))
        app.Middleware.Skip(myMiddlewareFunc, TestHandler)
        app.GET("/", HomeHandler)
        app.GET("/test", TestHandler)

        app.ServeFiles("/assets", assetsBox)
    }

    return app
}

// TestHandler is a test handler
func TestHandler(c buffalo.Context) error {
    return c.Render(200, r.String("Test1234"))
}

func myMiddlewareFunc(config myConfig) buffalo.MiddlewareFunc {
    return func(next buffalo.Handler) buffalo.Handler {
        return func(c buffalo.Context) error {
            c.Logger().Info("Test ", config.value)

            return next(c)
        }
    }
}

So @ttomalak's method of creating a method with a receiver worked as well as the two methods below:

    mw := myMiddlewareFunc(myConfig{
        value: "test value",
    })
    app.Use(mw)
    app.Middleware.Skip(mw, TestHandler)

and

    config := myConfig{
        value: "test value",
    }
    app.Use(myMiddlewareFunc(config))
    app.Middleware.Skip(myMiddlewareFunc(config), TestHandler)

All ways require calling the middleware method. The method shown here can't be used:

a.Middleware.Skip(Authorization, HomeHandler, ...)

https://github.com/gobuffalo/buffalo/blob/master/middleware.go#L77

I'm new to go but my sense of it is that the methods aren't conforming to the MiddlewareFunc interface so calling them and getting the return type buffalo.MiddlewareFunc is what's allowing it to work.

You need to staisfy required func but your require additional parameters which can't be passed. You probably can change how you approach this by making your myConfig as reciever of myMiddlewareFunc, by this I mean:

type myConfig struct {
    value string
}

func (config *myConfig) myMiddlewareFunc() buffalo.MiddlewareFunc {
    return func(next buffalo.Handler) buffalo.Handler {
        return func(c buffalo.Context) error {
            c.Logger().Info("Test ", config.value)

            return next(c)
        }
    }
}


func App() *buffalo.App {
    if app == nil {
        app = buffalo.New(buffalo.Options{
            Env:         ENV,
            SessionName: "_myapp_session",
        })
        // Automatically redirect to SSL
        app.Use(ssl.ForceSSL(secure.Options{
            SSLRedirect:     ENV == "production",
            SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
        }))

        if ENV == "development" {
            app.Use(middleware.ParameterLogger)
        }

        // Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
        // Remove to disable this.
        app.Use(csrf.New)

        // Wraps each request in a transaction.
        //  c.Value("tx").(*pop.PopTransaction)
        // Remove to disable this.
        app.Use(middleware.PopTransaction(models.DB))

        // Setup and use translations:
        var err error
        if T, err = i18n.New(packr.NewBox("../locales"), "en-US"); err != nil {
            app.Stop(err)
        }
        app.Use(T.Middleware())

        c := &myConfig{
            value: "test value",
        }
        app.Use(c.myMiddlewareFunc())
        app.Middleware.Skip(c.myMiddlewareFunc(), TestHandler)


        app.GET("/", HomeHandler)
        app.GET("/test", TestHandler)

        app.ServeFiles("/assets", assetsBox)
    }

    return app
}