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

报告相同问题?

悬赏问题

  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?