简体   繁体   English

使用/ rs / cors作为Buffalo预备件的示例?

[英]Example using /rs/cors as preware with Buffalo?

I'm trying to get the /rs/cors package to work with the latest Buffalo as Preware, which is supposed to work according to a recent blog post ( https://blog.gobuffalo.io/buffalo-v0-9-4-released-5d2327a4742e ), but the code snippet there doesn't seem to make sense. 我正在尝试将/ rs / cors包与最新的Buffalo as Preware一起使用,根据最近的博客文章( https://blog.gobuffalo.io/buffalo-v0-9-4) -released-5d2327a4742e ),但那里的代码片段似乎没有意义。 If I generate a new buffalo app as an API, and I look at adding the cors package, I start with: 如果我生成一个新的buffalo应用程序作为API,我看看添加cors包,我开始:

app = buffalo.New(buffalo.Options{
    Env:          ENV,
    SessionStore: sessions.Null{},
    SessionName:  "_creatorhub_session",
})
// Automatically redirect to SSL
app.Use(ssl.ForceSSL(secure.Options{
    SSLRedirect:     ENV == "production",
    SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
}))

// Set the request content type to JSON
app.Use(middleware.SetContentType("application/json"))

I should obviously add something along the lines of "PreWares: []buffalo.PreWare{}," but it seems to want a HandlerFunc, which /rs/cors doesn't seem to return under normal use, so I'm failing to find the right combination to make these work properly together and I haven't found a working example. 我显然应该添加一些“PreWares:[] buffalo.PreWare {},”但它似乎想要一个HandlerFunc,/ rs / cors似乎在正常使用下没有返回,所以我没有找到合适的组合,使这些工作正常,我还没有找到一个有效的例子。

Anyone out there have an example to share? 那里有人有一个例子可以分享吗? Any ideas appreciated! 任何想法赞赏!

Finally figured it out, so I'll share the info here for anyone else. 终于明白了,所以我会在这里为其他人分享信息。 This is using Buffalo v0.10.1 and the https://github.com/rs/cors project. 这是使用Buffalo v0.10.1和https://github.com/rs/cors项目。 I dug in and found that I could set up the cors object with (using localhost for example): 我挖了进来,发现我可以设置cors对象(例如使用localhost):

    c := cors.New(cors.Options{
        AllowedOrigins:   []string{"http://localhost:8080"},
        AllowCredentials: true,
    })

and then add it into the Buffalo instantiation by getting the Handler out of it: 然后通过从中获取Handler将其添加到Buffalo实例化中:

app = buffalo.New(buffalo.Options{
    Env:          ENV,
    SessionStore: sessions.Null{},
    SessionName:  "_creatorhub_session",
    PreWares:     []buffalo.PreWare{c.Handler},
})

That worked for me. 这对我有用。 The issue was that it was difficult to find documentation for what Buffalo wanted passed in to PreWare and matching it up with what could be provided by the cors package. 问题是很难找到Buffalo想要传递给PreWare的文档,并将其与cors包提供的内容相匹配。

Hope this helps out someone else in the future! 希望这有助于将来的其他人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM