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 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。