[plt-scheme] web-server and templates

From: Mike Burns (netgeek at speakeasy.net)
Date: Sun Aug 15 13:12:06 EDT 2004

--- Jordan Johnson mumbled on 2004-08-14 15.16.32 -0700 ---
> On Saturday, August 14, 2004, at 01:38  PM, Mike Burns wrote:
> >What you seem to want is `send/suspend/dispatch'. It will be part of a
> >future release of the PLT Web server. Until then, I've been using this:
> ..an interesting and ingenious approach, indeed.
> I don't see how it would work for the purpose of what I was doing 
> earlier, though, which required dispatching on two arguments, e.g.,
> 	<a href="myscript.ss;...?row=2&col=5">2x5</a>
> ..or am I missing something?  (I'd love to be shown wrong on this. :>)

Using send/suspend/callback, this problem can be entirely avoided. Here's an 
example that might help.

(module example-servlet mzscheme
  (require (lib "servlet.ss" "web-server")
           (lib "etc.ss"))
  (provide timeout interface-version start)
  (define timeout 15)
  (define interface-version 'v1)

 (define (send/suspend/callback p-exp)                                           
   (let/cc k0                                                                     
               p-exp (lambda (proc)                                                       
                       (let/cc k1 (k0 (proc (send/suspend k1)))))))))                     
 (define (replace-procedures p-exp p->a)                                         
     ((list? p-exp) (map (lambda (p-e) (replace-procedures p-e p->a))            
     ((procedure? p-exp) (p->a p-exp))                                           
     (else p-exp)))

 (define (blow-up-coordinate! row col)
   ;;; TODO: the grunt work

 (define (transition-explosion row col)
   (lambda (req)
     (blow-up-coordinate! row col)
     `(html (head (title "BLAM!"))
            (body (h1 "Ka-BOOOM!")
                  (p (string-append
                       "The co-ordinate "
                       ,(number->string row)
                       ,(number->string col)
                       " has exploded."))))))

 (define (start req)
     `(html (head (title "Pick a co-ordinate"))
            (body (h1 "Pick a co-ordinate")
                      (lambda (x y)
                        `(li (a ((href ,(transition-explosion x y)))
                                ,(string-append (number->string x) ","
                                                (number->string y)))))
                      (build-list 5 identity)
                      (build-list 5 identity)))))))


Mike Burns netgeek at speakeasy.net http://netgeek.ws

Posted on the users mailing list.