From a2cb6182e8479a6f9951f27092bc938c65092501 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 12 Aug 2023 19:19:45 +0400 Subject: [PATCH] Updated comments. --- httpr.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/httpr.go b/httpr.go index 4558075..6a0163f 100644 --- a/httpr.go +++ b/httpr.go @@ -124,9 +124,8 @@ outer: if (firstChar == ':' || firstChar == '*') && firstChar == child.endpoint[0] { // Do not allow different param names, because only the first one // is saved, so a param won't be available by a new name. - // - // I am not the one to judge, but it is a little strange to - // expect different types of param in one place. + // Therefore, it is good to return an error because in this case + // you're doing something wrong. if path[i+1] != child.endpoint { 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 -// passed by a Handler() func. +// subPath attaches a base path in front of a pattern. +// +// It is not a sub-router, it just passes a resulted pattern down to +// a router instance. type subPath struct { r *Router 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 { if 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 { 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 -// or an empty string if no such parameter found. +// Param returns a URL parameter set with :key, or an empty string if not found. func Param(r *http.Request, key string) string { if params := r.Context().Value(ParamsKey).(Params); params != nil { return params[key]