go - Negroni 特定于路由的中间件

标签 go negroni

我正在努力让我的路由特定中间件与 httprouter 和 Negroni 一起工作。登录路由需要 Middleware2,所有其他路由需要 Middleware1

到目前为止我有:

func Main() {
    router := httprouter.New()
    protectedRoutes := httprouter.New()

    DefineRoutes(router)
    DefineProtectedRoutes(protectedRoutes)

    //This is the block that I'm unsure about
    //How can I introduce Middleware2 for a specific route?
    n := negroni.New()
    n.Use(negroni.HandlerFunc(Middleware1))
    n.UseHandler(router)


    n.Run(":5000")
}

func DefineRoutes(router *httprouter.Router) {
    router.POST("/v1/users/authenticate", UserLogin)
    router.POST("/v1/users/authorize", UserLogin)
    router.POST("/v1/users/logout", UserLogout)
}

func DefineProtectedRoutes(router *httprouter.Router) {
    router.POST("/v1/users/login", UserLogin)
}

但现在我有点卡住了,因为网站 (https://github.com/codegangsta/negroni) 上的示例使用的是标准处理程序。

最佳答案

不确定是否有人仍在寻找答案。我认为可以通过调用 router.Lookup 函数来检索参数。这是我所做的:

_, params, _ :=  router.Lookup("GET", req.URL.Path)

其中 router 是 httprouter.Router 的一个实例

关于go - Negroni 特定于路由的中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30350317/

相关文章:

go - axios 不发送 POST 到 golang api

go - Negroni 在请求完成后继续调用其他处理程序

go - 在 golang 中更新 grpc 的接收和发送消息大小

go - 正确区分未设置(nil)和空白/空值

go - 如何避免同时打印字母数字的死锁

go - 在 HTTP 处理程序中提供子目录服务 [GoLang]

json - 在 http 响应中将任意字符串编码为 JSON

docker - Kubernetes:Golang应用程序容器无法启动

go - 如何使用命令行在 linux 中使用 ffmpeg 将两个 wav 拼接在一起?

go - 使用 Negroni 时自定义 HTTP 处理程序可以全局使用还是仅按请求使用?