dongxian6285 2016-01-06 17:34
浏览 14
已采纳

马提尼酒中的处理机叫什么顺序?

About golang martini

  • We can add middlewares using m.Use(). Of course, "Middleware Handlers are invoked in the order that they are added".
  • In addition, a handler can also be added by router like r.Get("/", handler).
  • Sometimes, we also need a handler be called after the router handler. That is a handler is called before something is written to ResponseWriter.

So, how to order of presentation of these handlers? I can not get solution is martini's document.

  • 写回答

1条回答 默认 最新

  • duanqun7761 2016-01-07 09:24
    关注

    As you said, middlewares in Martini and others are called in the order they are defined: first the ones added with use, then the route middlewares, then the route handler.

    Here is the middleware example given by the martini documentation:

    // log before and after a request
    m.Use(func(c martini.Context, log *log.Logger){
        log.Println("before a request")
    
        c.Next()
    
        log.Println("after a request")
    })
    

    According to this, if you have middlewares A and B and the route R, then the call chain will be something like that:

    func A() {
        // Do things before B
    
        func B() {
            // Do things before R
    
            func R() {
                // Do things in R
            } ()
    
            // Do things after R
        }()
    
        // Do things after B
    }
    

    So depending of what you need, you will need to add code in a middleware before or after the Next() call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示