duanba3707 2015-08-29 13:09
浏览 39
已采纳

使用Negroni时,可以仅在每个请求中全局使用自定义HTTP处理程序吗?

To make sure error results are handled correctly across all requests I'm implementing a custom handler as described in http://blog.golang.org/error-handling-and-go. So instead of only accepting the w http.ResponseWriter, r *http.Request params the handler optionally returns an error.

I'm using Negroni and wondered whether I can set it up once to wrap all requests into handler or if it will always have to be set up on a per-request basis as done for / and /foo in the following example?

type handler func(w http.ResponseWriter, r *http.Request) error

// ServeHTTP checks for error results and handles them globally
func (fn handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if err := fn(w, r); err != nil {
        http.Error(w, err, http.StatusInternalServerError)
    }
}

// Index matches the `handler` type and returns an error
func Index(w http.ResponseWriter, r *http.Request) error {
    return errors.New("something went wrong")
}

func main() {
    router := mux.NewRouter()
    // note how `Index` is wrapped into `handler`. Is there a way to 
    // make this global? Or will the handler(fn) pattern be required 
    // for every request?
    router.Handle("/", handler(Index)).Methods("GET")
    router.Handle("/foo", handler(Index)).Methods("GET")

    n := negroni.New(
        negroni.NewRecovery(),
        negroni.NewLogger(),
        negroni.Wrap(router),
    )

    port := os.Getenv("PORT")
    n.Run(":" + port)
}
  • 写回答

1条回答 默认 最新

  • duanpu1111 2015-08-30 01:27
    关注

    You can write a wrapper around r.Handle if you want. You can't do it globally with Negroni as not all middleware you use assumes your handler type.

    e.g.

    // Named to make the example clear.
    func wrap(r *mux.Router, pattern string, h handler) *mux.Route {
        return r.Handle(pattern, h)
    }
    
    func index(w http.ResponseWriter, r *http.Request) error {
        io.WriteString(w, "Hello")
        return nil
    }
    
    func main() {
        r := mux.NewRouter()
        wrap(r, "/", index)
    
        http.ListenAndServe(":8000", r)
    }
    

    I'd argue that this is not much better than just explicitly type-casting your handlers (which is clear, if a little repetitive), or turning your handler type into a struct. The latter you can later extend to contain thread-safe fields (your DB pool, app config, etc) that you can then explicitly pass alongside each handler).

    In reality your current router code is still clear and easy to read, and makes it obvious (to others) what type underpins your handlers.

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

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存