Updated comments.
This commit is contained in:
parent
aba211f3ec
commit
a2cb6182e8
25
httpr.go
25
httpr.go
@ -124,9 +124,8 @@ outer:
|
|||||||
if (firstChar == ':' || firstChar == '*') && firstChar == child.endpoint[0] {
|
if (firstChar == ':' || firstChar == '*') && firstChar == child.endpoint[0] {
|
||||||
// Do not allow different param names, because only the first one
|
// Do not allow different param names, because only the first one
|
||||||
// is saved, so a param won't be available by a new name.
|
// is saved, so a param won't be available by a new name.
|
||||||
//
|
// Therefore, it is good to return an error because in this case
|
||||||
// I am not the one to judge, but it is a little strange to
|
// you're doing something wrong.
|
||||||
// expect different types of param in one place.
|
|
||||||
if path[i+1] != child.endpoint {
|
if path[i+1] != child.endpoint {
|
||||||
return errors.New("param names " + path[i+1] + " and " + child.endpoint + " are differ")
|
return errors.New("param names " + path[i+1] + " and " + child.endpoint + " are differ")
|
||||||
}
|
}
|
||||||
@ -208,15 +207,24 @@ func (rr *Router) ServeStatic(path string, root http.FileSystem) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// subPath contains a root path that is being attached in front of a pattern
|
// subPath attaches a base path in front of a pattern.
|
||||||
// passed by a Handler() func.
|
//
|
||||||
|
// It is not a sub-router, it just passes a resulted pattern down to
|
||||||
|
// a router instance.
|
||||||
type subPath struct {
|
type subPath struct {
|
||||||
r *Router
|
r *Router
|
||||||
base string
|
base string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sub returns a sub-path with a root path, after that you can shorten patterns.
|
// Sub creates a group of handlers with the same base path.
|
||||||
//
|
//
|
||||||
|
// How to use:
|
||||||
|
//
|
||||||
|
// r := httpr.New()
|
||||||
|
// ...
|
||||||
|
// s := r.Sub("/api/v1")
|
||||||
|
// s.Handler(http.MethodGet, "/", func(w, r) {...})
|
||||||
|
// s.Handler(http.MethodGet, "/section", func(w, r) {...})
|
||||||
func (rr *Router) Sub(base string) *subPath {
|
func (rr *Router) Sub(base string) *subPath {
|
||||||
if base[len(base)-1] == '/' {
|
if base[len(base)-1] == '/' {
|
||||||
base = base[:len(base)-1]
|
base = base[:len(base)-1]
|
||||||
@ -228,13 +236,12 @@ func (rr *Router) Sub(base string) *subPath {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler attaches root path to a given pattern and pass it to a router.
|
// Handler registers a handler for a sub-path.
|
||||||
func (sp *subPath) Handler(method, pattern string, handler http.HandlerFunc) error {
|
func (sp *subPath) Handler(method, pattern string, handler http.HandlerFunc) error {
|
||||||
return sp.r.Handler(method, sp.base+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
|
// Param returns a URL parameter set with :key, or an empty string if not found.
|
||||||
// or an empty string if no such parameter found.
|
|
||||||
func Param(r *http.Request, key string) string {
|
func Param(r *http.Request, key string) string {
|
||||||
if params := r.Context().Value(ParamsKey).(Params); params != nil {
|
if params := r.Context().Value(ParamsKey).(Params); params != nil {
|
||||||
return params[key]
|
return params[key]
|
||||||
|
Loading…
Reference in New Issue
Block a user