-
Notifications
You must be signed in to change notification settings - Fork 43
AppServer
This page describes Links's current in-process web server model. It is intended to simplify initial setup requirements, and to provide more options for persistent server-side behavior.
The Links web server is a wrapper of Cohttp, a light-weight Ocaml web server, adapted to the needs of Links web apps. It maintains a mapping of URLs to Links functions, called the routing table, and executes those functions upon receiving web requests.
It adds two new primitive server-side functions:
-
addRoute(uri, fun)
adds a new entry to the routing table. Ifuri
ends in a forward slash character, it is interpreted as a directory entry, and handles all URIs beginning with that directory. Otherwise, it must be matched exactly. All URIs are treated as absolute, regardless of whether there is an initial slash. The functionfun
should be of typeString -> Page
, where the argument provides the actual URI. Other details of the web request (query parameters, cookies, &c.) are accessed via global variables, as in other Links web apps. Note that the routing table is dynamic, and that requests will be handled by the most specific installed handler. -
servePages()
begins accepting web requests and routing them according to the routing table; it does not return. As the routing table is dynamic, callingaddRoute(uri, fun)
after callingservePages()
will add routes to the web server. CallingservePages()
afterservePages()
has already been called in unsupported.
Otherwise, the web server makes no changes to Links execution model; programs using the web server APIs can be executed like any other server-side Links programs.
The prelude contains two functions that wrap common case uses of the addRoute
and servePages
functions:
sig serveThis : (() ~> Page) ~> ()
sig serveThese : ([(String,() ~> Page)]) ~> ()
The first takes a function from unit to page, adds it as the default route handler, and starts the server. The second takes a list of pairs of strings (prefixes) and page handlers, installs all of them, and starts the server. In both cases, the page handlers ignore the inputs.
The in-process app server adds two new configuration settings, exposing Cohttp options.
-
host
is astring
giving the host's IP address -
port
is aninteger
giving the host port number
By default, these are localhost and 8080, respectively.
The web server also contains several workarounds to address the inability to serve static content using Links functions.
- The first is that URIs beginning with the value of the
jsliburl
setting are interpreted as JavaScript library requests, and so these requests are intercepted before checking the routing table. They are mapped into the file system, starting at the newjslibdir
setting. Note that for this to work properly,jsliburl
should be an absolute URL. - Additional static routes can be added using the
addStaticRoute(uri, path, mimeTypes)
function, which maps URIs beginning withuri
to paths beginning withpath
. Default MIME types can be overridden using themimeTypes
argument, which pairs extensions with their new MIME types.