Add PathParams method.
This commit is contained in:
153
router.go
153
router.go
@ -23,18 +23,30 @@ type Router struct {
|
||||
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 {
|
||||
callback http.HandlerFunc
|
||||
method 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 {
|
||||
children map[string]*segment
|
||||
methods map[string]http.HandlerFunc
|
||||
path string
|
||||
parameter *segment
|
||||
parameterName string
|
||||
children map[string]*segment
|
||||
endpoints map[string]*endpoint
|
||||
parameter *segment
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc) (err error) {
|
||||
keys := setupKeys(strings.Split(path, "/"))
|
||||
pathParams := []string{}
|
||||
|
||||
if r.root == nil {
|
||||
r.root = &segment{}
|
||||
r.root.children = map[string]*segment{}
|
||||
r.root.methods = map[string]http.HandlerFunc{}
|
||||
r.root.path = "/"
|
||||
r.root.endpoints = map[string]*endpoint{}
|
||||
}
|
||||
|
||||
curr := r.root
|
||||
@ -64,7 +76,12 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc)
|
||||
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)
|
||||
curr = seg
|
||||
} 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")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
curr.methods[method] = callback
|
||||
curr.endpoints[method] = &endpoint{callback, path, pathParams}
|
||||
r.routes = append(r.routes, route{callback, method, path})
|
||||
|
||||
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.
|
||||
func (r *Router) Get(path string, callback http.HandlerFunc) {
|
||||
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) {
|
||||
method := req.Method
|
||||
path := req.URL.Path
|
||||
root := r.root
|
||||
curr := root
|
||||
|
||||
segments := strings.Split(path, "/")
|
||||
keys := setupKeys(segments)
|
||||
|
||||
if r.NotFoundHandler == nil {
|
||||
h = NotFoundHandler
|
||||
}
|
||||
|
||||
for _, v := range keys {
|
||||
if v == "/" {
|
||||
continue
|
||||
}
|
||||
endpoint, err := r.getEndpoint(method, path)
|
||||
|
||||
seg := getChild(v, curr)
|
||||
|
||||
if seg == nil {
|
||||
return
|
||||
}
|
||||
|
||||
curr = seg
|
||||
}
|
||||
|
||||
if cb, ok := curr.methods[method]; ok {
|
||||
h = cb
|
||||
pattern = curr.path
|
||||
if err == nil {
|
||||
h = endpoint.callback
|
||||
pattern = endpoint.path
|
||||
}
|
||||
|
||||
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
|
||||
// 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.ServeHTTP(w, req)
|
||||
|
||||
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) {
|
||||
if curr.parameterName == key {
|
||||
if curr.parameter != nil {
|
||||
seg = curr.parameter
|
||||
|
||||
} else if child, ok := curr.children[key]; !ok { // child does not match...
|
||||
var isParam bool
|
||||
|
||||
seg, isParam = newSegment(curr.path, key)
|
||||
seg, isParam = newSegment(key)
|
||||
|
||||
if isParam {
|
||||
curr.parameter = seg
|
||||
curr.parameterName = key[2:]
|
||||
|
||||
} else {
|
||||
curr.children[key] = seg
|
||||
@ -170,33 +196,60 @@ func addSegment(curr *segment, key string) (seg *segment) {
|
||||
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?
|
||||
|
||||
child = seg
|
||||
|
||||
} else if curr.parameter != nil { // could this be a parameter?
|
||||
child = curr.parameter
|
||||
|
||||
isParam = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func newSegment(parentPath string, key string) (seg *segment, isParam bool) {
|
||||
var path string
|
||||
// getEndpoint takes a path and traverses the tree until it finds the endpoint associated with that path.
|
||||
// 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 == "/" {
|
||||
path = key
|
||||
for _, v := range keys {
|
||||
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.children = map[string]*segment{}
|
||||
seg.methods = map[string]http.HandlerFunc{}
|
||||
seg.path = path
|
||||
seg.endpoints = map[string]*endpoint{}
|
||||
|
||||
if isParameter(key) {
|
||||
isParam = true
|
||||
@ -205,6 +258,8 @@ func newSegment(parentPath string, key string) (seg *segment, isParam bool) {
|
||||
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) {
|
||||
keys = append(keys, "/")
|
||||
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 ':'
|
||||
func isParameter(key string) (isVar bool) {
|
||||
if len([]rune(key)) <= 2 {
|
||||
func isParameter(key string) (isParam bool) {
|
||||
if len([]rune(key)) <= 1 {
|
||||
return // avoid empty variables, i.e. /somepath/:/someotherpath
|
||||
}
|
||||
|
||||
@ -226,7 +281,7 @@ func isParameter(key string) (isVar bool) {
|
||||
return
|
||||
}
|
||||
|
||||
isVar = true
|
||||
isParam = true
|
||||
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user