Let Handler() return an error instead of panicking. Also removed checks for / and * since they are checked in newPath().
This commit is contained in:
parent
99a7cebd0a
commit
a0b80ced85
17
httpr.go
17
httpr.go
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user