You are looking at historical revision 16321 of this page. It may differ significantly from its current revision.
A URI path (route) matching library which provides means for flexible and RESTful route definition.
Requires the uri-common egg.
make-routes[procedure] (make-routes routes #!optional (path ""))
Accepts routes in the form of nested lists and returns them in a flattened form for use with uri-match. If the car of the sublists is a string, it is treated as a path fragment regex. Its cdr is treated as subroutes to the one given in the car. If the car of the sublists is a symbol, it is treated as an HTTP method and its cadr is treated as the match result (or HTTP body). It can either be a string or a function whose arity must equal to the amount of capture groups within the expanded route.
The optional path is mainly used internally but may be given as a global path prefix.
(make-routes '(((/ "") (GET "this!") ((/ "bar") (GET "and this!") (POST "also this")))))
uri-match[procedure] (uri-match method uri routes)
Matches a given HTTP method (which should be given as an upper-case symbol to comply with intarweb) and the uri's path (which must either be an uri-reference with an uri-path or a string representing a path) in routes (which must be a list of the format returned by make-routes) and returns a thunk which, when invoked, returns the body of the first matching route, #f otherwise. If the body is a procedure, it is applied to the possibly found capture groups.
((uri-match 'GET "/foo/42" `((GET ((/ "foo" "(\\d+)") ,(lambda (n) (format "You got foo number ~A" n))))))) => "You got foo number 42"
make-uri-matcher[procedure] (make-uri-matcher routes)
Accepts a routes list in the same format as make-routes accepts and returns a procedure of two arguments (method and uri like uri-match) for matching against it.
(define match (make-uri-matcher `(((/ "") (GET "this is the root path!") ((/ "some") ((/ "nested") (GET "I'm nested!") ((/ "route" "(.+)" "(.+)") (GET ,(lambda (x y) (format "I am the ~A and ~A!" x y)))))))))) ((match 'GET "/")) => "this is the root path!" ((match 'GET (uri-reference "http://localhost/some/nested/route/alpha/omega"))) => "I am the alpha and omega!"