Instaweb : Instant Web Publishing Tool

Instaweb : Instant Web Publishing Tool

By Noel Welsh (noelwelsh at gmail dot com) 

This manual documents Instaweb version 2.1

Time-stamp: <2007-12-07 15:58:50 noel>

Keywords: _instaweb_, _servlet_, _internet_, _web_


Instaweb is a simple tool that makes it easier to run
servlets in the PLT web server.

It does the following:

  - configures the web server to run a single servlet

  - runs the web server in a handy interactive shell


If your servlet is in a file called in the
current directory, you run your servlet in the web-server
running by simply requiring the Instaweb collection and
calling the instaweb function with no arguments:

  (require (planet "" ("schematics" "instaweb.plt" 2)))

This will start the web server on port 8765 and listening
only to connections from the localhost (  You can
view your servlet by visiting the URL:

You can override these defaults, and many others, by passing
keyword arguments to the instaweb function.  For example, to
set the port:

  (instaweb #:port 4567)

Or perhaps you want to use a different file name for your

  (instaweb #:servlet-path "")

If you give a relative path for the servlet, as above, it is
resolved relative to the value of the current-directory

If you want to make your use of Instaweb unaffected by the
value of current-directory, use the instaweb-here macro,
which parameterizes current-directory to the directory of
the file containing the call:

  (instaweb-here #:servlet-path ""
                 #:port 5678
                 #:listen-ip "")

If you want to serve static files in addition to your
servlet you can put them in a directory called htdocs
(again, relative to the current-directory parameter) or
specify a different path to instaweb:

  (instaweb #:htdocs-path '("public-html"))

Note that you give a list of paths, so you can specify
multiple directories.  Files matching the path portion of
the request URL are searched for in these directories.  If
no matching file is found control is passed to your servlet.
This means your servlet will receive all requests that do
not match a file.

If the instaweb function is not sufficient for your needs,
the component parts are exposed in the API on which you can
build your own abstractions.


The main Instaweb API is required as:

  (require (planet "" ("schematics" "instaweb.plt" 2)))

> (instaweb [option] ...)
instaweb : [#:port integer]
           [#:listen-ip (U string #f)]
           [#:servlet-path (U path string)]
	   [#:htdocs-path (listof (U path string))]
 	   [#:mime-types-path path]
	   [#:servlet-namespace (listof require-spec)]

All options are specified using keyword arguments and may be
given in any order.  Paths are resolved relative to the
value of the current-directory parameter if they are not

Port is an integer specifying the port the web server should
listen on.  It defaults to 8765

Listen-ip is the IP address to listen on.  A string
specifies a particular IP addresses, and #f indicates all
addresses.  It defaults to

Servlet-path is the path to the file containing the servlet
code.  It defaults to

Htdocs-path is a list of paths to the directories containing
static HTML, CSS, and other files.  It defaults to the
single directory htdocs.  The web-server is configured so
the htdocs paths are first searched first for files matching
the request URL before control is passed to your servlet.

Mime-types-path is the path to a file containing a mapping
from file extensions to MIME types.  It defaults to an
Instaweb supplied file.

Servlet-namespace is a list of require expressions
specifying modules that should be copied into the servlet's
namespace.  It defaults to any empty list.  For example, the
following is a valid statement:

   (instaweb #:servlet-namespace '((lib "")))

This is principally useful for reducing memory usage and
sharing modules between servlets (future versions of
Instaweb will support running multiple servlets).

> (instaweb-here [option] ...)

Arguments are as for instaweb above.  All paths are resolved
relative to the directory of the file containing the
instaweb-here expression, otherwise this functions exactly
like the instaweb function.

> (make-dispatcher args)
make-dispatcher : (list [#:servlet-path (U path string)]
                        [#:htdocs-path (listof (U path string))]
                        [#:mime-types-path path]
                        [#:servlet-namespace (listof require-spec)])
               -> dispatcher

Constructs a dispatcher given a list of arguments.  The
meaning of the arguments is as for instaweb above.

> (run-server port listen-ip dispatcher-args)
run-server : integer (U string #f) (listof any) -> ( -> void)

Runs the web server using the given parameters.  Returns a
thunk that, when called, shuts down the web server.  The
dispatcher-args are passed to make-dispatcher above.

> (console-loop run-server)
console-loop : ( -> ( -> void)) -> void

Given a thunk that runs the web server (and returns a thunk
that shuts down said server) runs the Instaweb console to
control the web server.

Using the above procedures you can extend Instaweb in useful
ways.  For example, if you want to run some code after the
web-server starts, you could write:

(define (my-run-server)
  (begin0 (run-server port listen-ip dispatcher-args)

(console-loop my-run-server)


The file provides the contructor for the Instaweb
dispatcher. This might be useful if you get into the plumbing of 
Instaweb.  You can require it as:

  (require (planet "" ("schematics" "instaweb.plt" 2)))

> (make [option]) 
make : [#:servlet-path (U path string)]
       [#:htdocs-path (listof (U path string))]
       [#:mime-types-path path] 
       [#:servlet-namespace (listof require-spec)]

Constructs a dispatcher you can pass to the web server.
Parameters are as for Instaweb above.


The file provides the default values of the optional
arguments to the main instaweb procedure. These might be useful
if you get into the plumbing of Instaweb.  You can require it as:

  (require (planet "" ("schematics" "instaweb.plt" 2)))

> default-servlet-name
default-servlet-name : string

The default name of the file that contains your servlet.

> default-servlet-path
default-servlet-path : string

The default path to your servlet (calculated from the directory
in which mzscheme was launched).

> default-htdocs-name
default-htdocs-name : string

The default name of the directory (relative to
current-directory) that contains your static files.

> default-servlet-path
default-servlet-path : string

The default path to your directory of static files
(calculated from the directory in which mzscheme was launched).

> default-mime-types-path
default-mime-types-path : path

The path to the default MIME types file.

> default-servlet-namespace
default-servlet-namespace : (listof require-spec)

The default value of the namespace shared between the servlet
and the module that calls Instaweb.

Tips and Caveats

Note that unlike previous version of Instaweb your servlet
is not forced to reside under a URL beginning with
/servlets/<file-name>.  If receives all requests that don't
match a file in the htdocs direcory.

Calling your servlet is the last step in the web-server
dispatching process.  Your servlet should return a 404
response if it receives a URL it cannot process.

If you want to run a Instaweb launched servlet on a server
you probably don't want an interactive terminal.  This is ok
-- Instaweb will detect when it doesn't have a terminal and
so avoid filling your logs with rubbish.  Say
is the file that launches your servlet.  The recommended way
to run such a setup on a server is:

  mzscheme -qr

The important bit here is the 'r' flag, which runs MzScheme
without a REPL.  You probably want to spawn a new process to
run MzScheme, and prevent signals from interferring with it,
a better line might be:

  nohup mzscheme -qr &


Matthew Flatt, Jens Axel Soegaard, and Dave Herman for
suggestions for dealing with a missing terminal, and
suspending the main thread

Eric Hanchow, for pointing out documentation typos

Dave Gurnell, for suggesting the switch to a dispatcher
framework and providing example code.