From 5a002e8f9b73256d68798ae1979cdf297bc69939 Mon Sep 17 00:00:00 2001 From: nolwn Date: Sun, 22 Nov 2020 18:31:29 -0800 Subject: [PATCH] code cleanup. --- router.go | 121 ++++++++++++++++++++++++------------------------- router_test.go | 23 +++++++++- 2 files changed, 81 insertions(+), 63 deletions(-) diff --git a/router.go b/router.go index f0ead4a..fcaf98a 100644 --- a/router.go +++ b/router.go @@ -6,18 +6,6 @@ import ( "strings" ) -type route struct { - method string - path string - callback http.HandlerFunc -} - -type segment struct { - path string - methods map[string]http.HandlerFunc - children map[string]*segment -} - // Router is a replacement for the net/http DefaultServerMux. This version includes the // ability to add path parameter in the given path. // @@ -31,11 +19,23 @@ type segment struct { // characters, not including a slash, would match this path. type Router struct { routes []route - lookup *segment + root *segment NotFoundHandler http.Handler } +type route struct { + method string + path string + callback http.HandlerFunc +} + +type segment struct { + path string + methods map[string]http.HandlerFunc + children map[string]*segment +} + // NotFoundHandler is the default function for handling routes that are not found. If you wish to // provide your own handler for this, simply set it on the router. var NotFoundHandler http.Handler = http.HandlerFunc( @@ -45,17 +45,18 @@ var NotFoundHandler http.Handler = http.HandlerFunc( }) // AddRoute registers a new handler function to a path and http.HandlerFunc. If a path and -// method already have a callback registered to them, and error is returned. +// method already have a callback registered to them, an error is returned. func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc) (err error) { keys := setupKeys(strings.Split(path, "/")) - if r.lookup == nil { - r.lookup = &segment{} - r.lookup.children = map[string]*segment{} - r.lookup.methods = map[string]http.HandlerFunc{} - r.lookup.path = "/" + + if r.root == nil { + r.root = &segment{} + r.root.children = map[string]*segment{} + r.root.methods = map[string]http.HandlerFunc{} + r.root.path = "/" } - curr := r.lookup + curr := r.root for i, key := range keys { if i == 0 { @@ -73,29 +74,32 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc) if _, ok := curr.methods[method]; ok { err = errors.New("path already exists") + + return } - if err == nil { - curr.methods[method] = callback - r.routes = append(r.routes, route{method, path, callback}) - } + curr.methods[method] = callback + r.routes = append(r.routes, route{method, path, callback}) return } -// Handler returns the handler to use for the given request, -// consulting r.Method, r.URL.Path. It always returns -// a non-nil handler. +// Get is a convinience method which calls Router.AddRoute with the "GET" method. +func (r *Router) Get(path string, callback http.HandlerFunc) { + r.AddRoute(http.MethodGet, path, callback) +} + +// Handler returns the handler to use for the given request, consulting r.Method, r.URL.Path. It +// always returns a non-nil handler. // -// Handler also returns the registered pattern that matches the -// request. +// Handler also returns the registered pattern that matches the request. // -// If there is no registered handler that applies to the request, -// Handler returns a ``page not found'' handler and an empty pattern. +// If there is no registered handler that applies to the request, Handler returns a ``page not +// found'' handler and an empty pattern. func (r *Router) Handler(req *http.Request) (h http.Handler, pattern string) { method := req.Method path := req.URL.Path - root := r.lookup + root := r.root curr := root segments := strings.Split(path, "/") @@ -125,6 +129,19 @@ func (r *Router) Handler(req *http.Request) (h http.Handler, pattern string) { return } +// ServeHTTP is the function that is required by http.Handler. It takes an http.ResponseWriter which +// it uses to write to a response object that will construct a response for the user. It also takes +// an *http.Request which describes the request the user has made. +// +// 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. +func (r Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { + handler, _ := r.Handler(req) + handler.ServeHTTP(w, req) + + return +} + func addSegment(curr *segment, keys []string) (seg *segment) { for _, key := range keys { if child, ok := curr.children[key]; !ok { @@ -139,35 +156,6 @@ func addSegment(curr *segment, keys []string) (seg *segment) { return } -func setupKeys(slice []string) (clean []string) { - clean = append(clean, "/") - for _, v := range slice { - if v != "" { - clean = append(clean, "/"+v) - } - } - - return -} - -// Get is a convinience method which calls Router.AddRoute with the "GET" method. -func (r *Router) Get(path string, callback http.HandlerFunc) { - r.AddRoute(http.MethodGet, path, callback) -} - -// ServeHTTP is the function that is required by http.Handler. It takes an http.ResponseWriter which -// it uses to write to a response object that will construct a response for the user. It also takes -// an *http.Request which describes the request the user has made. -// -// 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. -func (r Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { - handler, _ := r.Handler(req) - handler.ServeHTTP(w, req) - - return -} - func newSegment(parentPath string, key string) (seg *segment) { var path string if parentPath == "/" { @@ -182,3 +170,14 @@ func newSegment(parentPath string, key string) (seg *segment) { return } + +func setupKeys(slice []string) (keys []string) { + keys = append(keys, "/") + for _, v := range slice { + if v != "" { + keys = append(keys, "/"+v) + } + } + + return +} diff --git a/router_test.go b/router_test.go index b2ddf0b..966a472 100644 --- a/router_test.go +++ b/router_test.go @@ -9,6 +9,8 @@ import ( ) func TestAddRouter(t *testing.T) { + defer testOutcome("can add router", t) + router := Router{} routeCounter := 0 @@ -41,11 +43,11 @@ func TestAddRouter(t *testing.T) { if err != nil { t.Error("The route was not correctly added to the router: ", err) } - - // checkLookup(router.lookup) } func TestServeHTTP(t *testing.T) { + defer testOutcome("can find correct callback function", t) + router := Router{} path := "/items" expectedBody := "I am /items" @@ -60,7 +62,10 @@ func TestServeHTTP(t *testing.T) { if err != nil { t.Error("Did not find the expected callback handler", err) + + return } + } func matchAndCheckRoute(r *Router, method string, path string, expectedBody string, expectedCode int) (err error) { @@ -136,6 +141,20 @@ func addAndCheckRoute(r *Router, method string, path string, callback http.Handl return } +func testOutcome(message string, t *testing.T) { + var status string + + if t.Failed() { + status = "\u001b[31mx\u001b[0m" + } else { + status = "\u001b[32m✓\u001b[0m" + } + + fmt.Printf("%s %s\n", status, message) + + return +} + // checkLookup prints out the various saved routes. It's not needed for any test, but is a helpful debugging tool. // func checkLookup(curr *segment) { // fmt.Printf("%p { path: %s, methods: %v, children: %v}\n", curr, curr.path, curr.methods, curr.children)