我正在尝试创建一个自定义Buffalo(gobuffalo)中间件,它接受正在运行的配置 . 问题是我失去了使用此错误跳过中间件功能的能力:
actions / app.go:63:22:不能使用myMiddlewareFunc(类型为func(myConfig)buffalo.MiddlewareFunc)作为app.Middleware.Skip参数中的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)
所有方式 require 调用中间件方法 . 此处显示的方法无法使用:
a.Middleware.Skip(Authorization, HomeHandler, ...)
我是新手,但我的感觉是这些方法不符合MiddlewareFunc接口所以调用它们并获得返回类型buffalo.MiddlewareFunc是允许它工作的 .