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

From: Janos Tobias Locsei (jtlocsei at cantab.net)
Date: Tue Dec 17 12:25:01 EST 2013

Hi Jay, I tried again using serve/servelet. This is my full code:

#lang racket

(require web-server/dispatch ; for dispatch-rules
         web-server/dispatchers/dispatch ; for next-dispatcher
         web-server/servlet-env ; for serve/servlet
         web-server/http ; for response/xexpr
         )

(define-values (url-based-dispatch url-generator)
  (dispatch-rules
   [((string-arg)) dispatch-static-html]
   [("home") display-home]
   [("some-app") run-app]))

(define (dispatch-static-html request a-path-string)
  (display "Entered dispatch-static-html\n")
  ; This will eventually have code to search for static html
  ; files and display them. For now, just pretend we couldn't find
  ; the relevant html file so we throw next-dispatcher.
  (next-dispatcher))

(define (display-home request)
  (display "Entered display-home\n")
  (response/xexpr
   `(html
     (head (title "Home"))
     (body (h1 "Home")
           (p "Welcome to the home page")))))

(define (run-app request)
  (display "Entered run-app\n")
  (response/xexpr
   `(html
     (head (title "App"))
     (body (h1 "App")
           (p "This app is actually just a static page")))))


(define (respond-unknown request)
  (display "Entered respond-unknown\n")
  (response/xexpr
   `(html
     (head (title "Unknown Reponse"))
     (body (h1 "Unknown Response")
           (p "Sorry, I couldn't find what you're looking for")))))

(serve/servlet
 url-based-dispatch ; answers requests
 #:servlet-path "/home" ; default url
 #:servlet-regexp #rx"" ; capture all top-level requests
 #:file-not-found-responder respond-unknown)

What I was expecting (and wanting) was that for the URL "
http://localhost:8000/home" the function dispatch-static-html would be
called (because it matches any URL), it would throw next-dispatcher, and
then display-home would be called, since it's the next matching clause.

But in reality when dispatch-static-html throws next-dispatcher the program
jumps straight to respond-unknown, which I specified as the
file-not-found-responder keyword argument when I called serve/servlet.

What am I missing? Is there a way to get the program to do what I wanted it
to?

Thanks for all your time with this.

Tobias




-------------------------------

Thanks Jay, that makes sense. I was testing it without using serve/servlet,
using the example on http://docs.racket-lang.org/web-server/dispatch.html

I will try again using serve/servlet.


On 17 December 2013 14:45, Jay McCarthy <jay.mccarthy at gmail.com> wrote:

> The first one is what I would write, except that I'd put it after the
> ["" ...] case so it doesn't shadow it.
>
> How are you testing this code? (next-dispatcher) is supposed to throw
> an exception, but it will caught and used by the Web server when you
> are using serve/servlet, etc.
>
> Jay
>
> On Tue, Dec 17, 2013 at 7:11 AM, Janos Tobias Locsei
> <jtlocsei at cantab.net> wrote:
> > I thought I'd add a note about what my overall objective with this is, in
> > case there's a completely different way of achieving the same thing.
> >
> > Basically what I want is to be able to serve a combination of apps and
> > static html pages on my domain, but I don't want ".html" as the end of
> the
> > url for the static pages. So, if someone goes to the url
> >
> > http://mysite.com/lolcats
> >
> > ...then I'd like the web server will look in the directory
> > /htdocs/static-html/ and check whether there's a file called
> lolcats.html,
> > and serve that page if it's there.
> >
> > My idea was that to achieve this I'd write a dispatch function that would
> > check the static-html directory for a file with the correct name, and
> serve
> > that page using the "include-template" function in web-server/templates.
> >
> > But if there's an easier/better way of achieving the same thing then I'm
> > open to that.
> >
> > Tobias
> >
> >
> >
>
>
>
> --
> 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/88799197/attachment.html>

Posted on the users mailing list.