Friday, June 26, 2009

Describing your web application using Swx (take 1)

Update July 05, 2009: Dropped the idea of describing web applications in favour of minimizing cognitive strain and busy work.
Update June 28, 2009: Added the 4th pattern with default params.


index.php

<?php

require '/path/to/swx.php';
requires ('handlers');

responds_to_requests_like
(
'POST /{handler}',
'/{handler}/[{id}]',
'GET {handler}.example.com:8080 /foobar',
'/.* handler=default,id=1'
);

?>

2 comments:

finalprefix said...

Magnificent!

This makes routes quite easy to specify.

Why do you do {[id]}, why not just [id].

A portion of your dsl here is parsed out of a string. I usually tend towards keeping my dsls as far as possible in code.

My lisp way of doing it would be:

(route POST /{handler})
(route /{handler}/[id])
(route GET {handler}.example.com/foobar)

It's almost the same in many ways.

This would be nice too:

(route GET {handler}.example.com/person/[id]/{visiting-card})

in which two handlers (handler and visiting-card) get called.

Or even:

(route GET
#'(lambda (name) (format t "You are at ~a!" name))
.example.com/person/
#'(lambda (id) (using-person id (format t "hello ~a~%" name))))

in which the handlers is defined inline. The url that hits this route of course is stuff.example.com/person/10

How about:

(routes
* POST / {handler}
* / {handler} / [id]
* GET {handler} .example.com /foobar
* GET {handler} .example.com /person/ [id]
* GET {handler} .example.com /person/ [id] / {visiting-card}
* GET
#'(lambda (name) (format t "You are at ~a!" name))
.example.com/person/
#'(lambda (id) (using-person id (format t "hello ~a~%" name))))

I think lisp can handle that. It's very much in the fashion of the powerful loop macro (see http://cl-cookbook.sourceforge.net/loop.html).

Sandeep Shetty said...

> Why do you do {[id]}, why not just [id].

Its actually [{id}]. The braces are needed to capture the variable id and the square brackets make it optional.