[racket] Web server and URL dispatch

From: Jordan Schatz (jordan at noionlabs.com)
Date: Fri Dec 2 22:15:23 EST 2011

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


Posted on the users mailing list.