Add message to 404 Handler, add logging messages.
This commit is contained in:
16
router.go
16
router.go
@ -2,6 +2,7 @@ package router
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -41,6 +42,7 @@ type Router struct {
|
|||||||
var NotFoundHandler http.Handler = http.HandlerFunc(
|
var NotFoundHandler http.Handler = http.HandlerFunc(
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
|
w.Write([]byte("Not Found."))
|
||||||
})
|
})
|
||||||
|
|
||||||
// AddRoute registers a new handler function to a path and http.HandlerFunc. If a path and
|
// AddRoute registers a new handler function to a path and http.HandlerFunc. If a path and
|
||||||
@ -79,9 +81,19 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc)
|
|||||||
r.routes = append(r.routes, route{method, path, callback})
|
r.routes = append(r.routes, route{method, path, callback})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkLookup(r.lookup)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkLookup(curr *segment) {
|
||||||
|
fmt.Printf("%p { path: %s, methods: %v, children: %v}\n", curr, curr.path, curr.methods, curr.children)
|
||||||
|
|
||||||
|
for _, v := range curr.children {
|
||||||
|
checkLookup(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Handler returns the handler to use for the given request,
|
// Handler returns the handler to use for the given request,
|
||||||
// consulting r.Method, r.URL.Path. It always returns
|
// consulting r.Method, r.URL.Path. It always returns
|
||||||
// a non-nil handler.
|
// a non-nil handler.
|
||||||
@ -97,6 +109,8 @@ func (r *Router) Handler(req *http.Request) (h http.Handler, pattern string) {
|
|||||||
root := r.lookup
|
root := r.lookup
|
||||||
curr := root
|
curr := root
|
||||||
|
|
||||||
|
fmt.Print("The route handler has been called.")
|
||||||
|
|
||||||
segments := strings.Split(path, "/")
|
segments := strings.Split(path, "/")
|
||||||
keys := setupKeys(segments)
|
keys := setupKeys(segments)
|
||||||
|
|
||||||
@ -161,6 +175,8 @@ func (r *Router) Get(path string, callback http.HandlerFunc) {
|
|||||||
// In the case of this router, all it needs to do is lookup the Handler that has been saved at a given
|
// In the case of this router, all it needs to do is lookup the Handler that has been saved at a given
|
||||||
// path and then call its ServeHTTP.
|
// path and then call its ServeHTTP.
|
||||||
func (r Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (r Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
fmt.Print("Router's ServeHTTP method has been called.")
|
||||||
|
|
||||||
handler, _ := r.Handler(req)
|
handler, _ := r.Handler(req)
|
||||||
handler.ServeHTTP(w, req)
|
handler.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
@ -137,10 +137,10 @@ func addAndCheckRoute(r *Router, method string, path string, callback http.Handl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checkLookup prints out the various saved routes. It's not needed for any test, but is a helpful debugging tool.
|
// checkLookup prints out the various saved routes. It's not needed for any test, but is a helpful debugging tool.
|
||||||
func checkLookup(curr *segment) {
|
// func checkLookup(curr *segment) {
|
||||||
fmt.Printf("%p { path: %s, methods: %v, children: %v}\n", curr, curr.path, curr.methods, curr.children)
|
// fmt.Printf("%p { path: %s, methods: %v, children: %v}\n", curr, curr.path, curr.methods, curr.children)
|
||||||
|
|
||||||
for _, v := range curr.children {
|
// for _, v := range curr.children {
|
||||||
checkLookup(v)
|
// checkLookup(v)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
Reference in New Issue
Block a user