The library allows the creation of two-way mappings between permanent URLs and request-handling procedures.
This library was inspired by the (planet untyped/dispatch) package.
Suppose you are writing a blog application and want pretty URLs for different views of the site. You would define some URL dispatching rules as follows:
[("")list-posts]
> (blog-dispatch(url->request"http://www.chrlsnchrg.com"))'(list-posts)
> (blog-dispatch(url->request"http://www.chrlsnchrg.com/"))'(list-posts)
> (blog-dispatch(url->request"http://www.chrlsnchrg.com/posts/Extracurricular-Activity"))'(review-post "Extracurricular-Activity")
> (blog-dispatch(url->request"http://www.chrlsnchrg.com/archive/1984/10"))'(review-archive 1984 10)
> (blog-dispatch(url->request"http://www.chrlsnchrg.com/contact"))'(list-posts)
> (blog-urllist-posts)"/"
> (blog-urlreview-post"Another-Saturday-Night")"/posts/Another-Saturday-Night"
> (blog-urlreview-archive198411)"/archive/1984/11"
> (sum-dispatch(url->request"http://www.sum.com/"))0
> (sum-dispatch(url->request"http://www.sum.com/2"))2
> (sum-dispatch(url->request"http://www.sum.com/2/3/4"))9
> (sum-dispatch(url->request"http://www.sum.com/5/10/15/20"))50
"/"
"/1"
"/2/3/5/7"
When you use web-server/dispatch with serve/servlet , you almost always want to use the #:servlet-regexp argument with the value "" to capture all top-level requests. However, make sure you don’t include an else in your rules if you are also serving static files, or else the filesystem server will never see the requests.
syntax
dispatch-clause...maybe-else-clause)dispatch-clause = [dispatch-patternmaybe-methoddispatch-fun]dispatch-pattern = ()| (string. dispatch-pattern)| (bidi-match-expander.... dispatch-pattern)| (bidi-match-expander. dispatch-pattern)maybe-method =| #:methodmethodmethod = patmaybe-else-clause =
If else-fun is left out, one is provided that calls (next-dispatcher ) to signal to the Web Server that this dispatcher does not apply.
The method syntax is used in a match expression to match the request-method part of the incoming request object. However, since HTTP allows methods to use any case, the byte string from request-method is normalized to a lower-case string. Thus, valid patterns are things like: "get", "post", "head", (or "get""post"), etc.
If method is left out, it assumed to apply to requests without methods and GET methods.
syntax
dispatch-clause...maybe-else-clause)
syntax
dispatch-clause...maybe-else-clause)
syntax
[dispatch-patterndispatch-fun]...)
procedure
( serve/dispatch dispatch)→void
dispatch-rules is purely functional. This presents a more declarative interface, but inhibits some programming and modularity patterns. Containers provide an imperative overlay atop dispatch-rules .
procedure
( container? x)→boolean?
x:any/c
syntax
( define-container container-id(dispatch-idurl-id))
syntax
( dispatch-rules! container-expr[dispatch-patterndispatch-fun]...)
web-server/dispatch builds in a few useful URL component patterns.
syntax
( number-arg )
syntax
( integer-arg )
syntax
( real-arg )
syntax
( string-arg )
syntax
( symbol-arg )
You can create new URL component patterns by defining bi-directional match expanders.
syntax
( define-bidi-match-expander idin-xformout-xform)
Both in-xform and out-xform should use the syntax (xformarg... id) where the args are specific to id and compatible with both in-xform and out-xform. id will typically be provided automatically by dispatch-rules .
syntax
When defining new patterns, you may find it useful to use these helper functions:
syntax
( define-coercion-match-expander idtest?coerce)
> (string->number?"1")#t
> (string->number?"1.2")#t
> (string->number?"+inf.0")#t
> (string->number?"one")#f