对于一个可能很简单的问题,我深陷其中。我需要使用对第三方 CAS 身份验证服务的调用包装一个函数。我正在使用 go-cas 来执行此操作,并且在我开始添加路由要求之前一直有效。我选择了 Julien Schmidt 的 httprouter,不知何故我也需要让它与 go-cas 一起工作。
如果我没记错的话,我需要使用某种定制设计的中间件来从一个处理程序转到另一个处理程序。我认为链条需要像这样:
http.Handler -> func(http.ResponseWriter, *http.Request, httprouter.Params)
...第一个是 CAS 想要的,第二个是 httprouter 想要的。但我现在很迷茫,不知道该做什么。
感谢您的任何建议!
在下面的代码中,调用...
router.Handler("GET", "/", client.HandleFunc(defaultHandler))
... 产生此错误:
“不能在 client.HandleFunc 的参数中使用 defaultHandler(类型 func(http.ResponseWriter, *http.Request, httprouter.Params))作为类型 func(http.ResponseWriter, *http.Request)”
这是无效代码:
package main
import (
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/go-cas/cas"
"github.com/julienschmidt/httprouter"
)
func defaultHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if !cas.IsAuthenticated(r) {
cas.RedirectToLogin(w, r)
}
pageID := ps.ByName("pageID")
type pageModel struct {
Title string
PageID string
}
model := pageModel{
Title: "Seminars",
PageID: pageID,
}
render.ToBrowser(w, "views/index.html", &model)
}
func main() {
u, _ := url.Parse("https://cas_example_server.com")
client := cas.NewClient(&cas.Options{
URL: u,
})
router := httprouter.New()
//This line fails with the message:
//"Cannot use defaultHandler (type func(http.ResponseWriter, *http.Request, httprouter.Params))
//as type func(http.ResponseWriter, *http.Request) in argument to client.HandleFunc"
router.Handler("GET", "/", client.HandleFunc(defaultHandler))
err := http.ListenAndServe(":8080", router)
if err != nil {
panic(err)
}
}
最佳答案
您的中间件可能会使用请求上下文将数据传递给具有不同签名的处理程序:
import (
"net/http"
"net/url"
"github.com/go-cas/cas"
"github.com/julienschmidt/httprouter"
"golang.org/x/net/context"
)
func defaultHandler(w http.ResponseWriter, r *http.Request) {
if !cas.IsAuthenticated(r) {
cas.RedirectToLogin(w, r)
}
ps := r.Context().Value("params").(httprouter.Params)
// business logic
}
func main() {
u, _ := url.Parse("https://cas_example_server.com")
client := cas.NewClient(&cas.Options{
URL: u,
})
router := httprouter.New()
//This line fails with the message:
//"Cannot use defaultHandler (type func(http.ResponseWriter, *http.Request, httprouter.Params))
//as type func(http.ResponseWriter, *http.Request) in argument to client.HandleFunc"
router.Handler("GET", "/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
newContext := context.WithValue(r.Context(), "params", ps)
r.WithContext(newContext)
client.HandleFunc(defaultHandler)(w, r)
})
err := http.ListenAndServe(":8080", router)
if err != nil {
panic(err)
}
}
更新: 有许多有用的库可以像这样保存您的 http 处理程序堆栈 https://github.com/urfave/negroni
关于go - 链接 CAS 中间件和 httprouter() 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46433817/
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle
我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c
Rails中有没有一种方法可以提取与路由关联的HTTP动词?例如,给定这样的路线:将“users”匹配到:“users#show”,通过:[:get,:post]我能实现这样的目标吗?users_path.respond_to?(:get)(显然#respond_to不是正确的方法)我最接近的是通过执行以下操作,但它似乎并不令人满意。Rails.application.routes.routes.named_routes["users"].constraints[:request_method]#=>/^GET$/对于上下文,我有一个设置cookie然后执行redirect_to:ba
路由有如下代码:resources:orders,only:[:create],defaults:{format:'json'}resources:users,only:[:create,:update],defaults:{format:'json'}resources:delivery_types,only:[:index],defaults:{format:'json'}resources:time_corrections,only:[:index],defaults:{format:'json'}是否可以使用1个字符串为所有资源设置默认格式,每行不带“默认值”散列?谢谢。
我有一个未排序的链接列表,我将其保存在旁边,我想单击每个链接并确保它转到真实页面而不是404、500等。问题是我不知道该怎么做。是否有一些我可以检查的对象会给我http状态代码或任何东西?mylinks=Browser.ul(:id,'my_ul_id').linksmylinks.eachdo|link|link.click#needtocheckfora200statusorsomethinghere!how?Browser.backend 最佳答案 我的回答与铁皮人的想法类似。require'net/http'require'
我一直在玩一个脚本,它在Chrome中获取选定的文本并在Google中查找它,提供四个最佳选择,然后粘贴相关链接。它以不同的格式粘贴,具体取决于当前在Chrome中打开的页面-DokuWiki打开的DokuWiki格式,普通网站的HTML,我想要我的WordPress所见即所得编辑器的富文本。我尝试使用pbpaste-Preferrtf来查看没有其他样式的富文本链接在粘贴板上的样子,但它仍然输出纯文本。在文本编辑中保存文件并进行试验后,我想出了以下内容text=%q|{\rtf1{\field{\*\fldinst{HYPERLINK"URL"}}{\fldrsltTEXT}}}|te
基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub