处理子路由路径作为 URL 参数

huangapple go评论81阅读模式
英文:

go chi treating sub route path as URL param

问题

我正在创建一个基础路由器,并添加一些中间件和健康检查路由,如下所示:

baseRouter := chi.NewRouter()
baseRouter.Use(middleware.Logger)
baseRouter.Use(core.CorsHandler)
baseRouter.Get("/", func(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Context-Type", "text/plain")
    w.Write([]byte("Up and Running!!"))
})

然后我将我的子路由挂载到基础路由器上,如下所示:

baseRouter.Route("/{param1}/{param2}/dummy/new", func(r chi.Router) {
    productHandler.MountRoutes(r)
})

以下是我的productHandler.MountRoutes函数:

func (h Handler) MountRoutes(r chi.Router) {
    r.MethodFunc(http.MethodGet, "/product/report/{report_id}", h.ExportStatus)
    r.MethodFunc(http.MethodPost, "/product/report", h.ExportProducts)
    r.MethodFunc(http.MethodPost, "/product", h.GetProducts)
}

但是在GetProducts处理函数中,当我从请求上下文中获取所有的URL参数时,URL参数*被映射到了路径product,而不是URL参数。以下是GetProducts处理函数的代码片段:

ctx := context.WithValue(r.Context(), "var1", 0)
routeCtx := chi.RouteContext(ctx)
urlKeys := routeCtx.URLParams.Keys
for _, param := range urlKeys {
    parsedParam := chi.URLParam(r, param)
    fmt.Printf("%s - %s\n", param, parsedParam)
}

当调用路由/val1/val2/dummy/new/product时,上述循环的输出结果如下:

param1 - val1
param2 - val2
*      - product

我无法理解为什么*被映射到了product,尽管它是一个子路由而不是URL参数。我找不到相关问题的参考资料。

英文:

i am creating a base router and adding few middlewares and health check route as below

baseRouter := chi.NewRouter()
baseRouter.Use(middleware.Logger)
baseRouter.Use(core.CorsHandler)
baseRouter.Get("/", func(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Context-Type", "text/plain")
	w.Write([]byte("Up and Running!!"))
})

then i am mounting my sub-routes on the base router as

baseRouter.Route("/{param1}/{param2}/dummy/new", func(r chi.Router) {
	productHandler.MountRoutes(r)
})

and below is my productHandler.MountRoutes function

func (h Handler) MountRoutes(r chi.Router) {

	r.MethodFunc(http.MethodGet, "/product/report/{report_id}", h.ExportStatus)
	r.MethodFunc(http.MethodPost, "/product/report", h.ExportProducts)
	r.MethodFunc(http.MethodPost, "/product", h.GetProducts)
}

but in the GetProducts handler function, when i fetch all the url parameters from the request context, the url param * is getting mapped to the path product which is not a URL param. Below is the code snippet of the GetProducts handler

ctx := context.WithValue(r.Context(), "var1", 0)
routeCtx := chi.RouteContext(ctx)
urlKeys := routeCtx.URLParams.Keys
for _, param := range urlKeys {
	parsedParam := chi.URLParam(r, param)
	fmt.Printf("%s - %s\n", param, parsedParam)
}

the output from the above for loop when calling the url with route /val1/val2/dummy/new/product is

param1 - val1
param2 - val2
*      - product

couldn't get why the * is getting mapped to the product eventhough its a sub-route and not a URL param. Couldn't find any reference on related issue.

答案1

得分: 1

这里的问题似乎是我使用的软件包版本的问题,我使用的是github.com/go-chi/chi v4.1.2与go1.17一起使用时出现了这个问题。将其迁移到github.com/go-chi/chi/v5 v5.0.0解决了这个问题。

英文:

The issue here seems to be the version of the package i was using, i was using github.com/go-chi/chi v4.1.2 with go1.17 where i was getting this issue. Migrating to github.com/go-chi/chi/v5 v5.0.0 solved the issue.

huangapple
  • 本文由 发表于 2022年12月31日 14:32:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/74967911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定