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

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

go chi treating sub route path as URL param

问题

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

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

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

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

以下是我的productHandler.MountRoutes函数:

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

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

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

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

  1. param1 - val1
  2. param2 - val2
  3. * - product

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

英文:

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

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

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

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

and below is my productHandler.MountRoutes function

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

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

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

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

  1. param1 - val1
  2. param2 - val2
  3. * - 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:

确定