Getting started with Ocsigen
Ocsigen is a web framework for OCaml that has a very special feature: it allows one to write tierless applications. This means that the line between front-end and back-end is blurred, and the entire application can be written in OCaml. That allows one to make use OCaml libraries and features such as type-safety. There is no need to write JavaScript. This guide will lead you through the installation steps on Ubuntu (or Debian), and after that, present a small hello world example that can be run locally.
Installation
To install Eliom (a component of Ocsigen), update the package lists, and install the dependencies of Eliom:
sudo apt-get update sudo apt-get install \ libev-dev libgdbm-dev libncurses5-dev libpcre3-dev \ libssl-dev libsqlite3-dev libcairo-ocaml-dev m4 \ opam camlp4-extra \ libgmp-dev
After that, use OPAM (the OCaml package manager) to install eliom
:
opam init opam install eliom
Now that you have Eliom installed, you can create a new Eliom project.
Creating an Eliom project
To create a new Eliom project:
eliom-distillery -name helloworld -target-directory helloworld
This will create a directory named helloworld
that contains your Eliom project. This directory contains a file that has a .eliom
extension. This is where the source code of the web application goes.
Writing a Hello World application
Replace the contents of the helloworld.eliom
file with the following:
let hello_world = Eliom_content.Html.D.( Eliom_registration.Html.create ~path:(Eliom_service.Path [""]) ~meth:(Eliom_service.Get Eliom_parameter.unit) (fun () () -> Lwt.return (html (head (title (pcdata "Hello World")) []) (body [h1 [pcdata "Hello World."]]))))
After that, run make test.byte
, open your web browser and navigate to http://localhost:8080
. You should see a page that containing 'Hello World.'.
I hope that was good enough of an example to get you started on this web framework. Ocsigen and Eliom are capable of so much more than the conventional back-end web frameworks out there. I urge you to read the Ocsigen documentation and its tutorials.