gpt4 book ai didi

go - 当我传递中间件配置时,如何允许 Buffalo (gobuffalo) 中间件使用 skip() 方法?

转载 作者:数据小太阳 更新时间:2023-10-29 03:23:31 25 4
gpt4 key购买 nike

我正在尝试创建一个接受正在运行的配置的自定义 Buffalo (gobuffalo) 中间件。问题是我无法通过此错误跳过中间件功能:

actions/app.go:63:22: 不能在 app.Middleware.Skip 的参数中使用 myMiddlewareFunc(类型 func(myConfig) buffalo.MiddlewareFunc)作为类型 buffalo.MiddlewareFunc

到目前为止我得到的代码是:

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)
}
}
}

所以@ttomalak 创建一个带有接收器的方法的方法和下面的两个方法一样有效:

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

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

所有方式需要调用中间件方法。不能使用此处显示的方法:

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

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

我是新手,但我的感觉是这些方法不符合 MiddlewareFunc 接口(interface),因此调用它们并获取返回类型 buffalo.MiddlewareFunc 是允许它工作的原因。

最佳答案

您需要稳定所需的 func 但您需要无法传递的额外参数。您可能可以通过将 myConfig 作为 myMiddlewareFunc 的接收者来改变您的处理方式,我的意思是:

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
}

关于go - 当我传递中间件配置时,如何允许 Buffalo (gobuffalo) 中间件使用 skip() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48791193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com