duanjuan3931 2018-02-14 15:52
浏览 46
已采纳

通过中间件配置时,如何允许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.

  • 写回答

1条回答 默认 最新

  • duanrui3480 2018-02-15 19:34
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示