httprouter icon indicating copy to clipboard operation
httprouter copied to clipboard

Is it possible to bind one router to some path in another router?

Open ravsii opened this issue 3 years ago • 1 comments

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.

ravsii avatar Mar 07 '22 16:03 ravsii

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.

abemedia avatar Mar 12 '22 11:03 abemedia

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

letmestudy avatar Sep 21 '22 16:09 letmestudy