[racket] Cascading dispatch rules

From: André Matheus (amatheus at mac.com)
Date: Tue Jan 20 10:27:50 EST 2015

Hi,

I'm currently learning racket, and started developing a web application.
When using dispatch-rules, my first thought was to create separate dispatch
rules and then call them from a top dispatch. Something like this:

(dispatch-rules
   (("admin") admin-dispatch)
   (("api") api-dispatch)
   ... other rules)

(define-values (admin-dispatch admin-url)
  ("" admin-index)
  ...)

(define-values (api-dispatch api-url)
  ("" api-index)
  ...)

If I understand correctly there's no simple direct way to do this. I managed to do it the
following way:

- defining the top level dispatch with string-arg's to catch all requests,
and wrapping the dispatch function to use only the first argument (the request)
(define-values (top-dispatch top-url)
  (dispatch-rules
    (("admin") (string-arg) ...)  (lambda args (admin-dispatch (first args))))))

- defining the other dispatch functions with the sub-path repeated
(define-values (admin-dispatch admin-url)
  (dispatch-rules
    (("admin" "posts") admin-posts)))

My questions:

- is there an existing way to create this kind of cascading or nested dispatch rules?
- is there a parameter matcher to match anything that I could use instead of
   (("path") (string-arg) ...)  (lambda args (path-dispatch (first args))))
or some better way to match everything starting with "path"?

If there is no way I'm thinking of creating a macro to simplify, so that I don't need
wrapping the dispatch functions, specifying parameters and then discarding and so on.

Thanks,

André
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20150120/3684c031/attachment.html>

Posted on the users mailing list.