[racket] passing a list of dispatch clauses to dispatch-rules

From: Janos Tobias Locsei (jtlocsei at cantab.net)
Date: Tue Dec 17 08:47:19 EST 2013

OK, here come some more silly questions...

I tried using (string-arg) and (next-dispatcher) like this:

(define-values (blog-dispatch blog-url)
  (dispatch-rules
   [((string-arg)) *first-request-handler*]
   [("") list-posts]
   [("posts" (string-arg)) review-post]
   [("archive" (integer-arg) (integer-arg)) review-archive]
   [else list-posts]))

(define (*first-request-handler* req a-string) (*next-dispatcher*))
(define (list-posts req) `(list-posts))
(define (review-post req p) `(review-post ,p))
(define (review-archive req y m) `(review-archive ,y ,m))

But when next-dispatcher runs it gives rise to an uncaught exception. So, I
guess what's going on is that (next-dispatcher) exits the blog-dispatch
call entirely and looks for another dispatcher, rather than just the next
matching clause in the dispatch rules of blog-dispatch.

So then I thought what I need must be the dispatch sequencer. So I tried
this:

(require web-server/dispatchers/dispatch-sequencer)

(define-values (first-dispatcher first-url-gen)
  (dispatch-rules
   [((string-arg)) first-request-handler]))

(define-values (second-dispatcher second-url-gen)
  (dispatch-rules
   [("") list-posts]
   [("posts" (string-arg)) review-post]
   [("archive" (integer-arg) (integer-arg)) review-archive]
   [else list-posts]))

(define master-dispatcher
  (make first-dispatcher
        second-dispatcher))

But this raises a contract violation, because "make" expects dispatchers as
its arguments, and first-dispatcher and second-dispatcher aren't truly
dispatchers. A dispatcher takes a connection and a request as arguments,
whereas first-dispatcher and second-dispatcher only take a request as their
argument.

So, I need to do something to build first-dispatcher and second-dispatcher
into proper dispatchers, but I'm not sure what. The
web-server/dispatchers/dispatch-lift module looks like it might do the
trick but then I get a conflicting definition of "make".

Any help much appreciated!

This thread from September looks like it's addressing a similar sort of
issue:
http://lists.racket-lang.org/users/archive/2013-September/059728.html
... but I still don't understand how to construct a "next dispatcher" that
(next-dispatcher) will call.

Tobias




On 16 December 2013 22:44, Jay McCarthy <jay.mccarthy at gmail.com> wrote:

> On Mon, Dec 16, 2013 at 3:25 PM, Janos Tobias Locsei
> <jtlocsei at cantab.net> wrote:
> > Hi, I'm a newbie Racketeer so apologies if this is a silly question:
> >
> > Is it possible to pass a list of dispatch-clause to dispatch-rules? Naive
> > use of "apply" doesn't work:
> >
> > (define-values (blog-dispatch blog-url)
> >   (let ([arglist
> >          (list
> >             [("") list-posts]
> >             [("posts" (string-arg)) review-post]
> >             [("archive" (integer-arg) (integer-arg)) review-archive]
> >             [else list-posts])])
> >     (apply dispatch-rules arglist)))
>
> dispatch-rules is a macro, so it cannot be "apply"d
>
> > (example modified from docs.racket-lang.org/web-server/dispatch.html)
> This
> > code gives rise to the error
> >
> > string-arg: this match expander must be used inside match in:
> (string-arg)
>
> This error happens before you try to apply, beucase (string-arg) can
> only be used inside of a dispatch case expression.
>
> > Maybe there's something clever I could do with delay and force, but I'm
> not
> > very familiar with how they work...
> >
> > Background: the reason I want to do this is so that I can dynamically
> > construct a list of all the filenames in a directory of static html files
> > and make dispatch clauses so that they can be accessed at URL's without
> > ".html" on the end.
>
> If you want it truly dynamic on every request, then I would just catch
> [(string-arg) ...] and in the handler throw next-dispatcher if the
> string doesn't correspond to a file.
>
> Jay
>
> >
> > Thanks in advance for any guidance!
> >
> > Tobias
> >
> > ____________________
> >   Racket Users list:
> >   http://lists.racket-lang.org/users
> >
>
>
>
> --
> Jay McCarthy <jay at cs.byu.edu>
> Assistant Professor / Brigham Young University
> http://faculty.cs.byu.edu/~jay
>
> "The glory of God is Intelligence" - D&C 93
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131217/6749680c/attachment.html>

Posted on the users mailing list.