Aluminium

Gallery Example

This example demonstrates how Aluminium applications can communicate via HTTP: on the server side there's an application that accepts POST requests containing images, and stores those images in the database, and on the client side, there's an application that posts all images from a local directory.

Server Side

First, the database:

CREATE TABLE gallery
(
    id          INTEGER PRIMARY KEY,
    title       TEXT
);

CREATE TABLE image
(
    id          INTEGER PRIMARY KEY,
    title       TEXT,
    gallery_id  INTEGER,
    thumbnail   BLOB,
    image       BLOB
);

Next, the templates. base.html will be used as the base for all HTML documents:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <meta http-equiv="Content-type" content="text/html; charset=UTF-8">
 <title>A Gallery</title>
</head>
<body>
<center>
<% echo(content); %>
</center>
</body>
</html>

list.html will be used to list all galleries in a 4-column table. A thumbnail from each gallery will also be displayed:

<% if (id.length > 0) { %>
<table>
<tr>
<% var i; for (i = 0; i < id.length; i++) { %>
<% if (i && i % 4 == 0) echo("</tr><tr>"); %>
<td>
<a href="show_gallery.al?id=<% echo(id[i]); %>">
<img src="thumbnail.al?id=<% echo(img_id[i]); %>" alt="<% echo(title[i]); %>"><br>
<% echo(title[i]); %></a></td>
<% } %>
<% while (i % 4 != 0) { echo("<td>&nbsp;</td>"); i++ } %>
</tr>
</table>
<% } else { %>
<p>No galleries uploaded.</p>
<% } %>

gallery.html will be used to display all images in a gallery in a 4-column table, with a thumbnail for each image:

<% if (id.length > 0) { %>
<table>
<tr>
<% var i; for (i = 0; i < id.length; i++) { %>
<% if (i && i % 4 == 0) echo("</tr><tr>"); %>
<td>
<a href="show_image.al?id=<% echo(id[i]); %>">
<img src="thumbnail.al?id=<% echo(id[i]); %>" alt="<% echo(title[i]); %>"><br>
<% echo(title[i]); %>
</a>
</td>
<% } %>
<% while (i % 4 != 0) { echo("<td>&nbsp;</td>"); i++ } %>
</tr>
</table>
<% } else { %>
<p>No images in this gallery.</p>
<% } %>

image.html will be used to display a single image:

<img src="image.al?id=<% echo(id); %>" alt="<% echo(title); %>">
<br>
<% echo(title); %>

Application code:

example_gallery_s.png

As in Wiki example, reusable structures are at the top, and the operations that can be pointed to, are colored in gray-blue-ish. Start point operations are colored in pink, and the active pointer is green (coloring elements is not necessary, but it makes code reading easier).

In the middle, there are three packages - three types of pages: index.al to show the list of all galleries, show_gallery.al to show all images from a gallery, and show_image.al to show a single image.

At the bottom there are two structures: one that generates image.al and thumbnail.al files, and another, that generates insert_image.al and insert_gallery.al files. image.al and thumbnail.al files are used to retrieve images from the database. insert_image.al is used to store an image in the database, and insert_gallery.al is used to create a gallery; both programs expect their input to be serialized and sent via POST request, and return the id of the record they have created.

Client Side

Application code:

example_gallery_c.png

The common part of both operations (gallery creation and image upload) is expressed as a separate structure, so it doesn't have to be repeated. create_gallery.al will read the title of the new gallery from stdin, and write the id of newly created gallery to stdout. upload_image.al will read the name of the local file from stdin, the id of the gallery from environment variable GAL_ID, and will write the id of uploaded image to stdout. Both programs will make requests to the address specified in the ADDR environment variable.

Using the programs we've just created directly would be quite inconvenient, so we'll write a primitive shell script to upload a whole directory of images:

#!/bin/sh

# Change this to your gallery address:
export ADDR="http://localhost/gallery"

GAL_ID=`echo -n $1 | alrunc create_gallery.al`

export GAL_ID

for i in `ls -1 $2/*.jpg`; do
        echo -n $i | alrunc upload_image.al;
done

The script takes two parameters: first - the name of the gallery and the second - directory that contains the images (.jpg only). It assumes that alrunc in the PATH, and that create_gallery.al and upload_image.al files are in the current directory, so you should edit this script to fit your setup.