1
0
Fork 0

Let Handler() return an error instead of panicking. Also removed checks for / and * since they are checked in newPath().

This commit is contained in:
Alexander Andreev 2023-05-28 01:24:18 +04:00
parent 99a7cebd0a
commit a0b80ced85
Signed by: Arav
GPG Key ID: D22A817D95815393
1 changed files with 8 additions and 9 deletions

View File

@ -157,22 +157,21 @@ func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// Handler registers a handler for provided pattern for a given HTTP method.
func (rr *Router) Handler(method, pattern string, handler http.HandlerFunc) {
if pattern[0] != '/' {
panic("first element of path should be a slash (/) symbol")
}
if strings.Count(pattern, "*") > 1 {
panic("there can be only one wildcard (*) symbol in path")
func (rr *Router) Handler(method, pattern string, handler http.HandlerFunc) error {
path, err := newPath(pattern)
if err != nil {
return err
}
if rr.tree[method] == nil {
rr.tree[method] = &node{endpoint: "/"}
}
path, _ := newPath(pattern)
if err := rr.tree[method].add(path, handler); err != nil {
return err
}
rr.tree[method].add(path, 0, handler)
return nil
}
// ServeStatic serves a given file system.