[racket] Web server and URL dispatch

76 views
Skip to first unread message

Jordan Schatz

unread,
Dec 2, 2011, 8:17:20 PM12/2/11
to Racket Users
Its unclear to me how to get url dispatch working...

----------------------------------------------------------------------
#lang web-server
(require web-server/templates
web-server/dispatch
web-server/web-server)

(define-values (blog-dispatch blog-url) ;;blog-dispatch is our
;;new dispatcher?
(dispatch-rules
[("") list-posts]
[("posts" (string-arg)) review-post]
[("archive" (integer-arg) (integer-arg)) review-archive]
[else list-posts]))

(define (list-posts req) `(list-posts))
(define (review-post req p) `(review-post ,p))
(define (review-archive req y m) `(review-archive ,y ,m))

(define (start)
(serve
#:dispatch blog-dispatch ;;Start the server with our dispatcher?
#:port 8080))

(start) ;;Doesn't start a server : (
----------------------------------------------------------------------

I cant seem to get a server running that is using that dispatcher... Also
I presume its possible to mix "pretty" urls made by dispatch-rules and
embed/url ?

Thanks,
Jordan
_________________________________________________
For list-related administrative tasks:
http://lists.racket-lang.org/listinfo/users

Jay McCarthy

unread,
Dec 2, 2011, 8:23:38 PM12/2/11
to Jordan Schatz, Racket Users
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.

The URLs from blog-url and embed/url (& send/suspend's friends) can be freely mixed.

Jay
--
Jay McCarthy <j...@cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93

Jordan Schatz

unread,
Dec 2, 2011, 10:15:23 PM12/2/11
to Jay McCarthy, Racket Users
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,

Jay McCarthy

unread,
Dec 2, 2011, 11:44:41 PM12/2/11
to Jordan Schatz, Racket Users
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


?

Jordan Schatz

unread,
Dec 5, 2011, 11:00:11 PM12/5/11
to Jay McCarthy, Racket Users
> 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

Reply all
Reply to author
Forward
0 new messages