Ocsigen by example, Part 3: delayed service registration


In the example from the previous article, the various services are registered with Eliom upon declaration. Note that we used the function Eliom_predefmod.Xhtml.register_new_service for this purpose. It is however also possible to separate these two actions, by first just declaring a service, and registering it later. You may use function Eliom_services.new_service for the former action, and function Eliom_predefmod.Xhtml.register for the latter.

Why is the separation between the declaration and registration actions useful? The most obvious application lies in services that call each other. Without this feature, you would most likely would need to resort to a "let rec ... and ..." construct, which isn't always the most practical.

The code below depicts two services, "coucou" and "foobar", which call each other. Note that we begin by just declaring each service and that we only register them in the end.

(************************************************************************)
(* Example for delayed registration. *)
(************************************************************************)

open XHTML.M
open Eliom_parameters


(************************************************************************)
(* We begin by declaring services "coucou" and "foobar". The first
takes no parameters, while the second takes a float as a GET
parameter.
*)

let coucou_service =
Eliom_services.new_service
~path: ["coucou"]
~get_params: Eliom_parameters.unit
()

let foobar_service =
Eliom_services.new_service
~path: ["foobar"]
~get_params: (Eliom_parameters.float "x")
()


(************************************************************************)
(* We now build the handler functions for each service. Note how
each handler creates a link to the other service.
*)

let coucou_handler sp () () =
Lwt.return
(html
(head (title (pcdata "Coucou")) [])
(body [p
[
pcdata "Here's a ";
Eliom_predefmod.Xhtml.a foobar_service sp [pcdata "link"] 3.1416;
pcdata " to service foobar";
]]))


let foobar_handler sp x () =
Lwt.return
(html
(head (title (pcdata "Foobar")) [])
(body [p
[
pcdata ("The parameter X is " ^ (string_of_float x));
br ();
pcdata "Here's a ";
Eliom_predefmod.Xhtml.a coucou_service sp [pcdata "link"] ();
pcdata " to service coucou";
]]))



(************************************************************************)
(* Finally we register the declared services and assign to each
one its handler function.
*)

let () =
Eliom_predefmod.Xhtml.register coucou_service coucou_handler;
Eliom_predefmod.Xhtml.register foobar_service foobar_handler

Postscriptum: If you're on the bleeding edge, you may have noted that the ocsigen.conf file I presented in the first chapter of this series needs a tweak in order to work with latest development version of Ocsigen (soon to be version 1.2). The change is minimal: basically, the "hostname" attribute for the host configuration is now called "defaulthostname". Here's the revised file:

<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 defaulthostname="localhost">
<site dir="">
<eliom findlib-package="module"/>
<static dir="./"/>
</site>
</host>
</server>
</ocsigen>

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this entry.
Comments
  • No comments exist for this entry.
Leave a comment

Submitted comments will be subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.