dpf7891 2014-08-03 18:26
浏览 56
已采纳

嵌套大猩猩Mux路由器不起作用

Using code below, when I access /test2 it responds with 404 - not found. /test1 works correctly. Why is that? Is nesting not allowed despite the fact that routers implement http.Handler interface?

package main

import (
    "fmt"
    "net/http"
    "github.com/gorilla/mux"
)

func main() {

    mainRouter := mux.NewRouter()
    subRouter := mux.NewRouter()

    mainRouter.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test1") })

    subRouter.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test2") })

    mainRouter.Handle("/", subRouter)

    http.ListenAndServe(":9999", mainRouter)
}

EDIT:

My main goal was to add some initial work which would be common for all routes in subRouter, and only for them. To be even more specific, I would like to use Negroni as my middleware orchiestrator. On the Negroni website there is an example of adding middleware to the group of routes:

router := mux.NewRouter()
adminRoutes := mux.NewRouter()
// add admin routes here

Create a new negroni for the admin middleware
router.Handle("/admin", negroni.New(
  Middleware1, 
  Middleware2, 
  negroni.Wrap(adminRoutes),
)) 

Negroni basically executes ServeHTTP methods of every argument, since all of them implement http.Handler. It executes them in order, so router routes will be last.

I'm familiar with the concept of Subrouter in Mux, but AFAIK I can't use it in similar fashion as example above, in particular, I can't inject anything between mainRouter and its Subrouter. This is why nesting looks more flexible.

  • 写回答

4条回答 默认 最新

  • duanfang7757 2014-12-05 22:07
    关注

    I know this question is somewhat old, but I have spent some time figuring out how handlers and matching work in go. You can see my experiment code here.

    Basically, you can get the effect you want with code like this:

    package main
    
    import (
        "fmt"
        "net/http"
        "github.com/gorilla/mux"
    )
    
    func main() {
    
        mainRouter := mux.NewRouter()
        subRouter := mux.NewRouter()
    
        mainRouter.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprint(w, "test1")
        })
        subRouter.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprint(w, "test2")
        })
    
        // in mux, you need to register subrouter
        // with the same path that the handlers in
        // it are matching
        mainRouter.Handle("/test2", subRouter)
    
        // if your subrouter has handlers that match
        // other sub paths - you also need to do this
        mainRouter.Handle("/test2/{_dummy:.*}", subRouter)
    
        http.ListenAndServe(":9999", mainRouter)
    }
    

    I hope this helps someone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大