Ocsigen by example, Part 1: basic setup
This article is the first of a series I'll be doing on Ocsigen programming. Ocsigen is both a web server and the umbrella name for an Ocaml-based framework that brings the power and safety of functional programming to the world of web development. Strictly speaking, this tutorial is about Eliom, the actual programming framework part of Ocsigen. However, because the two are closely related, and because the name "Ocsigen" has much better recognition, I've used it as a title.
Note that the Ocsigen site already offers a very comprehensive tutorial on Eliom. This series of articles is not intended to compete with it. In fact, I won't be doing any tutoring at all and I'll assume that readers are familiar with the existing tutorial. The goal of this series is simply to present self-contained and documented code samples illustrating various aspects of Eliom programming. These are taken from my own self-study experiments with Eliom, and I'm only sharing them with the hope they might be useful to others.
Let's start with the development setup. All the examples in this series can be compiled with the same Makefile and invoked with the same ocsigen.conf configuration file for the server and META file with findlib information. For the sake of simplicity, you can put all of these files in the same directory and just run the Ocsigen web server from that directory in debug mode by invoking "ocsigen -v -c ocsigen.conf" for the byte-code version or "ocsigen.opt -v -c ocsigen.conf" for the native-code version (if you're running Ocaml >= 3.11). The Makefile is as follows:
NAME=module
all: $(NAME).cma $(NAME).cmxs
%.cma: %.cmo
ocamlc -a -o $@ $+
%.cmxa: %.cmx
ocamlopt -a -o $@ $+
%.cmxs: %.cmxa
ocamlopt -shared -linkall -I `pwd` -o $@ $<
%.cmo: %.ml
ocamlfind ocamlc -thread -package lwt,ocsigen -c $<
%.cmx: %.ml
ocamlfind ocamlopt -thread -package lwt,ocsigen -c $<
clean:
rm -f $(NAME).cm[ioax] $(NAME).cmx[as] $(NAME).[oa]
As for the ocsigen.conf file, it can be defined like the code below (just change the findlib path according to your installation). Note the we're using port 8080 so the server can be started by any user:
<ocsigen>
<server>
<port>8080</port>
<charset>utf-8</charset>
<mimefile>/etc/mime.types</mimefile>
<debugmode/>
<findlib path="/home/dario/.local/lib/ocsigen/METAS"/>
<findlib path="."/>
<extension findlib-package="ocsigen_ext.ocsipersist-sqlite"/>
<extension findlib-package="ocsigen_ext.eliom"/>
<extension findlib-package="ocsigen_ext.staticmod"/>
<host hostname="dual">
<site dir="">
<eliom findlib-package="module"/>
<static dir="./"/>
</site>
</host>
</server>
</ocsigen>
Finally, note that because we rely on findlib for the installation of the module containing the actual code for the web site, we must provide a META file indicating the dependencies and the names of the compiled code units. As per the ocsigen.conf declaration, the name of this file must be META.module:
directory = "."
requires = ""
archive(plugin,byte) = "module.cma"
archive(plugin,native) = "module.cmxs"
And that's it as far as the development setup is concerned. The next article will present the first real example. Stay tuned!

Is there way to develop applications without rebooting ocsigen at every change in code?
Reply to this