1 回答

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以http.Handler按照此處所述進(jìn)行組合運(yùn)算以重用代碼。
在您的情況下,組合器看起來(lái)像這樣(根據(jù)您的口味和要求進(jìn)行調(diào)整):
func NewCanonicalDomainHandler(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Host != "myapp.com" {
u := *r.URL
u.Host = "myapp.com"
u.Scheme = "http"
http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
return
}
next(w, r)
}
}
你可以用它包裝你的處理程序:
http.Handle("/foo", NewCanonicalDomainHandler(someHandler))
- 1 回答
- 0 關(guān)注
- 206 瀏覽
添加回答
舉報(bào)