1
0
Fork 0

Renamed a root var to a more logically suitable name base.

This commit is contained in:
Alexander Andreev 2023-08-12 19:19:04 +04:00
parent d53622908b
commit aba211f3ec
Signed by: Arav
GPG Key ID: D22A817D95815393
1 changed files with 6 additions and 9 deletions

View File

@ -212,28 +212,25 @@ func (rr *Router) ServeStatic(path string, root http.FileSystem) error {
// passed by a Handler() func.
type subPath struct {
r *Router
root string
base string
}
// Sub returns a sub-path with a root path, after that you can shorten patterns.
//
// E.g. instead of writing each time "/api/something/other" create a
// sub-router with a root path "/api/something" and then pass just "/other" in
// a Handler() func of subPath struct.
func (rr *Router) Sub(root string) *subPath {
if root[len(root)-1] == '/' {
root = root[:len(root)-1]
func (rr *Router) Sub(base string) *subPath {
if base[len(base)-1] == '/' {
base = base[:len(base)-1]
}
return &subPath{
r: rr,
root: root,
base: base,
}
}
// Handler attaches root path to a given pattern and pass it to a router.
func (sp *subPath) Handler(method, pattern string, handler http.HandlerFunc) error {
return sp.r.Handler(method, sp.root+pattern, handler)
return sp.r.Handler(method, sp.base+pattern, handler)
}
// Param returns a URL parameter (that is set like `/a/b/:key/d`) with a key