给定以下内容(Go playground 的完整示例):
// Collection
root := r.PathPrefix("/widgets/").Subrouter()
root.Methods("POST").Handler(h.Create)
// Individual
object := root.PathPrefix("/{uuid}").Subrouter()
// ~neither: object := root.PathPrefix("/{uuid}").Subrouter()
object.Methods("GET").Handler(h.Show)
object.Methods("PUT").Handler(h.Replace)
object.Methods("DELETE").Handler(h.Delete)
// Relationships
object.Methods("GET").Path("/foos").Handler(eh.Foos)
object.Methods("GET").Path("/bars").Handler(eh.Bars)
我本来希望以下 URL 触发相应的处理程序,但我似乎无法使其工作:
✔ POST /widgets => h.Create
✔ GET /widgets/123 => h.Show (assumes PathPrefix('/{uuid}'))
✔ GET /widgets/123/ => h.Show (required if 'PathPrefix('/{uuid}/')')
✖ GET /widgets/123/foos => h.Foos (actually routes h.Show)
✖ GET /widgets/123/bars => h.Bars (actually routes h.Show)
不幸的是,最后两个显然都不是可路由的,它们都触发h.Show
了,谁能指出我做错了什么?我可能已经预料到有一个无界的{uuid}
(没有尾部斜杠)可能会继续运行,忽略/
但似乎并非如此。
我什至不知道这是否与在 Github (#31) 上仍然开放的 Subrouter strict-slash 问题有关,但据我所知,我确实在那里尝试了替代方案。(即object.Methods("GET").Path("/").Handler(h.Show)
)
安装在object
根目录上的处理程序是否可以Methods()
防止任何进一步的路由匹配?