Changeset 20875 in project


Ignore:
Timestamp:
10/18/10 15:46:30 (11 years ago)
Author:
Moritz Heidkamp
Message:

gazette: spiffy omelette!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gazette/src/issues/8.wiki

    r20874 r20875  
    1515[[http://wiki.call-cc.org/egg/dict|dict 1.4]],
    1616[[http://wiki.call-cc.org/egg/pstk|pstk 1.1]] and
    17 [[http://wiki.call-cc.org/egg/colorize|colorize 0.3]] 0.3), as well as
     17[[http://wiki.call-cc.org/egg/colorize|colorize 0.3]]), as well as
    1818some releases introducing new features:
    1919
     
    2626Core development has been pretty quiet this week with only a little
    2727activity on the
    28 [[http://code.call-cc.org/cgi-bin/gitweb.cgi?p=chicken-core.git;a=log;h=refs/heads/experimental|experimental branch]]. Let's
     28[[http://code.call-cc.org/cgi-bin/gitweb.cgi?p=chicken-core.git;a=log;h=refs/heads/experimental|experimental branch]]. The most visible change is the [[http://www.mail-archive.com/chicken-hackers@nongnu.org/msg01246.html|addition of a blob literal syntax]] by [[http://wiki.call-cc.org/users/felix-winkelmann|Felix Winkelmann]].
    2929
    3030== 3. Chicken Talk
     
    4545== 4. Omelette Recipes - Tips and Tricks
    4646
    47 TODO
     47This week I'd like to direct your attention at our web server
     48[[http://wiki.call-cc.org/egg/spiffy|Spiffy]]. Its feature set is
     49comparable to that of the
     50[[http://httpd.apache.org/|Apache HTTP Server]] or
     51[[http://www.lighttpd.net/|lighttpd]], although it doesn't sport as
     52many extension modules (yet). While that may seem intimidating at
     53first, it actually isn't: Since Spiffy is a Scheme web server, no
     54idiosyncratic configuration file format is needed to set it up, all
     55you need is a few s-exprs to get started! Let's start with a simple
     56example:
     57
     58<enscript language="scheme">
     59(use spiffy)
     60
     61(vhost-map `(((* any) . ,(lambda (continue)
     62                        (send-response body: "hey there!\n"
     63                                       headers: '((content-type text/plain)))))))
     64
     65(start-server port: 12345)
     66</enscript>
     67
     68Alright, what does that mean? The {{vhost-map}} thing may look a
     69little odd at first. This is necessary because Spiffy has been built
     70from the ground up to support virtual hosting. The {{vhost-map}}
     71parameter is just an alist of hostnames to handlers. Hostnames are
     72given as regular expressions (either traditional or
     73[[http://synthcode.com/scheme/irregex/#SECTION_3.2|SREs]]). In the
     74example we just define a handler for {{(* any)}}, i.e. we use the same
     75handler for all hostnames.
     76
     77The handler procedure is called whenever a request for the matching
     78hostname is to be handled. In the example, we always send a static
     79plain text response regardless of what the request was using the
     80convenience function {{send-response}} (let's ignore the {{continue}}
     81argument for the moment). Execute the code using {{csi}} to make
     82Spiffy listen on TCP port 12345. Try it like this:
     83
     84    $ curl 'http://localhost:12345/'
     85    hey there!
     86
     87Or with a more complex URI, inspecting the response headers:
     88   
     89    $ curl -D - 'http://localhost:12345/foo/bar?baz'   
     90    HTTP/1.1 200 OK
     91    Content-Type: text/plain
     92    Server: Spiffy/4.8 (Running on Chicken 4.6.0)
     93    Content-Length: 11
     94    Date: Mon, 18 Oct 2010 13:06:39 GMT
     95
     96    hey there!
     97   
     98Very well! Now, usually a handler would inspect the
     99{{current-request}} parameter (which holds an
     100[[http://wiki.call-cc.org/egg/intarweb#requests|intarweb request record]]
     101representing the currently handled request) to determine how to
     102respond. Let's try to change our handler so that it only sends {{"hey
     103there"}} when the path {{/hey}} is requested. Otherwise, we'd like to
     104serve files from a certain directory (say {{/var/www}}) or, if no such
     105file exists, send a {{404}} response. This is how it can be done:
     106
     107<enscript language="scheme">
     108(use spiffy uri-common intarweb)
     109
     110(root-path "/var/www")
     111
     112(vhost-map `(((* any) . ,(lambda (continue)
     113                           (if (equal? '(/ "hey") (uri-path (request-uri (current-request))))
     114                               (send-response body: "hey there!\n"
     115                                              headers: '((content-type text/plain)))
     116                               (continue))))))
     117
     118(start-server port: 12345)
     119</enscript>
     120
     121As you can see, we now need
     122[[http://wiki.call-cc.org/egg/intarweb|intarweb]] and
     123[[http://wiki.call-cc.org/egg/uri-common|uri-common]] to be able to
     124inspect {{current-request}}. We also set the {{root-path}} which is
     125akin do Apache's {{DocumentRoot}} directive. Since it is a paramter,
     126it can be easily altered by handlers using {{paramterize}}. The
     127handler itself is only slightly more complicated now: We check whether
     128{{current-request}}'s {{uri-path}} is {{(/ "hey")}} ({{uri-common}}'s
     129way of representing the {{/hey}}) and if so, send just the response as
     130before. For any other path we call the {{continue}} procedure which is
     131passed to our handler by Spiffy. This tells Spiffy "I am done with
     132that request, please pass control on to the next handler". As it
     133happens, the next handler is Spiffy's file system handler which will
     134just take the {{uri-path}}, check whether it exists under the current
     135{{root-path}} and then delivers the file. If the path doesn't exist, a
     136{{404}} response is sent.
     137
     138Spiffy ships
     139[[a few more useful handlers|http://wiki.call-cc.org/egg/spiffy#modules]]
     140which you can easily integrate into your own application. There's also
     141an
     142[[http://wiki.call-cc.org/eggref/4/spiffy-directory-listing|egg providing a directory listing handler]]
     143as well as several eggs that may help you creating more advanced
     144applications such as
     145[[http://wiki.call-cc.org/eggref/4/spiffy-request-vars|spiffy-request-vars]],
     146[[http://wiki.call-cc.org/eggref/4/spiffy-uri-match|spiffy-cookies]],
     147[[http://wiki.call-cc.org/eggref/4/spiffy-uri-match|spiffy-uri-match]]
     148and
     149[[http://wiki.call-cc.org/eggref/4/http-session|http-session]].
     150
     151For real-world uses check out the Spiffy based
     152[[http://wiki.call-cc.org/eggref/4/phoghorn|image gallery application phoghorn]],
     153[[http://wiki.call-cc.org/egg/qwiki|qwiki]] (the wiki software
     154powering [[http://wiki.call-cc.org/|the Chicken wiki]]) and, of
     155course, the infamous
     156[[http://wiki.call-cc.org/eggref/4/awful|awful web framework]].
     157
    48158
    49159== 5. About the Chicken Gazette
Note: See TracChangeset for help on using the changeset viewer.