Inversed logic of if else statement to reduce nesting by 1.
This commit is contained in:
parent
a2cb6182e8
commit
92692454da
42
httpr.go
42
httpr.go
@ -157,27 +157,29 @@ func New() *Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if tree, ok := rr.tree[r.Method]; ok {
|
tree, ok := rr.tree[r.Method]
|
||||||
path, err := newPath(r.URL.Path)
|
if !ok {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusNotAcceptable)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if handler, params := tree.get(path); handler != nil {
|
|
||||||
if params != nil {
|
|
||||||
r = r.WithContext(context.WithValue(r.Context(), ParamsKey, params))
|
|
||||||
}
|
|
||||||
handler(w, r)
|
|
||||||
} else {
|
|
||||||
if rr.NotFoundHandler != nil {
|
|
||||||
rr.NotFoundHandler(w, r)
|
|
||||||
} else {
|
|
||||||
http.Error(w, "Not Found", http.StatusNotFound)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
path, err := newPath(r.URL.Path)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusNotAcceptable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if handler, params := tree.get(path); handler != nil {
|
||||||
|
if params != nil {
|
||||||
|
r = r.WithContext(context.WithValue(r.Context(), ParamsKey, params))
|
||||||
|
}
|
||||||
|
handler(w, r)
|
||||||
|
} else {
|
||||||
|
if rr.NotFoundHandler != nil {
|
||||||
|
rr.NotFoundHandler(w, r)
|
||||||
|
} else {
|
||||||
|
http.Error(w, "Not Found", http.StatusNotFound)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user