Adam Hinz: The Blog
Again with the plt
On 2008-09-07 at 9/07/2008 12:58:00 AM...
Jens Lekman - Black CabI'm back with this PLT Scheme web server. I had a few hiccups, which are now somewhat ironed out.
First, I found out that I was actually using an old version. PLT is up to 4.1, but Ubuntu's repository is still at 3.5 something. I've got 64 bit Ubuntu, and I ended up download the version here: http://download.plt-scheme.org/drscheme/plt-4-1-bin-x86_64-linux-f7-sh.html.
The 32-bit installed fine, but it wouldn't cooperate with the mysql shared library files.
Mysql. Ugh.
Lucky for me, someone already wrote wrappers for interacting with it. I got them here: http://software.pupeno.com/mr-mysql. It needs to load libmysqlclient.so, which exists on my machine as libmysqlclient.so.15.0.0 (not sure why), but a symbolic link fixed that.
Next problem was actually spitting out the content from the database. Getting the data was easy enough with the mysql library, but the web server insisted on escape my html. That is, every > was turned into an >, etc. Kind of annoying. Finally, I found this page that offered a solution: http://www.cs.brown.edu/pipermail/plt-scheme/2006-March/012349.html. Throw in a (require xml) at the top of your module and wrap your data with (make-cdata data) and you're all set.
"Will! Grab me a continuation!"
On 2008-09-02 at 9/02/2008 12:31:00 AM...
Bob Dylan - Like a Rolling StoneIt's very lonely in my apartment, but I find ways to cope with it. Like right now, I'm feeding my old interest in continuation-based web development. I've heard about it for a couple years now, mainly that Paul Graham used it in his million dollar online store thing, but I honestly had no idea what it is.
So I explored. This is more or less a log for myself to look back on in case of schemnesia (I should copyright that...).
I have only worked inside of Petite Chez Scheme (and Chez Scheme, when it was available on IU's computers), so learning a new implementation was a bit of an adventure.
PLT-Scheme seems to be the current leader in this. From what I gather, PLT-scheme has these three parts.
1) Mzscheme - the main scheme implementation. Has a command-line interface.
2) DrScheme - GUI for building modules/programs. Apparently you can draw little squares and circles, too.
3) PLaneT - repository for scheme code. You can browse it, copy a line of code, and the library will be downloaded and loaded in for you.
Installation was easy enough with Ubuntu's Synaptic package manager.
To get started, you need to start the web server.
> sudo plt-web-server
I think the sudo is necessary in order to write to the log files (since PLT was installed under sudo). I haven't confirm this, but when run without sudo, it spits out a bunch of crap about "can't write to [blah]."
The files you'll be working all sit here:
/usr/lib/plt/collects/web-server/default-web-root
Take a look at the example servlets (a servlet is simply a web application). These sit in the above directory under servlets/examples. The add.ss is pretty easy to work through.
(module add mzscheme
(require (lib "servlet.ss" "web-server"))
(provide (all-defined))
(define interface-version 'v1)
(define timeout +inf.0)
; request-number : str -> num
(define (request-number which-number)
(string->number
(extract-binding/single
'number
(request-bindings (send/suspend (build-request-page which-number))))))
; build-request-page : str -> str -> response
(define (build-request-page which-number)
(lambda (k-url)
`(html (head (title "Enter a Number to Add"))
(body ([bgcolor "white"])
(form ([action ,k-url] [method "post"])
"Enter the " ,which-number " number to add: "
(input ([type "text"] [name "number"] [value ""]))
(input ([type "submit"] [name "enter"] [value "Enter"])))))))
(define (start initial-request)
`(html (head (title "Sum"))
(body ([bgcolor "white"])
(p "The sum is "
,(number->string (+ (request-number "first") (request-number "second"))))))))
The coolest thing here is send/suspend. It's kind of a call/cc for web programming. It takes one argument, which is a procedure taking one argument (k-url) representing the next url (the continuation url, so to speak). k-url is used as the action of the form.
The rest of the things here are just handy procedures for grabbing variables from the form, which relieves a lot of the annoyingness of other languages. This immediately grabs me as really, really cool. My intuition says that I would be writing an application in reverse. That is, assuming I have all the data, I would figure out what to I do with it. Then, I would work out how to get the data. In the mean time, gluing all the steps together comes for free.
From what I've read, the real beauty of it has to do with the user's browser actions. If they are working through a web form, open a new window with the same page, and submit both forms, they will continue without noticing each other. In php or python, you would have to deal with session variables interfering with each other. Here, a new browser starts a new instance of the continuation, and any new variables assigned are saved only in the scope of that continuation.