不理解中间件的结构,请问为什么用 next.ServeHTTP(w, r) 可以调用别的handler
1. 运行环境
2. 问题描述?
//: package main
import (
“fmt”
“net/http”
"github.com/gorilla/mux"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, “
Hello, 欢迎来到 goblog!
“)
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, “此博客是用以记录编程笔记,如您有反馈或建议,请联系 “+
“<a href="mailto:summer@example.com">summer@example.com“)
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "<h1>请求页面未找到 :(</h1><p>如有疑惑,请联系我们。</p>")
}
func articlesShowHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars[“id”]
fmt.Fprint(w, “文章 ID:”+id)
}
func articlesIndexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, “访问文章列表”)
}
func articlesStoreHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, “创建新的文章”)
}
func forceHTMLMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 1. 设置标头
w.Header().Set(“Content-Type”, “text/html; charset=utf-8”)
// 2. 继续处理请求
next.ServeHTTP(w, r)
})
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", homeHandler).Methods("GET").Name("home")
router.HandleFunc("/about", aboutHandler).Methods("GET").Name("about")
router.HandleFunc("/articles/{id:[0-9]+}", articlesShowHandler).Methods("GET").Name("articles.show")
router.HandleFunc("/articles", articlesIndexHandler).Methods("GET").Name("articles.index")
router.HandleFunc("/articles", articlesStoreHandler).Methods("POST").Name("articles.store")
// 自定义 404 页面
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
// 中间件:强制内容类型为 HTML
router.Use(forceHTMLMiddleware)
// 通过命名路由获取 URL 示例
homeURL, _ := router.Get("home").URL()
fmt.Println("homeURL: ", homeURL)
articleURL, _ := router.Get("articles.show").URL("id", "1")
fmt.Println("articleURL: ", articleURL)
http.ListenAndServe(":3000", router)
}
3. 您期望得到的结果?
//: 不太理解为什么 next.ServeHTTP(w, r) 可以调用之前的函数
推荐文章: