Add PathParams method.
This commit is contained in:
151
router.go
151
router.go
@ -23,18 +23,30 @@ type Router struct {
|
|||||||
routes []route
|
routes []route
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// endpoint is comes at the end of each valid path in the tree. It contains the information you
|
||||||
|
// need to call the endpoint, including path parameter names.
|
||||||
|
type endpoint struct {
|
||||||
|
callback http.HandlerFunc
|
||||||
|
path string
|
||||||
|
pathParams []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// route is not part of the tree, but is saved on the router to represent all the available
|
||||||
|
// routes in the tree.
|
||||||
type route struct {
|
type route struct {
|
||||||
callback http.HandlerFunc
|
callback http.HandlerFunc
|
||||||
method string
|
method string
|
||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// segment is a tree node. It can have children, or endpoints, or both attached to it. It also
|
||||||
|
// has a special child called "parameter" which represents a path parameter. If a route string
|
||||||
|
// doesn't match any of the children, and there is a parameter child present, it will match that
|
||||||
|
// parameter child.
|
||||||
type segment struct {
|
type segment struct {
|
||||||
children map[string]*segment
|
children map[string]*segment
|
||||||
methods map[string]http.HandlerFunc
|
endpoints map[string]*endpoint
|
||||||
path string
|
|
||||||
parameter *segment
|
parameter *segment
|
||||||
parameterName string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@ -49,12 +61,12 @@ var NotFoundHandler http.Handler = http.HandlerFunc(
|
|||||||
// method already have a callback registered to them, an 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, "/"))
|
||||||
|
pathParams := []string{}
|
||||||
|
|
||||||
if r.root == nil {
|
if r.root == nil {
|
||||||
r.root = &segment{}
|
r.root = &segment{}
|
||||||
r.root.children = map[string]*segment{}
|
r.root.children = map[string]*segment{}
|
||||||
r.root.methods = map[string]http.HandlerFunc{}
|
r.root.endpoints = map[string]*endpoint{}
|
||||||
r.root.path = "/"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
curr := r.root
|
curr := r.root
|
||||||
@ -64,7 +76,12 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc)
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if child, ok := curr.children[key]; !ok {
|
if isParameter(key) {
|
||||||
|
pathParams = append(pathParams, key[2:])
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if child, _ := getChild(key, curr); child == nil {
|
||||||
seg := addSegment(curr, key)
|
seg := addSegment(curr, key)
|
||||||
curr = seg
|
curr = seg
|
||||||
} else {
|
} else {
|
||||||
@ -72,18 +89,43 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := curr.methods[method]; ok {
|
if _, ok := curr.endpoints[method]; ok {
|
||||||
err = errors.New("path already exists")
|
err = errors.New("path already exists")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
curr.methods[method] = callback
|
curr.endpoints[method] = &endpoint{callback, path, pathParams}
|
||||||
r.routes = append(r.routes, route{callback, method, path})
|
r.routes = append(r.routes, route{callback, method, path})
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PathParams takes a path and returns the values for any path parameters
|
||||||
|
// in the path.
|
||||||
|
func (r *Router) PathParams(method string, reqPath string) (params map[string]string, err error) {
|
||||||
|
end, err := r.getEndpoint(method, reqPath)
|
||||||
|
path := end.path
|
||||||
|
|
||||||
|
reqParts := strings.Split(reqPath, "/")
|
||||||
|
reqKeys := setupKeys(reqParts)
|
||||||
|
|
||||||
|
pathParts := strings.Split(path, "/")
|
||||||
|
pathKeys := setupKeys(pathParts)
|
||||||
|
|
||||||
|
params = map[string]string{}
|
||||||
|
|
||||||
|
for i, pathKey := range pathKeys {
|
||||||
|
if isParameter(pathKey) {
|
||||||
|
name := pathKey[2:]
|
||||||
|
value := reqKeys[i][1:]
|
||||||
|
params[name] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Get is a convinience method which calls Router.AddRoute with the "GET" method.
|
// Get is a convinience method which calls Router.AddRoute with the "GET" method.
|
||||||
func (r *Router) Get(path string, callback http.HandlerFunc) {
|
func (r *Router) Get(path string, callback http.HandlerFunc) {
|
||||||
r.AddRoute(http.MethodGet, path, callback)
|
r.AddRoute(http.MethodGet, path, callback)
|
||||||
@ -99,33 +141,16 @@ func (r *Router) Get(path string, callback http.HandlerFunc) {
|
|||||||
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.root
|
|
||||||
curr := root
|
|
||||||
|
|
||||||
segments := strings.Split(path, "/")
|
|
||||||
keys := setupKeys(segments)
|
|
||||||
|
|
||||||
if r.NotFoundHandler == nil {
|
if r.NotFoundHandler == nil {
|
||||||
h = NotFoundHandler
|
h = NotFoundHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range keys {
|
endpoint, err := r.getEndpoint(method, path)
|
||||||
if v == "/" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
seg := getChild(v, curr)
|
if err == nil {
|
||||||
|
h = endpoint.callback
|
||||||
if seg == nil {
|
pattern = endpoint.path
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
curr = seg
|
|
||||||
}
|
|
||||||
|
|
||||||
if cb, ok := curr.methods[method]; ok {
|
|
||||||
h = cb
|
|
||||||
pattern = curr.path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@ -137,25 +162,26 @@ func (r *Router) Handler(req *http.Request) (h http.Handler, pattern string) {
|
|||||||
//
|
//
|
||||||
// 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) {
|
||||||
handler, _ := r.Handler(req)
|
handler, _ := r.Handler(req)
|
||||||
handler.ServeHTTP(w, req)
|
handler.ServeHTTP(w, req)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// addSegment create a new segment either as a child or as a parameter depending on whether the key
|
||||||
|
// qualifies as a parameter. A pointer to the created segment is then returned.
|
||||||
func addSegment(curr *segment, key string) (seg *segment) {
|
func addSegment(curr *segment, key string) (seg *segment) {
|
||||||
if curr.parameterName == key {
|
if curr.parameter != nil {
|
||||||
seg = curr.parameter
|
seg = curr.parameter
|
||||||
|
|
||||||
} else if child, ok := curr.children[key]; !ok { // child does not match...
|
} else if child, ok := curr.children[key]; !ok { // child does not match...
|
||||||
var isParam bool
|
var isParam bool
|
||||||
|
|
||||||
seg, isParam = newSegment(curr.path, key)
|
seg, isParam = newSegment(key)
|
||||||
|
|
||||||
if isParam {
|
if isParam {
|
||||||
curr.parameter = seg
|
curr.parameter = seg
|
||||||
curr.parameterName = key[2:]
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
curr.children[key] = seg
|
curr.children[key] = seg
|
||||||
@ -170,33 +196,60 @@ func addSegment(curr *segment, key string) (seg *segment) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getChild(key string, curr *segment) (child *segment) {
|
// getChild takes a path part and finds the appropriate segment child for it. If it is an exact match to a
|
||||||
|
// child on the segment, then that child segment is returned. If it is not a match, then the parameter child
|
||||||
|
// is returned. If there is no parameter child, nil is returned. isParam is true if the parameter child is
|
||||||
|
// being returned.
|
||||||
|
func getChild(key string, curr *segment) (child *segment, isParam bool) {
|
||||||
if seg, ok := curr.children[key]; ok { // is there an exact match?
|
if seg, ok := curr.children[key]; ok { // is there an exact match?
|
||||||
|
|
||||||
child = seg
|
child = seg
|
||||||
|
|
||||||
} else if curr.parameter != nil { // could this be a parameter?
|
} else if curr.parameter != nil { // could this be a parameter?
|
||||||
child = curr.parameter
|
child = curr.parameter
|
||||||
|
isParam = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSegment(parentPath string, key string) (seg *segment, isParam bool) {
|
// getEndpoint takes a path and traverses the tree until it finds the endpoint associated with that path.
|
||||||
var path string
|
// If no endpoint if found, an error is returned.
|
||||||
|
func (r *Router) getEndpoint(method string, path string) (end *endpoint, err error) {
|
||||||
|
curr := r.root
|
||||||
|
segments := strings.Split(path, "/")
|
||||||
|
keys := setupKeys(segments)
|
||||||
|
|
||||||
if parentPath == "/" {
|
for _, v := range keys {
|
||||||
path = key
|
if v == "/" {
|
||||||
|
continue
|
||||||
} else {
|
|
||||||
path = parentPath + key
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
seg, _ := getChild(v, curr)
|
||||||
|
|
||||||
|
if seg == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
curr = seg
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := curr.endpoints[method]; !ok {
|
||||||
|
err = errors.New("route not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
end = curr.endpoints[method]
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: refactor out newSegment as it's not longer needed.
|
||||||
|
|
||||||
|
// newSegment constructs a new, empty segment and reports back if the key is a parameter.
|
||||||
|
func newSegment(key string) (seg *segment, isParam bool) {
|
||||||
seg = &segment{}
|
seg = &segment{}
|
||||||
|
|
||||||
seg.children = map[string]*segment{}
|
seg.children = map[string]*segment{}
|
||||||
seg.methods = map[string]http.HandlerFunc{}
|
seg.endpoints = map[string]*endpoint{}
|
||||||
seg.path = path
|
|
||||||
|
|
||||||
if isParameter(key) {
|
if isParameter(key) {
|
||||||
isParam = true
|
isParam = true
|
||||||
@ -205,6 +258,8 @@ func newSegment(parentPath string, key string) (seg *segment, isParam bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setupKeys takes an array of strings representing the parts of a path, and returns a new slice
|
||||||
|
// made up of the parts with "/" prepended to each.
|
||||||
func setupKeys(slice []string) (keys []string) {
|
func setupKeys(slice []string) (keys []string) {
|
||||||
keys = append(keys, "/")
|
keys = append(keys, "/")
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
@ -217,8 +272,8 @@ func setupKeys(slice []string) (keys []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// isParameter returns true if the key is more than one character long and starts with a ':'
|
// isParameter returns true if the key is more than one character long and starts with a ':'
|
||||||
func isParameter(key string) (isVar bool) {
|
func isParameter(key string) (isParam bool) {
|
||||||
if len([]rune(key)) <= 2 {
|
if len([]rune(key)) <= 1 {
|
||||||
return // avoid empty variables, i.e. /somepath/:/someotherpath
|
return // avoid empty variables, i.e. /somepath/:/someotherpath
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,7 +281,7 @@ func isParameter(key string) (isVar bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
isVar = true
|
isParam = true
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
var testLevel = 1
|
var testLevel = 1
|
||||||
|
|
||||||
func TestAddRouter(t *testing.T) {
|
func TestAddRouter(t *testing.T) {
|
||||||
describeTests("Test AddRouter function")
|
describeTests("Test AddRouter method")
|
||||||
|
|
||||||
router := Router{}
|
router := Router{}
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ func TestAddRouter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestServeHTTP(t *testing.T) {
|
func TestServeHTTP(t *testing.T) {
|
||||||
describeTests("Test ServeHTTP function")
|
describeTests("Test ServeHTTP method")
|
||||||
|
|
||||||
router := Router{}
|
router := Router{}
|
||||||
|
|
||||||
@ -31,6 +31,14 @@ func TestServeHTTP(t *testing.T) {
|
|||||||
testMatchesPathParam(router, t)
|
testMatchesPathParam(router, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPathParams(t *testing.T) {
|
||||||
|
describeTests("Test PathParams method")
|
||||||
|
|
||||||
|
router := Router{}
|
||||||
|
|
||||||
|
testParamValues(router, t)
|
||||||
|
}
|
||||||
|
|
||||||
func addAndCheckRoute(r *Router, method string, path string, callback http.HandlerFunc) (err error) {
|
func addAndCheckRoute(r *Router, method string, path string, callback http.HandlerFunc) (err error) {
|
||||||
routeCount := len(r.routes)
|
routeCount := len(r.routes)
|
||||||
|
|
||||||
@ -71,7 +79,7 @@ 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, parameter: %v, parameterName: \"%s\"}\n", curr, curr.path, curr.methods, curr.children, curr.parameter, curr.parameterName)
|
fmt.Printf("%p { methods: %v, children: %v, parameter: %v\"}\n", curr, curr.endpoints, curr.children, curr.parameter)
|
||||||
|
|
||||||
for _, v := range curr.children {
|
for _, v := range curr.children {
|
||||||
checkLookup(v)
|
checkLookup(v)
|
||||||
@ -226,6 +234,52 @@ func testMatchesRoot(router Router, t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testParamValues(router Router, t *testing.T) {
|
||||||
|
defer testOutcome("returns param names and values", t)
|
||||||
|
|
||||||
|
method := http.MethodOptions
|
||||||
|
path := "/users/:userID/edit/:status"
|
||||||
|
userID := "46"
|
||||||
|
status := "inactive"
|
||||||
|
expectedBody := "done"
|
||||||
|
expectedStatus := 200
|
||||||
|
reqPath := fmt.Sprintf("/users/%s/edit/%s", userID, status)
|
||||||
|
|
||||||
|
router.AddRoute(method, path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
requestPath := r.URL.Path
|
||||||
|
requestMethod := r.Method
|
||||||
|
|
||||||
|
params, err := router.PathParams(requestMethod, requestPath)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error("An error occurred while getting path parameters")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(params) != 2 {
|
||||||
|
t.Errorf("Received the wrong number of parameters. Expected 2, recieved %d", len(params))
|
||||||
|
}
|
||||||
|
|
||||||
|
if params["userID"] != userID {
|
||||||
|
t.Errorf("userID should be %s, but it is %s", userID, params["userID"])
|
||||||
|
}
|
||||||
|
|
||||||
|
if params["status"] != status {
|
||||||
|
t.Errorf("status should be %s, but it is %s", status, params["status"])
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(expectedStatus)
|
||||||
|
w.Write([]byte(expectedBody))
|
||||||
|
})
|
||||||
|
|
||||||
|
err := matchAndCheckRoute(&router, method, reqPath, expectedBody, expectedStatus)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Did not find the expected callback handler", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func testOutcome(message string, t *testing.T) {
|
func testOutcome(message string, t *testing.T) {
|
||||||
var status string
|
var status string
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user