大猩猩/多路复用器嵌套路由不匹配

我不确定为什么我的路线不匹配。 匹配,我得到预期的输出,但不是。当我尝试在浏览器上进行匹配时,我得到了。localhost:8000localhost:8000/admin/test404/admin/test


我想以这种方式组织我的代码,因为我想在每个.package


项目结构


/admin

  admin.go

main.go

main.go


package main


import (

    "example.com/liondancer/playground/admin"

    "fmt"

    "github.com/gorilla/mux"

    "log"

    "net/http"

)


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

    fmt.Fprintf(w, "Hi there!")

}


func main() {

    r := mux.NewRouter()

    r.HandleFunc("/", handler)

    r.PathPrefix("/admin").Handler(admin.NewHandler())

    log.Fatal(http.ListenAndServe(":8000", r))

}

admin.go


package admin


import (

    "fmt"

    "net/http"

    "github.com/gorilla/mux"

)


type Handler struct {

    router *mux.Router

}


func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

    h.router.ServeHTTP(w, r)

}


func NewHandler() *Handler {

    h := &Handler{}

    r := mux.NewRouter()

    h.router = r

    h.addRoutes()

    return h

}


func (h *Handler) addRoutes() {

    fmt.Print("hi here"). // <--- printed here to prove func gets ran.

    h.router.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {

        w.WriteHeader(http.StatusOK)

        fmt.Fprintf(w, "test")

    })

}


沧海一幻觉
浏览 79回答 2
2回答

手掌心

嵌套处理程序永远不会匹配,因为您没有删除早期处理程序的 URL 前缀。快速修复以显示我的意思:// h.router.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {// quick fixh.router.HandleFunc("/admin/test", func(w http.ResponseWriter, r *http.Request) {显然,您希望嵌套处理程序不知道它的嵌套方式,因此在注册子处理程序时,请去除 URL 前缀,以便您的子处理程序匹配:// r.PathPrefix("/admin").Handler(admin.NewHandler())// betterr.PathPrefix("/admin/").Handler(&nbsp; &nbsp; http.StripPrefix(&nbsp; &nbsp; &nbsp; &nbsp; "/admin",&nbsp; &nbsp; &nbsp; &nbsp; admin.NewHandler(),&nbsp; &nbsp; ),)

拉风的咖菲猫

我能够使用.以下是大猩猩Mux中嵌套子路由的用法示例的链接Subrouter()Subrouter()func main() {&nbsp; &nbsp; r := mux.NewRouter()&nbsp; &nbsp; r.HandleFunc("/", handler)&nbsp; &nbsp; r.PathPrefix("/admin").Subrouter().HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; w.WriteHeader(http.StatusOK)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintf(w, "test")&nbsp; &nbsp; })&nbsp; &nbsp; log.Fatal(http.ListenAndServe(":8000", r))}如果你想把它分成另一个文件,你也可以这样做:func main() {&nbsp; &nbsp; r := mux.NewRouter()&nbsp; &nbsp; r.HandleFunc("/", handler)&nbsp; &nbsp; subRoute := r.PathPrefix("/admin").Subrouter()&nbsp; &nbsp; NewHandler(subRoute)&nbsp; &nbsp; log.Fatal(http.ListenAndServe(":8000", r))}func NewHandler(r* mux.Router) *Handler {&nbsp; &nbsp; h := &Handler{}&nbsp; &nbsp; h.router = r&nbsp; &nbsp; h.AddRoutes()&nbsp; &nbsp; return h}func (h *Handler) AddRoutes() {&nbsp; &nbsp; fmt.Print("hi here") // <--- printed here to prove func gets ran.&nbsp; &nbsp; &nbsp; &nbsp; h.router.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; w.WriteHeader(http.StatusOK)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintf(w, "test")&nbsp; &nbsp; })}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go