You are looking at historical revision 16101 of this page. It may differ significantly from its current revision.
uri-match
Description
A URI path (route) matching library which provides means for flexible and RESTful route definition.
Author
Requirements
Requires the uri-common egg.
Documentation
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.
Example:
(make-routes '((get "this!") ("/bar" (get "and this!") (post "also this")))) => ((post ("/bar" "also this")) (get ("/bar" "and this!") ("/" "this!")))
uri-match
[procedure] (uri-match method uri routes)Matches a given HTTP method (which must be given as a lower-case symbol) and the uri's 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.
Example:
((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.
Example:
(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!"