- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很难理解如何一起使用 negroni 和 httprouter。
我有几个公共(public)路由,例如 /api/v1/ping
我有一堆需要身份验证中间件的私有(private)路由,例如/api/v1/user
如果我想要所有路由都使用 negroni 通用中间件,但我想将 auth 中间件和其他中间件仅应用于私有(private)路由,我该如何设置?
v1.router := httprouter.New()
v1.router.GET("/api/v1/ping", v1.ping)
v1.router.GET("/api/v1/user", v1.getUsers)
n := negroni.Classic()
n.UseHandler(v1.router)
http.ListenAndServe(port, n)
最佳答案
您可以尝试采用“Path Prefix Middleware in Go ”中描述的技术,该技术使用net/http/#ServeMux
,与另一个路由器( gorilla/mux
),但应该对 julienschmidt/httprouter
有效还有:
Specifying middleware based on route prefixes
This is where the magic happens, and it is also where things get confusing.
The easiesy way I have found to specify middleware for a path prefix is to setup a second muxer (we use the
sirMuxalot
variable for ours below) that has the path prefixes we want to apply middleware to, and to then pass in our original router wrapped in some middleware for those routes.This works because the
sirMuxalot
router won’t ever call the middleware-wrapped router unless the path prefix we define gets matched with the incoming web request’s path.sirMuxalot := http.NewServeMux()
sirMuxalot.Handle("/", r)
sirMuxalot.Handle("/api/", negroni.New(
negroni.HandlerFunc(APIMiddleware),
negroni.Wrap(r),
))
sirMuxalot.Handle("/dashboard/", negroni.New(
negroni.HandlerFunc(DashboardMiddleware),
negroni.Wrap(r),
))
n := negroni.Classic()
n.UseHandler(sirMuxalot)
http.ListenAndServe(":3000", n)
关于go - 用于公共(public)和私有(private)路由中间件的 httprouter 和 negroni,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58460030/
我想为不同的路径使用不同的中间件。我当前的实现来自这个 link UserRouter := mux.NewRouter().StrictSlash(true) AdminRouter := mux.
我正在尝试将 Gorilla session 添加到 Negroni 中间件处理程序中的请求上下文,以便我可以在我的 Gorilla Mux 处理程序中访问它。这是我的代码的精简版本: // Sess
我的 Go 网络应用程序(使用 Gorilla mux 和 negroni)有大约 20 个处理程序,根据应应用的中间件功能分为三组。具体来说: 第 1 组:静态请求(根本没有中间件) GET /
我正在努力让我的路由特定中间件与 httprouter 和 Negroni 一起工作。登录路由需要 Middleware2,所有其他路由需要 Middleware1。 到目前为止我有: func Ma
我有一个函数,我将其用作每个 GET 请求的包装器: type HandlerFunc func(w http.ResponseWriter, req *http.Request) (interfac
我在实现中间件时遇到问题 我想使用 negroni.Wrap 函数来验证用户位置,下面是调用处理程序的方法: r.Handle("/users/{userID}", negroni.New(
我正在使用 Golang、Negroni 和 Gorilla mux 作为 Web API 服务器。我在/api 下有我的 api 路由,我正在使用 Negroni 使用/下的 url 从我的/pub
对我的应用程序的每个请求都应该使用一些中间件。我使用 Negroni 文档实现了它: func MyMiddleware(rw http.ResponseWriter, r *http.Request
我有一个使用 httprouter 和 negroni 的网络服务器。用户通过外部 OAuth 登录该系统。我们将 token 保存到加密 session 中,指示他们是否已登录。我想使用中间件来验证
所以我正在尝试设置我的路由器以响应 /users 和 /users/{userId} 所以我尝试了这段代码: usersRouter := router.PathPrefix("/users").Su
我的基础 main设置: muxRouter := mux.NewRouter() v1Router.Router(muxRouter.PathPrefix("/v1").Subrouter()) h
为了确保错误结果在所有请求中得到正确处理,我正在实现自定义处理程序,如 http://blog.golang.org/error-handling-and-go 中所述。 .因此,处理程序不是只接受
是否可以使用 Gorilla's context.ClearHandler() 作为 Negroni 的中间件,就像我看到它用作 Alice 的中间件一样?像这样的东西: n.Use(context.
我试图只在某些路由上添加中间件。我写了这段代码: func main() { router := mux.NewRouter().StrictSlash(false) admin_subrou
我正在使用以下代码运行服务器: // Assuming there is no import error mux := http.NewServeMux() mux.HandleFunc("/
我很难理解如何一起使用 negroni 和 httprouter。 我有几个公共(public)路由,例如 /api/v1/ping 我有一堆需要身份验证中间件的私有(private)路由,例如/ap
所以我是 Go 的新手,正在尝试构建一个简单的 Web 服务器。我遇到问题的一部分是我想使用动态静态 url 提供静态文件(以启用浏览器的长期缓存)。例如,我可能有这个 url: /static/87
我是一名优秀的程序员,十分优秀!