Gorilla mux custom middleware

前端 未结 5 1572
醉梦人生
醉梦人生 2020-12-13 01:01

I am using gorilla mux for manage routing. What I am missing is to integrate a middleware between every request.

For example

package main

import (
         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 01:29

    I'm not sure why @OneOfOne chose to chain router into the Middleware, I think this is slight better approach:

    func main() {
        r.Handle("/",Middleware(http.HandlerFunc(homeHandler)))
        http.Handle("/", r)
    }
    
    func Middleware(h http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        h.ServeHTTP(w, r)
    })}
    

提交回复
热议问题