The ‘CGI’ model of Kaya
The CGI development model allows you to quickly build small CGI applications.
Hello web!
To begin, let us look at the simplest program, “Hello world!”, adapted to run as a CGI web application. Put the following in a file called helloweb.k
cgi helloweb;
import CGI;
Void PreContent() {
content("<html><head><title>Hello!</title>
</head><body>");
}
Void Default() {
content("Hello web!");
}
Void PostContent() {
content("</body></html>");
}
Compiling this file with kayac hello.k creates an executable, hello.cgi
. Kaya knows that it should produce a CGI program because of the header cgi helloweb
, and expects to find a Default
function, which is run on entry to the cgi program for the first time. PreContent
and PostContent
, if present, are run before and after Default
, respectively. The content
function is defined in the CGI module, and is preferred to simply sending data to standard output, for several reasons including portability.You can of course run this from the command line, but it is more likely that you will want to put it somewhere where your web server knows to execute it as a CGI script. Try helloweb.cgi
PostContent, Default and PreContent
We consider a web application to be an event driven application, just like a GUI application, and abstract away tricky details such as form handling and variable passing. We think of a session with an application to be a series of events (eg, following a link, or submitting a form) and write event handling functions for each such event. These functions then have the following purposes:
PreContent
is responsible for displaying headers, and any other processing which happens before any event handler.Default
is the default event handler, called on the first visit to a cgi program, or if no other event handler is appropriate.PostContent
is responsible for displaying footers and any post-processing of events.
Let us now look at a more complicated application, which includes some event handling.
Forms and Event Handlers
The CGI module includes a number of functions for generating HTML. Among them is a function formHandler, which produces code for a form:
String formHandler(Void(a) fn,a dat);
The two arguments are fn
, the function which will be called when this form’s submit button is pressed (the event handler), and dat
, which is the data which will be passed to the function. The dat argument is a useful place to put any hidden variables which need to be passed through; it can be any type, provided that it can be marshalled. This argument may not be needed, in which case you can simply pass through a dummy value.
We can create a simple form with the following function:
Void helloForm(){
content(formHandler(OnHello,0)+
"What is your name? "+
textBox("name")+
submit("Say Hello")+
closeForm());
}
The functions textBox, submit and closeForm are also all provided by the CGI module. The effect of this is to output a form which, when submitted, will be processed by a function OnHello
, defined as follows:
Void OnHello(Int dummy) {
name = incomingValue("name",DataPost);
content("<p>Hello "+name+"!</p>");
helloForm();
}
OnHello
gets the value of name from the HTTP POST variables, and says hello. Finally, it redisplays the form. Note that there is a dummy argument, as this function does not need any other data to be passed in.
Finally, the default event handler needs to know to display the form:
Void Default() {
helloForm();
}
You can download the source code, or try running it.
Link Handlers
Sometimes, we don’t want to handle complex user input, but simply want the user to follow a link to access a function. fFor this purpose, the CGI module has a function linkHandler:
String linkHandler(Void(a) fn,a dat,String inf,
[Pair<String,String>] others = [])
Like formHandler, this function specifies an event handler to execute and a value to pass to it when the event happens. Unlike formHandler
, it also allows a list of variables to be passed in the query string.
Examples of simple CGI programs
- Web Calculator – A simple calculator language, with assignment, +, -, * and /. Includes an expression parser which makes use of the Parser library.
- Sudokumatic – A (very simple) sudoku solver. No backtracking or any AI, just refinement according to some simple rules. (sudokumatic.k, sudoku.k)
- Shapes – An application for drawing and manipulating images with shapes, using the Image library.