httprouter
httprouter copied to clipboard
Is it possible to bind one router to some path in another router?
I think this is possible with the current kit httprouter provides but I can't figure it out. Let's say we have some code like this
mainRouter := httprouter.New()
subRouter := httprouter.New()
subRouter.GET("/", subRouterIndexHandler)
subRouter.GET("/test", subRouterTestHandler)
mainRouter.GET("/", mainIndexHandler)
mainRouter.GET("/sub", (?)subRouter)
Assuming this would produce these routers
/ - mainIndexHandler
/sub/ - subRouterIndexHandler
/sub/test - subRouterTestHandler
I tried it by using HandlerFunc on main and ServeHTTP on sub:
mainRouter.HandlerFunc(http.MethodGet, "/sub", subRouter.ServerHTTP)
but /sub and its children return 404.
Try
mainRouter.Handler(http.MethodGet, "/sub", http.StripPrefix("/sub", subRouter))
I wouldn't recommend it though as you'll lose the performance benefits of the radix tree.
这是来自QQ邮箱的假期自动回复邮件。 您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。