Commit 8f650bcb authored by gremlin43820's avatar gremlin43820

adding a version of the adder which uses fastcgi

git-svn-id: f54c9a64-0731-4a92-b797-30fd5898f27c
parent 18ae6675
all: adder.fcgi
ocamlfind ocamlc -g -o adder.fcgi -package wdialog -linkpkg
opt: adder-opt.fcgi
ocamlfind ocamlopt -o adder-opt.fcgi -package wdialog -linkpkg
rm adder-opt.fcgi adder.fcgi *.cmo *.cmi *.cmx *.o
To use fastcgi, you will likely need to add a module to your webserver.
Apache for example uses mod_fastcgi. Fastcgi documentation and modules for
many web servers are avaliable from
A sample process managment directive for mod_fastcgi.
Directory /Users/eric/Sites>
Options +ExecCGI
AddHandler fastcgi-script .cgi .fcgi
FastCGIServer /Users/eric/Sites/portalep_engine/index.fcgi
mod_fastcgi will then take care of starting, restarting, and killing
the fastcgi program.
(* An example: Add two numbers and display the result. This version of the
* example uses the FastCGI protocol instead of CGI. It also uses in memory
* sessions, a secure (and fast) alternative to the instant session manager
* which is only avaliable for FastCGI and jserv (because the daemon must be
* memory residant).
open Wd_dialog
open Wd_run_fcgi
open Netcgi
open Netcgi_fcgi
open Netcgi_types
open Wd_types
open Session
class add_numbers universe name env =
object (self)
inherit dialog universe name env
method prepare_page() =
(* This method is empty in this example *)
method handle() =
(* Check which event has happened: *)
match self # event with
Button("add") ->
(* Get the numbers and convert them from string to int. Catch
* errors.
let n_1, n_2 =
( try
let s_1 = self # string_variable "first_number" in
let s_2 = self # string_variable "second_number" in
(int_of_string s_1, int_of_string s_2)
error ->
(* On error: Jump to the error page *)
raise(Change_page "display-error")
(* Add the numbers, and store the result into the variable *)
let r = n_1 + n_2 in
self # set_variable "result" (String_value (string_of_int r));
| _ ->
(* Do nothing if the event is not recognized *)
prerr_endline("Args: " ^ String.concat ";" (Array.to_list Sys.argv));
let reg universe =
(* Bind the XML dialog "add-numbers" to the class "add_numbers": *)
universe # register "add-numbers" (new add_numbers);
prerr_endline "(Initializing...)";
prerr_endline ("cwd=" ^ Sys.getcwd())
~session_manager: (new memory_session_manager)
~charset:`Enc_utf8 ~reg ~uifile:"adder.ui" ())
<?xml version="1.0"?>
<!DOCTYPE ui:application
<ui:application start-dialog="add-numbers">
<ui:dialog name="add-numbers" start-page="enter-numbers">
<ui:variable name="first_number"/>
<ui:variable name="second_number"/>
<ui:variable name="result"/>
<ui:page name="enter-numbers">
<title>The Ultimative Adder</title>
<h1>The Ultimative Adder</h1>
<p>Please enter the two numbers you want to add:</p>
<ui:text variable="first_number"/> +
<ui:text variable="second_number"/> =
<ui:button name="add" label="Compute Result" goto="display-result"/>
<ui:page name="display-result">
<title>The Ultimative Adder</title>
<h1>The Ultimative Adder</h1>
<p>The result is:</p>
<ui:dynamic variable="first_number"/> +
<ui:dynamic variable="second_number"/> =
<ui:dynamic variable="result"/>
<ui:button name="back" label="Go back" goto="enter-numbers"/>
<ui:page name="display-error">
<title>The Ultimative Adder</title>
<h1>The Ultimative Adder</h1>
<p>Sorry, one of your numbers is not a number. Please go
back and correct your input.</p>
<ui:button name="back" label="Go back" goto="enter-numbers"/>
open Wd_dialog
open Wd_types
let _ = Random.self_init
let random_char () =
if Random.bool () then
Char.chr (( 26) + 65)
Char.chr (( 26) + 97)
let make_session_id () =
let length = 128 + ( 128) in
let id = String.create length in
for i=0 to length - 1
id.[i] <- random_char ()
exception Session_not_found
class memory_session (id: string) (instant_session: session_type) =
val id = id
val instant_session = instant_session
method commit_changes () = ()
method serialize = id
method change_dialog dlg = instant_session#change_dialog dlg
method dialog = instant_session#dialog
method dialog_name = instant_session#dialog_name
class memory_session_manager =
inherit instant_session_manager () as super
val sessions = Hashtbl.create 500
method create dlg =
let instant_session = super#create dlg in
let id = make_session_id () in
let memses = new memory_session id instant_session in
Hashtbl.replace sessions id memses;
method unserialize universe env id =
try Hashtbl.find sessions id
with Not_found -> raise Session_not_found
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment