- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我的基础 main
设置:
muxRouter := mux.NewRouter()
v1Router.Router(muxRouter.PathPrefix("/v1").Subrouter())
http.Handle("/", muxRouter)
n := negroni.Classic()
n.Use(negroni.HandlerFunc(apiRouter.Middleware))
n.UseHandler(muxRouter)
s := &http.Server{
Addr: ":6060",
Handler: n,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Fatal(s.ListenAndServe())
在apiRouter.Middleware
里面我设置了以下上下文:
context.Set(req, helperKeys.DomainName, "some-value")
但是,在v1Router.Router
内的一些handlerFunc中当试图 Get
上下文的值,结果为 nil:
domain := context.Get(req, helperKeys.DomainName)
fmt.Println("DomainName", domain)
打印:DomainName <nil>
我知道 Set
方法是正确的,因为在 apiRouter.Middleware
中设置值后立即获取值将返回正确的字符串值。
最佳答案
我最终使用了 Go 1.7
的内置 Context
:
context.Set(req, helperKeys.DomainName, "some-value")
// Replaced with:
ctx := req.Context()
ctx = context.WithValue(ctx, helperKeys.DomainName, "some-value")
req = req.WithContext(ctx)
和
domain := context.Get(req, helperKeys.DomainName)
// Replaced with:
domain := req.Context().Value(helperKeys.DomainName).(string)
关于go - negroni 中间件中设置的请求上下文在嵌套的 gorilla 子路由器中丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42229594/
我想为不同的路径使用不同的中间件。我当前的实现来自这个 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
我是一名优秀的程序员,十分优秀!