Pages

Oct 5, 2013

Using Ocsigen for Web Development with OCaml on Nitrous.IO

As I said in my last post, once you have configured OPAM on nitrous.io, it is quite easy to install additional OCaml libraries and framework. Ocsigen is a web development framework for OCaml. In this post we will see how to configure Ocsigen on nitrous.io to build a simple website with OCaml. To install Ocsigen, just use OPAM
opam install ocsigenserver
After installing Ocsigen we need to install Eliom to enable dynamic websites with Ocsigen. Again run the following command to install it from OPAM.
opam install eliom
If the installations were successful you will now have  a working Ocsigen setup on your machine. In order to test it out let's just create a simple website from the hello world example. Make a new folder in your box inside that copy the following example eliom source file.


 (* ************************************************************************** *)  
 (* Project: Ocsigen Quick Howto : Page                    *)  
 (* Description: Example of a simple page (The famous "Hello World"!)     *)  
 (* Author: db0 (db0company@gmail.com, http://db0.fr/)             *)  
 (* Latest Version is on GitHub: http://goo.gl/sfvvq              *)  
 (* ************************************************************************** *)  
 open Eliom_content  
 open Html5.D  
 open Eliom_parameter  
 (* ************************************************************************** *)  
 (* Application                                *)  
 (* ************************************************************************** *)  
 module Example =  
  Eliom_registration.App  
   (struct  
    let application_name = "example"  
    end)  
 (* ************************************************************************** *)  
 (* Service declaration                            *)  
 (* ************************************************************************** *)  
 let main =  
  Eliom_service.service  
   ~path:[]  
   ~get_params:unit  
   ()  
 (* ************************************************************************** *)  
 (* Service definition                             *)  
 (* ************************************************************************** *)  
 let _ =  
  Example.register  
   ~service:main  
   (fun () () ->  
    Lwt.return  
      (html  
        (head (title (pcdata "Hello World of Ocsigen")) [])  
        (body [h1 [pcdata "Hello World!"];  
            p [pcdata "Welcome to my first Ocsigen website."]])))  

In my box I saved this file as the example/example.eliom. Then I created example/static folder to host the static resources of the website. In order to build this file you can use the magic makefile provided by Ocsigen. Change the static dir to "./static" and  in server files put "example.eliom" , in addition I had to remove the "-noinfer" option from the file as the compiler was giving an error with it. Once you make these changes you can compile your eliom web application example.eliom by typing "make" on the console. It will create a directory structure as follows in the same directory.



The compiled web application is "example.cma". Now we need to launch the Ocsigen server with this web application. For that we take help of the following conf file. 

 <!-- ----------------------------------------------------------------------- -->  
 <!-- Project: Ocsigen Quick Howto                      -->  
 <!-- Description: Configuration file to launch the examples         -->  
 <!-- Author: db0 (db0company@gmail.com, http://db0.fr/)           -->  
 <!-- Latest Version is on GitHub: https://github.com/db0company/Gallery/   -->  
 <!-- ----------------------------------------------------------------------- -->  
 <ocsigen>  
  <server>  
   <port>3000</port>  
   <logdir>./tmp/</logdir>  
   <datadir>./tmp/</datadir>  
   <user></user>  
   <group></group>  
   <commandpipe>./tmp/ocsigen_command</commandpipe>  
   <mimefile>/home/action/.opam/4.01.0/etc/ocsigenserver/mime.types</mimefile>  
   <extension findlib-package="ocsigenserver.ext.ocsipersist-sqlite">  
    <database file="./tmp/ocsidb"/>  
   </extension>  
   <extension findlib-package="ocsigenserver.ext.staticmod"/>  
   <extension findlib-package="eliom.server"/>  
   <charset>utf-8</charset>  
   <debugmode/>  
   <host hostfilter="*">  
    <static dir="./static/" />  
    <eliom module="./example.cma">  
    </eliom>  
   </host>  
  </server>  
 </ocsigen>  
Save the above file as "example.conf" in the same directory and launch the server with the following command.

ocsigenserver -c example.conf 
 This is configure the server to run on the 3000 port as specified in "example.conf" and run the example application. You can test that the application is running using the Preview feature on nitrous.io.



Just use the 3000 port and then the web server should serve the following page in a new window.


This enables nitrous.io to be used as a web development environment for OCaml.  You can now use the Web IDE to develop web applications for OCaml in the same way as you can do for the default boxes supported by nitrous.io. 

Oct 3, 2013

Configuring OPAM for OCaml on Nitrous.IO

In the previous post I showed how to get a working OCaml compiler installed on a nitrous.io box.  We had to get the source code of OCaml from github and build it. It turns out there is a much easier way to get the compiler and all the necessary libraries. We can install the OPAM package manager inside our box and then use OPAM to download additional libraries. In order to configure OPAM on nitrous.io we follow the steps on the quick install page  for the binary installer of OPAM. Run the following commands in the terminal.
wget http://www.ocamlpro.com/pub/opam_installer.sh 
sh ./opam_installer.sh /home/action/.local/bin
The installer will guide you through the process of setting up and installing the OCaml compiler. Once the installation finishes you can use the OCaml system from console. You can also use OPAM to install additional libraries. Details on the usage of OPAM as a package manager are given here. By default it OPAM will install the libraries shown in the screenshot below.


Sep 29, 2013

OCaml on Nitrous.IO

Nitrous.IO provides free servers with console access to developers. The servers can be provisioned across different geographies and allows for a good way to code on the web. Each nitrous box comes with a web based IDE and console access. They provide boxes for web development in Ruby/Rails, Go, Python/Django and node.js. However since they allow console access it is possible to install many other systems on your nitrous box. It can be used as a good way to do we based tutorials or small projects inside the box as well. The web based IDE also has experimental support for collaboration so it can be a good tool to work on some project together with others. In this post I will show how to install OCaml on Nitrous so that you can code and test simple OCaml programs. It can be ideal for class assignments or small projects since the OCaml compiler is not the easiest thing to install on Windows and Mac platforms (based on my experience of teaching a class of undergrads). 

To begin, register on nitrous.io and create a new box, you can choose any of the default box but I chose python. Give it some name (dev-box) and chose the region closest to you (Southeast Asia).


Once you create a box you should be logged into the web based IDE on the box with console at bottom as shown below.



We will use the console to download and install the OCaml compiler. You can use the source code from the OCaml github repository. Just clone the source code using the https url to the repository. ("git clone https://github.com/ocaml/ocaml.git") 



It will create an ocaml directory with all the source code in your workspace. Change to that directory and then run "./configure -prefix /home/action/.local" This will tell the compiler to install in your local user directory under .local folder. 



Once the configuration finishes successfully, we can use "make world.opt" to build the compiler and "make install" to install it. (Look up INSTALL file in the ocaml directory for more detailed notes on installation. It is pretty standard way of  building the compiler on a linux machine.)  If you followed the steps without errors you will now have a working OCaml compiler on your nitrous box. You can run the OCaml toplevel by running "ocaml" from your workspace. Additional OCaml libraries like findlib can also be installed by using wget command to get the source code from their website and building them. 


You can use the web based IDE provided with your nitrous box to edit OCaml programs and run them using the console.



As you can see above I am using C for syntax highlighting as the web IDE doesn't support OCaml yet. The setup is not perfect but it allows anyone to try OCaml on the web without much fuss and hey its free !!! I actually managed to run all the lab assignments and tutorials for the module I am TAing this semester at NUS.

Jun 25, 2013

Air Pollution in New Delhi


After the furor created by the recent haze situation in Singapore, I wondered about the air quality back home in India. So I decided to check up the air pollution levels in New Delhi which is known to be one of the most polluted cities in India. What I found surprised me, the PSI level on average in New Delhi is 200+. The Delhi Pollution Control Committee provides real time data on air quality in different parts of Delhi on its website. A quick glance on the PM 10 concentration in Delhi suggests that the PSI level routinely goes above 400 (a level considered extremely hazardous by Singapore). I was really surprised that there is no one talking about it in India. The air quality in Delhi is one of the worst in the world. A recent article on Slate also found that air pollution in Delhi is far worse than Beijing (a city considered worst by many in terms of air pollution).

Residents in Delhi continuously breath air which is considered extremely dangerous by accepted standards in other parts of the world. Even though I have lived in Delhi for some time now and was aware of the pollution problem, I wasn't aware that it is possibly the worst in the world. News about air quality is rarely reported in the Indian media. As such many Indians are blissfully unaware about the gravity of the situation in Delhi. There is some evidence that the gains made by the CNG public transportation program in Delhi are already eroded. The high particulate content in Delhi is mostly due to road dust and industrial exhaust rather than vehicular pollution. This means than government intervention and systemic policy making can improve the air quality by shutting down industrial facilities near Delhi.

However it may be too much to expect the Delhi (State and Central) government to do anything concrete about this problem in the near future. In this respect India may be even worse than China where the local governing bodies have acknowledged the problem at least. The environmental bodies in Delhi are doing a good job providing real time air quality data but lack of awareness among the public, limited coverage by the local media and inaction by the appropriate agencies has left the residents high and dry. Can people themselves do something to make the air better ? May be the new Aam Admi Party can take up air pollution as an important issue for the next elections ? I am sure residents of Delhi could do with a bit of fresh air.