Ocsigen by example, Part 2: hello world


The second instalment of this series presents the first actual Eliom code sample. You can use the Makefile shown in the first part to compile it, and the META.module and ocsigen.conf files for starting the Ocsigen web server.

This example contains three different services: first, the hello-worldish service "coucou" (so named as a homage of sorts to the convention used in the Eliom tutorial); second, the "foobar" service, which illustrates the use of GET parameters; and third, the "main" service, which makes use of Eliom_predefmod.Xhtml.a to create type safe hyperlinks to the first two services. I suggest you fiddle with the types of the GET parameters — you'll see that Eliom makes good use of the type safety of the Ocaml language by making it impossible to create hyperlinks that pass the wrong type to a service.

(************************************************************************)
(* Simple "Hello World!" Eliom examples. *)
(************************************************************************)

open XHTML.M
open Eliom_parameters


(************************************************************************)
(* The "coucou" service takes no arguments, and just displays a
welcome message with the client's IP address. The IP can be
obtained via the "sp" (server parameters) parameter that is
passed to every handler.
*)

let coucou_handler sp () () =
let greeting = "Hello " ^ (Eliom_sessions.get_remote_ip sp) ^ "!"
in Lwt.return
(html
(head (title (pcdata "Coucou")) [])
(body [p [pcdata greeting]]))

let coucou_service =
Eliom_predefmod.Xhtml.register_new_service
~path: ["coucou"]
~get_params: Eliom_parameters.unit
coucou_handler


(************************************************************************)
(* The "foobar" service takes two GET parameters, an integer "i"
and a string "s". Note that the handler is declared as an
anonymous function instead of as a separate function like we
did with "coucou". Though sometimes useful, this practice can
lead to confusing code; for this reason, this is the only time
we'll use it in this tutorial series.
*)

let foobar_service =
Eliom_predefmod.Xhtml.register_new_service
~path: ["foobar"]
~get_params: (Eliom_parameters.int "i" ** Eliom_parameters.string "s")
(fun _ (i, s) () ->
Lwt.return
(html
(head (title (pcdata "Foobar")) [])
(body [p [pcdata ("The GET parameters are: " ^ (string_of_int i) ^ " and " ^ s)]])))


(************************************************************************)
(* The "main" service just provides links to both the "coucou" and
"foobar" services. Try changing the types of the parameters to
see that the compiler catches any typing inconstencies.
*)

let main_handler sp () () =
Lwt.return
(html
(head (title (pcdata "Main")) [])
(body [
p [
pcdata "Here's a ";
Eliom_predefmod.Xhtml.a coucou_service sp [pcdata "link"] ();
pcdata " to the coucou service"
];
p [
pcdata "And here's a ";
Eliom_predefmod.Xhtml.a foobar_service sp [pcdata "link"] (10, "hello");
pcdata " to the foobar service"
]
]))

let main_service =
Eliom_predefmod.Xhtml.register_new_service
~path: [""]
~get_params: Eliom_parameters.unit
main_handler

 

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.