The route resolution algorithm should be fairly clear. It goes like this:
- Remove query params, attach to the request object (or somewhere accessible for later)
- Break a route down by slashes, you now have a list of components
- Add into a tree, treating
*
and:param
as wild cards ex.get("/a/b")
andget(":id")
should make a tree like this
root (/)
|\
| \
a *
|
|
b
- Split the incoming route in the same way as during construction
- Set
current_node = root
- If
split.get(0)
does not exist, you're done, return the accumulated middleware - Check if the first piece (
split.get(0)
) matchescurrent_node
- If it does
- Set
current_node = child
- Accumulate any middleware attached to that node.
- Set
- If it does not
- Check if there is a wildcard node, if so then
current_node = current_node.wildcard
- If no wildcard, then the route isn't found, call the 404 handler.
- Check if there is a wildcard node, if so then
- If it does
- Remove the first piece of the split (or increment index)
- Go back to 3