gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:48:13 26 4
gpt4 key购买 nike

我正在努力让我的路由特定中间件与 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/

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