Skip to content

Serving Dynamic Content

September 13, 2010

This is the second in a series of articles about web programming in Dylan. This example will show how to dynamically generate the contents of a web page.

First, the ever-exciting library and module definitions. In addition to common-dylan and koala (the HTTP server) we need streams for writing data to the response and date so we can show something dynamic happening:

define library web60-dynamic-content
  use common-dylan;
  use io, import: { streams };
  use koala;
  use system, import: { date };
end;

define module web60-dynamic-content
  use common-dylan;
  use date, import: { as-iso8601-string, current-date };
  use koala;
  use streams, import: { write };
end;

A web page is a resource mapped to a URL inside the web server. To create a resource we subclass <resource>:

define class <clock-page> (<resource>) end;

To make our resource do something we define a method on respond. (If we only wanted to implement the GET request method we could define a method on respond-to-get instead.)

define method respond (page :: <clock-page>, #key)
  let stream = current-response();
  let date = as-iso8601-string(current-date());
  write(stream, concatenate("<html><body>", date, "</body></html>"));
end;

current-response() returns the active <response> object. To send data back to the client we write to the current response.

let server = make(<http-server>,
                  listeners: list("0.0.0.0:8888"));
add-resource(server, "/", make(<clock-page>));
start-server(server);

In the previous example we already saw how to create and start a server, so the new bit here is using add-resource to map a URL to a <resource>. The first argument to add-resource is the URL router. (In Routes terminology it would be a "mapper".) For convenience, an <http-server> is a kind of router so we can add resources directly to the server. In a future example, I will show how to do more complex URL routing, which will explain the reason for the mysterious #key in the respond definition above.

Here’s the complete code:

---- File: library.dylan ----
Module: dylan-user

define library web60-dynamic-content
  use common-dylan;
  use io, import: { streams };
  use koala;
  use system, import: { date };
end;

define module web60-dynamic-content
  use common-dylan;
  use date, import: { as-iso8601-string, current-date };
  use koala;
  use streams, import: { write };
end;

---- File: main.dylan ----
Module: web60-dynamic-content

define class <clock-page> (<resource>)
end;

define method respond (page :: <clock-page>, #key)
  let stream = current-response();
  let date = as-iso8601-string(current-date());
  write(stream, concatenate("<html><body>", date, "</body></html>"));
end;

let server = make(<http-server>,
                  listeners: list("0.0.0.0:8888"));
add-resource(server, "/", make(<clock-page>));
start-server(server);

Run this example and point your browser at http://127.0.0.1:8888/.

Previous: Serving Static Content

Back to top

Advertisements

From → Computers, Dylan

5 Comments
  1. Mark permalink

    If Dylan was a little less verbose….If Apple had stuck with Dylan…If retarded Java had been a flash in the pan….

    In an parallel universe, Dylan is kicking ass….sigh.

  2. Thanks for writing this article! I’m a big fan of Dylan and happen to work with web development so it matched my interests perfectly. I like your writing style and am looking forward to more articles about Dylan.

Trackbacks & Pingbacks

  1. Static URL Routing « cgay's random notes
  2. Dylan Web in 60 Seconds « cgay's random notes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: