The Ocgi library

The count example

(*********************************************************************************)

(*                Ocgi                                                           *)
(*                                                                               *)
(*    Copyright (C) 2003 Institut National de Recherche en Informatique et       *)
(*    en Automatique. All rights reserved.                                       *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU Lesser General Public License as published   *)
(*    by the Free Software Foundation; either version 2.1 of the License, or     *)
(*    any later version.                                                         *)
(*                                                                               *)
(*    This program is distributed in the hope that it will be useful,            *)
(*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *)
(*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *)
(*    GNU Lesser General Public License for more details.                        *)
(*                                                                               *)
(*    You should have received a copy of the GNU Lesser General Public License   *)
(*    along with this program; if not, write to the Free Software                *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)


open Ocgi

(** We define variables for our two arguments. We will add their values. *)

let a = ref 0
let b = ref 0

(** We define two mandatory arguments for our cgi.

The string in argument of Args.Mandatory is the error message in the Args.Missing_argument exception is the argument is missing.

The option specification works more or less like the Arg module of the standard library. *)


let options =
  [ "a"Args.Set_int a, [Args.Mandatory "Missing \"a\" parameter!"] ;
    "b"Args.Set_int b, [Args.Mandatory "Missing \"b\" parameter!"] ;
  ] 

(** Our main function *)

let main () =
  
  (** We get the environment of the cgi. *)

  let env = Env.get_cgi_env () in
  
  (** We create the html code to display the result of the addition. *)

  let result =
    try
      
      (** To do that we need to parse the arguments. *)

      Args.parse env options;
      let s = Printf.sprintf "%d + %d = %d" !a !b (!a + !b) in
      s      
    with
      
      (** If we have an error, the html code we return is the message. *)

      Args.Missing_argument (_, mes) ->
        mes
    | e ->
        Printexc.to_string e
  in
  
  (** We build the html code for the form, using the functions of the Html module. *)

  let form = 
    Html.form env.Env.script_name
      (Html.seq
         [
           
           (** A text input field named 'a' *)

           Html.input Html.Text "a" ;
           " + ";
           
           (** A text input field named 'b' *)

           Html.input Html.Text "b" ;
           " = " ;
           
           (** The submit button, with '?' as label *)

           Html.submit_button "?" ;
         ] 
      )
  in
  
  (** Here is the body of our page. *)

  let contents = 
    Html.seq
      [
        result ;
        Html.br;
        form
      ]        
  in
  
  (** Then we output the answer, by building the whole page with Html.page and the correct cgi answer with Model.html_answer.

The empty list in argument of Html.page means that there are no cookie actions to perform.*)


  print_string
    (Model.html_answer
       (Html.page "Ocgi: count demo" contents, [])
    )


let _ = main ()