我有一个实现中间件的问题,我想使用negroni.Wrap函数来验证用户位置,下面是一个调用处理程序的方法:
r.Handle("/users/{userID}", negroni.New(
negroni.HandlerFunc(validateTokenMiddleware),
negroni.Wrap(&userLocation),
negroni.Wrap(&userDetailHandler),
)).Methods("GET")而&userLocation是包含数据库信息的结构的对象,在这里,当我请求处理程序时,两个wrap一起执行的时间。但是我想先执行&userlocation,如果出现任何错误,那么next wrap不应该被执行,如何解决这个问题。
谢谢。
发布于 2018-03-01 01:55:16
使用具有ServeHttp方法的中间件,该方法将使用userDetailHandler作为中间件内部的接收器来调用。并在中间件中将db结构作为参数传递。你应该使用单层包装。
r.Handle("/users/{userID}", negroni.New(
negroni.HandlerFunc(validateTokenMiddleware),
negroni.Wrap(userDetailHandler.Middleware(&userLocation)),
)).Methods("GET")
func (userDetailHandler *UserDetailHandler)Middleware(res http.ResponseWriter, req *http.Request, userLocation *userLocation){
// use your serve Http inside this
userDetailHandler.ServeHttp(res, req)
}https://stackoverflow.com/questions/49034014
复制相似问题