chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] How to do CGI stuffs?


From: Tobia Conforto
Subject: Re: [Chicken-users] How to do CGI stuffs?
Date: Mon, 5 Jan 2009 16:02:21 +0100

Hello

I have used the cgi-util egg for this kind of thing in the past and it wasn't at all bad.

Here is what a simple "controller" cgi script (one that only receives HTTP POSTs) looks like:

;;<initialization stuff goes here>

(case (string->symbol (CGI:lookup 'action 'token #f))
  ;;
  ((new-post)
   (and-let* ((text   (CGI:lookup 'text 'string #f))
              (author (CGI:lookup 'author 'string "")))
     (model:new-post *current-user* text author))
     (printf "Status: 302 Moved Temporarily\n")
     (printf "Location: index.cgi\n\n")))
  ;;
  ((edit-post)
   (and-let* ((postid (CGI:lookup 'id 'number #f))
              (text   (CGI:lookup 'text 'string #f))
              (author (CGI:lookup 'author 'string ""))
              (pag    (CGI:lookup 'pag 'number 1)))
     (model:secure-edit-post *current-user* postid text author)
     (printf "Status: 302 Moved Temporarily\n")
     (printf "Location: index.cgi?page=~A#post-~A\n\n" pag postid)))
  ...

The "model" lambdas are defined in a separate source file. If it's big you can compile it separately into a .so, otherwise a simple (include "model") will work both in interpreted and compiled code. In my case the model layer executes queries on a Sqlite db, using the sqlite3 egg.

The "view" cgi scripts (the ones that only receive HTTP GETs) use the same CGI:lookup function from the cgi-util egg, when needed, then build the html page using SXML, quasiquotation, a ton of helper lambdas, and finally sxml:sxml->xml from the sxml-tools egg.

The output of sxml:sxml->xml is "almost" pure XHTML, you just have to display it ignoring the list structure, with something like this:

(define (display-flattened x)
  (if (list? x)
      (for-each display-flattened x)
      (display x)))

I strongly suggest taking the time to learn SXML and related eggs: it takes the ugly part out of XML! (and XHTML as well)

Finally, I found it useful to keep the .scm source files in the same directory as the compiled versions, with the executable bit set and a shebang line (#!/usr/local/bin/csi -script) for debugging purposes. Then I can compile them to an .exe extension (csc -o index.exe index.scm) and switch between development and compiled versions using .cgi symbolic links (ln -s index.exe index.cgi) which is everything the user sees.

HTH
Tobia




reply via email to

[Prev in Thread] Current Thread [Next in Thread]