code cleanup.
This commit is contained in:
117
router.go
117
router.go
@ -6,18 +6,6 @@ import (
|
|||||||
"strings"
|
"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
|
// Router is a replacement for the net/http DefaultServerMux. This version includes the
|
||||||
// ability to add path parameter in the given path.
|
// 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.
|
// characters, not including a slash, would match this path.
|
||||||
type Router struct {
|
type Router struct {
|
||||||
routes []route
|
routes []route
|
||||||
lookup *segment
|
root *segment
|
||||||
|
|
||||||
NotFoundHandler http.Handler
|
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
|
// 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.
|
// provide your own handler for this, simply set it on the router.
|
||||||
var NotFoundHandler http.Handler = http.HandlerFunc(
|
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
|
// 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) {
|
func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc) (err error) {
|
||||||
keys := setupKeys(strings.Split(path, "/"))
|
keys := setupKeys(strings.Split(path, "/"))
|
||||||
if r.lookup == nil {
|
|
||||||
r.lookup = &segment{}
|
if r.root == nil {
|
||||||
r.lookup.children = map[string]*segment{}
|
r.root = &segment{}
|
||||||
r.lookup.methods = map[string]http.HandlerFunc{}
|
r.root.children = map[string]*segment{}
|
||||||
r.lookup.path = "/"
|
r.root.methods = map[string]http.HandlerFunc{}
|
||||||
|
r.root.path = "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
curr := r.lookup
|
curr := r.root
|
||||||
|
|
||||||
for i, key := range keys {
|
for i, key := range keys {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
@ -73,29 +74,32 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc)
|
|||||||
|
|
||||||
if _, ok := curr.methods[method]; ok {
|
if _, ok := curr.methods[method]; ok {
|
||||||
err = errors.New("path already exists")
|
err = errors.New("path already exists")
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
curr.methods[method] = callback
|
curr.methods[method] = callback
|
||||||
r.routes = append(r.routes, route{method, path, callback})
|
r.routes = append(r.routes, route{method, path, callback})
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler returns the handler to use for the given request,
|
// Get is a convinience method which calls Router.AddRoute with the "GET" method.
|
||||||
// consulting r.Method, r.URL.Path. It always returns
|
func (r *Router) Get(path string, callback http.HandlerFunc) {
|
||||||
// a non-nil handler.
|
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
|
// Handler also returns the registered pattern that matches the request.
|
||||||
// request.
|
|
||||||
//
|
//
|
||||||
// If there is no registered handler that applies to the request,
|
// If there is no registered handler that applies to the request, Handler returns a ``page not
|
||||||
// Handler returns a ``page not found'' handler and an empty pattern.
|
// found'' handler and an empty pattern.
|
||||||
func (r *Router) Handler(req *http.Request) (h http.Handler, pattern string) {
|
func (r *Router) Handler(req *http.Request) (h http.Handler, pattern string) {
|
||||||
method := req.Method
|
method := req.Method
|
||||||
path := req.URL.Path
|
path := req.URL.Path
|
||||||
root := r.lookup
|
root := r.root
|
||||||
curr := root
|
curr := root
|
||||||
|
|
||||||
segments := strings.Split(path, "/")
|
segments := strings.Split(path, "/")
|
||||||
@ -125,6 +129,19 @@ func (r *Router) Handler(req *http.Request) (h http.Handler, pattern string) {
|
|||||||
return
|
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) {
|
func addSegment(curr *segment, keys []string) (seg *segment) {
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
if child, ok := curr.children[key]; !ok {
|
if child, ok := curr.children[key]; !ok {
|
||||||
@ -139,35 +156,6 @@ func addSegment(curr *segment, keys []string) (seg *segment) {
|
|||||||
return
|
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) {
|
func newSegment(parentPath string, key string) (seg *segment) {
|
||||||
var path string
|
var path string
|
||||||
if parentPath == "/" {
|
if parentPath == "/" {
|
||||||
@ -182,3 +170,14 @@ func newSegment(parentPath string, key string) (seg *segment) {
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupKeys(slice []string) (keys []string) {
|
||||||
|
keys = append(keys, "/")
|
||||||
|
for _, v := range slice {
|
||||||
|
if v != "" {
|
||||||
|
keys = append(keys, "/"+v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
@ -9,6 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestAddRouter(t *testing.T) {
|
func TestAddRouter(t *testing.T) {
|
||||||
|
defer testOutcome("can add router", t)
|
||||||
|
|
||||||
router := Router{}
|
router := Router{}
|
||||||
routeCounter := 0
|
routeCounter := 0
|
||||||
|
|
||||||
@ -41,11 +43,11 @@ func TestAddRouter(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("The route was not correctly added to the router: ", err)
|
t.Error("The route was not correctly added to the router: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkLookup(router.lookup)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServeHTTP(t *testing.T) {
|
func TestServeHTTP(t *testing.T) {
|
||||||
|
defer testOutcome("can find correct callback function", t)
|
||||||
|
|
||||||
router := Router{}
|
router := Router{}
|
||||||
path := "/items"
|
path := "/items"
|
||||||
expectedBody := "I am /items"
|
expectedBody := "I am /items"
|
||||||
@ -60,7 +62,10 @@ func TestServeHTTP(t *testing.T) {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Did not find the expected callback handler", err)
|
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) {
|
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
|
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.
|
// 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)
|
||||||
|
Reference in New Issue
Block a user