如何突破martini中间件

简而言之,如果我运行一些中间件并http.Request确定该请求应该得到 HTTP 422,我如何“突破”中间件链,并在不调用链中所有中间件功能的情况下提前返回?


例如,如果我有这个:


func Routes(m *martini.ClassicMartini) {


    m.Get("/cp/users", mw.AsJson, mw.RequestTimer, ctr.GetMany)


}

如果我调用return上面的任何中间件资金,据我所知,它仍然会调用链中注册的所有中间件资金,因此ctr.GetMany始终会被调用。


有没有办法使请求/响应完成并告诉 martini 停止调用链中的所有函数?


如果第一个返回值是整数,我猜 martini 会假设它是状态代码。我目前最好的猜测是根据文档: https://github.com/go-martini/martini#middleware-handlers


我们可以用这个:


m.Use(func(c martini.Context, w http.ResponseWriter){


    if reqIsMalformed() {

        http.Error(w, "Bad request because xyz", 422)

        return;

    }


    c.Next()


})

如果不满足条件,我们就永远不会打电话c.Next()?


繁花如伊
浏览 122回答 1
1回答

明月笑刀无情

我用路由器/中间件做了一个实验,结果如下(有用的信息在最后):func check0() {&nbsp; &nbsp; return}func check01() int {&nbsp; &nbsp; return 200}func check02() (int, string) {&nbsp; &nbsp; return 200, "boop"}func check03() bool {&nbsp; &nbsp; return true}func check04() string {&nbsp; &nbsp; return "04"}func check1(res http.ResponseWriter) string {&nbsp; &nbsp; return "1"}func check2(c martini.Context, res http.ResponseWriter) string {&nbsp; &nbsp; if true {&nbsp; &nbsp; &nbsp; &nbsp; return "hiii"&nbsp; &nbsp; }&nbsp; &nbsp; c.Next()&nbsp; &nbsp; return "2"}func check3(c martini.Context, res http.ResponseWriter) string {&nbsp; &nbsp; c.Next()&nbsp; &nbsp; return "3"}func check4(res http.ResponseWriter) {&nbsp; &nbsp; res.Write([]byte("4"))}func check5(c martini.Context, res http.ResponseWriter) (int, string, string) {&nbsp; &nbsp; res.Write([]byte("5.0"))&nbsp; &nbsp; c.Next()&nbsp; &nbsp; return 200, "5.1x", "5.1y"}func finish(res http.ResponseWriter) {&nbsp; &nbsp; fmt.Println("in finish")&nbsp; &nbsp; res.Write([]byte("all done"))}func Routes(m *martini.ClassicMartini) {&nbsp; &nbsp; m.Get("/cp/meta/test/middleware0", check0, finish)&nbsp; &nbsp; m.Get("/cp/meta/test/middleware01", check01, finish)&nbsp; &nbsp; m.Get("/cp/meta/test/middleware02", check02, finish)&nbsp; &nbsp; m.Get("/cp/meta/test/middleware03", check03, finish)&nbsp; &nbsp; m.Get("/cp/meta/test/middleware04", check04, finish)&nbsp; &nbsp; m.Get("/cp/meta/test/middleware1", check1, finish)&nbsp; &nbsp; m.Get("/cp/meta/test/middleware2", check2, finish)&nbsp; &nbsp; m.Get("/cp/meta/test/middleware3", check3, finish)&nbsp; &nbsp; m.Get("/cp/meta/test/middleware4", check4, finish)&nbsp; &nbsp; m.Get("/cp/meta/test/middleware5", check5, finish)&nbsp; &nbsp; m.Get("/cp/meta/echo_runtime_config", common.AsJson, common.RequestTimer, mw.BodyToMap, ctr.GetRuntimeConfig)}这是我使用 api 时的结果:GET /cp/meta/test/middleware0 => 'all done'GET /cp/meta/test/middleware01 => ''GET /cp/meta/test/middleware03 => '<bool Value>'GET /cp/meta/test/middleware02 => 'boop'GET /cp/meta/test/middleware1 => '1'GET /cp/meta/test/middleware04 => '04'GET /cp/meta/test/middleware2 => 'hiii'GET /cp/meta/test/middleware3 => 'all done3'GET /cp/meta/test/middleware4 => '4'GET /cp/meta/test/middleware5 => '5.0all done5.1x'本来应该添加到这个问题中。所以规则如下:如果中间件函数返回任何内容(又名 func 具有非 void 返回签名),则不会调用后续中间件。注入各种参数似乎对于是否调用后续中间件(包括 martini.Context 等)没有影响。使用 martini.Context.Next() 似乎仅对在调用所有其他剩余中间件后运行挂钩有用。如果没有返回任何内容,剩下的中间件将会被调用,显然你不需要调用c.Next()。如果返回 int 作为返回列表中的第一个参数,它将被解释为 http 状态代码,第二个参数(如果有)将被写入正文。如果第一个参数是字符串而不是 int,那么它将被写入正文。我不确定是否使用或忽略第三个参数,但它们似乎被忽略。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go