[racket] Web server and URL dispatch

From: Jordan Schatz (jordan at noionlabs.com)
Date: Mon Dec 5 23:00:11 EST 2011

> Where do you think a different example should go? Somewhere on
> http://docs.racket-lang.org/web-server/dispatch.html

Yes, I think so. I would guess that URL based dispatch is a common entry
point for people getting started with the web server, as it more closely
resembles other frameworks / languages. The need to capture top-level
requests, and what happens when you don't was a surprise to me. Now that
I "get it" the docs seem very clear.

I first tried out URL dispatch with web-server/insta like so:
#lang web-server/insta

(define-values (blog-dispatch blog-url)
    (dispatch-rules
     [("") list-posts]
     ...
     [else list-posts]))

(define (list-posts req) 
  (response/xexpr
   `(list-posts)))
   ...
(define (start req)
  (blog-dispatch req))

To no avail. Then I thought since it was called dispatch-rules, and
blog-dispatch I must be creating a customer dispatcher. But there wasn't
much on dispatchers in Web Applications, after abit I found the HTTP
Server docs, and read all of them, much of which was close to being over
my head. Then I tried different combinations of things for a few hours.

I think it would have helped me alot to have an example of URL dispatch
working in a server, as well as the url->request examples. The
url->request set of examples are great to see how dispatch-rules is
handling types, and creating a bi-directional mapping, but it doesn't
help much getting it working in a servlet.

I don't know where it would fit, but one of the things I most missed in
the docs was a more "complete" example of servlet usage, something mixing
dispatch-rules and embed/url, running multiple servlets, and making use
of #:servlet-regexp. Also I guess its possible to run stateful and
stateless servlets together? If so that would be a nice addition to a
more "complete" example.

Overall its a really nice frameworks, and great docs... I am just finding
it to be a steep learning curve and I thought I knew alot of languages /
frameworks.

Thanks,
Jordan

On Fri, Dec 02, 2011 at 09:44:41PM -0700, Jay McCarthy wrote:
> http://docs.racket-lang.org/web-server/run.html#(part._.Examples)
> 
> contains this...
> 
> >>>
> Suppose you wanted it to capture top-level requests:
> 
> (serve/servlet my-app
>                #:servlet-regexp #rx"")
> <<<
> 
> Where do you think a different example should go? Somewhere on
> 
> http://docs.racket-lang.org/web-server/dispatch.html
> 
> ?
> 
> On Fri, Dec 2, 2011 at 8:15 PM, Jordan Schatz <jordan at noionlabs.com> wrote:
> 
> > On Fri, Dec 02, 2011 at 06:23:38PM -0700, Jay McCarthy wrote:
> > > The blog-dispatch function returned by dispatch-rules has the contract
> > > "request? -> response?" so you pass it in in place of "start" to
> > > serve/servlet:
> > >
> > > (serve/servlet blog-dispatch ...)
> > >
> > > It is not a "dispatcher" in the Web server's terminology. That's a
> > > "connection? request? -> void" function and only low level functions use
> > > those.
> >
> > ----------------------------------------------------------------------
> > #lang web-server
> >
> > (require web-server/dispatch
> >         web-server/servlet-env)
> >
> > (define-values (blog-dispatch blog-url)
> >  (dispatch-rules
> >   [("") list-posts]
> >   [("posts" (string-arg)) review-post]))
> >
> > (define (list-posts req)
> >  (response/xexpr
> >   `(body "list-posts")))
> >
> > (define (review-post req p)
> >  (response/xexpr
> >   `(body ,(string-append "review-posts " p))))
> >
> > ;; ;; Doesn't work
> > ;; (serve/servlet blog-dispatch
> > ;;                #:port 8080
> > ;;                #:launch-browser? #t)
> > ;; ;; Doesn't work
> > ;; (serve/servlet blog-dispatch
> > ;;                #:port 8080
> > ;;                #:servlet-path "/"
> > ;;                #:launch-browser? #t)
> > ;; Works
> > (serve/servlet blog-dispatch
> >               #:port 8080
> >               #:servlet-regexp #rx".*" ;But is this "the right way"?
> >               #:launch-browser? #t)
> > ----------------------------------------------------------------------
> >
> > Once I got it working, it makes sense; but maybe a more complete example
> > could be added to the docs?
> >
> > Shalom,
> > Jordan
> >
> 
> 
> 
> -- 
> 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


Posted on the users mailing list.