You are looking at historical revision 17782 of this page. It may differ significantly from its current revision.



A URI path (or route) matching library which provides means for flexible and RESTful route definition.


Moritz Heidkamp


Requires the uri-common egg.


Routes Format

Route defintions must adhere to the following grammar:

   <routes>           ::= ((<path>  [<action> | <routes>] ...) ...)
   <path>             ::= (/ <fragment-matcher> ...)
   <fragment-matcher> ::= {{regexp}} | {{sre}}
   <action>           ::= (<method> <body>)
   <method>           ::= GET | POST | PUT | DELETE | OPTIONS | HEAD
   <body>             ::= {{atom}} | {{handler-procedure}}


[procedure] (make-routes routes #!optional (path ""))

Accepts routes list in the format described under Routes Format and returns them in a format which can be passed to uri-match.

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")))))


[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 of the matching route.


((uri-match 'GET "/foo/42"
	    (make-routes `(((/ "foo" "(\\d+)")
			    (GET ,(lambda (n) (format "You got foo number ~A" n))))))))

=> "You got foo number 42"


[procedure] (make-uri-matcher routes)

Accepts a routes list in the format described under Routes Format 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" (submatch (+ any)) (submatch (+ any)))
				      (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!"