如果我要使用 DefaultServeMux(我通过将它nil作为第二个参数传递给 ListenAndServe 来指定它),那么我可以访问http.HandleFunc,您在下面的 Go wiki 示例中看到了它:
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
在我当前的代码中,我无法使用 DefaultServeMux,即我将自定义处理程序传递给 ListenAndServe
h := &mypackage.Handler{
Database: mydb
}
http.ListenAndServe(":8080", h)
所以我没有http.HandleFunc内置。但是,我必须将一些授权代码调整到我的代码库中,这些代码需要类似http.HandleFunc. 例如,如果我一直在使用 DefaultServeMux,当我点击"/protected"路由时,我会想去Protected处理程序,但只有在通过h.AuthorizationHandlerFunc这样的
h.AuthorizationHandlerFunc(Protected)
但是,由于我没有使用 DefaultServeMux,因此它不起作用,即我无法将Protected函数(并调用它)传递给AuthorizationHandlerFunc. 这是下面 AuthorizationHandlerFunc 的实现。你可以在下面看到Protected永远不会被调用的。
问题:HandlerFunc在这种情况下如何实现(不使用 DefaultServeMux)?
func (h *Handler) AuthorizationHandlerFunc(next http.HandlerFunc) http.Handler{
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
h.AuthorizationMiddleWare(w, r, next)
})
}
func (h *Handler) AuthorizationMiddleWare(w http.ResponseWriter, r *http.Request, next http.HandlerFunc){
//other stuff happens
log.Println("this is never getting called")
next(w,r)
}
func (h *Handler)Protected(w http.ResponseWriter, r *http.Request){
log.Println("this is never getting called")
}
更新 ServeHTTP 在 mypackage.Handler 上实现。为什么 Protected 函数没有被调用,或者,AuthorizationMiddleWare 中的相关代码没有被调用?
智慧大石
相关分类