[plt-scheme] ANN: dispatch.plt

From: Dave Gurnell (d.j.gurnell at gmail.com)
Date: Sat Mar 8 07:30:11 EST 2008

Here it is (without an imaginative choice of name):


Dispatch is a tool for configuring controllers in web applications.  
It's three main functions are:

1. providing a mapping from simple, elegant, permanent URLs to  
controller procedures;
2. providing a reverse mapping from controller calls to URLs;
3. providing a simple way to create clean, MVC oriented code without  
having to worry about cyclic dependencies between modules.

The example below uses Dispatch and Instaservlet to set up a simple  

- "site.ss" uses Dispatch to define a simple site with three  
- "run.ss" uses Instaservlet to configure the PLT web server and  
create a servlet that dispatches all incoming requests to the site;
- "posts.ss" provides sample implementations of two of the three  
controllers and demonstrates non-continuation-based linking between  
the controllers.

The third controller is intentionally left unimplemented to  
demonstrate Dispatch's default error page, which can be used as a  
placeholder during development.

I haven't added the continuation URL rewriting features that Jay  
mentioned in the recent discussion on this list. I should be able to  
add this in a 1.x update without significantly breaking backward  


-- Dave

===== run.ss =====

#lang scheme/base

(require (planet "instaservlet.ss" ("untyped" "instaservlet.plt" 1))
          (planet "dispatch.ss" ("untyped" "dispatch.plt" 1))
          (file "/posts.ss")
          (file "site.ss"))

; (request -> response)
(define (main request)
   (dispatch request blog))
(go! main)

===== site.ss =====

#lang scheme/base

(require (planet "dispatch.ss" ("untyped" "dispatch.plt" 1)))

(provide blog index review-post review-archive)

(define-site blog
   ([(url "/") index]
    [(url "/post/" (string-arg)) review-post]
    [(url "/archive/" (integer-arg) "/" (integer-arg))

===== posts.ss =====

#lang scheme/base

(require (planet "dispatch.ss" ("untyped" "dispatch.plt" 1))
          (file "site.ss"))

; (request string -> html-response)
(define-controller (review-post request slug)
   `(html (head (title ,slug))
          (body (h1 "You are viewing " ,(format "~s" slug))
                (p "And now for some content..."))))
; (request -> html-response)
(define-controller (index request)
   `(html (head (title "Index"))
          (body (h1 "Index")
                (ul ,@(map index-item-html
                           (list "post1"

; (string -> html)
(define (index-item-html slug)
   `(li (a ([href ,(controller-url review-post slug)])
           "View " ,(format "~s" slug))))

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080308/d309db68/attachment.html>

Posted on the users mailing list.