doupike2351 2016-10-27 23:45
浏览 4

如何通过yaag中间件包装大猩猩mux func处理程序

I am following this tutorial. http://thenewstack.io/make-a-restful-json-api-go/

router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
    router.
        Methods(route.Method).
        Path(route.Pattern).
        Name(route.Name).
        Handler(route.HandlerFunc)
}

I need to wrap the endpoint func using yaag middleware.

r.HandleFunc("/", middleware.HandleFunc(handler))

How to achieve this?

EDIT: I am wrapping in around the Logger and returning the haddler. Logger takes first argument, as http.Handle. So wrapping the route.HandlerFunc won't work. Can you please help me here?

handler := Logger(route.HandlerFunc, route.Name)

    router.
        Methods(route.Method).
        Path(route.Pattern).
        Name(route.Name).
        Handler(handler)
  • 写回答

1条回答 默认 最新

  • doulan8846 2016-10-28 01:06
    关注

    All you have to do is substitute .Handler() by .HandlerFunc() and wrap your handler function with the middleware, so each endpoint will pass first for the yaag middleware and then to your handler function, like this:

    router := mux.NewRouter().StrictSlash(true)
    for _, route := range routes {
        router.
            Methods(route.Method).
            Path(route.Pattern).
            Name(route.Name).
            HandlerFunc(middleware.HandleFunc(route.HandlerFunc)) // change here
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败