Commit 8f650bcb authored by gremlin43820's avatar gremlin43820

adding a version of the adder which uses fastcgi


git-svn-id: https://godirepo.camlcity.org/svn/wdialog/trunk@192 f54c9a64-0731-4a92-b797-30fd5898f27c
parent 18ae6675
all: adder.fcgi
adder.fcgi: session.ml adder.ml
ocamlfind ocamlc -g -o adder.fcgi -package wdialog -linkpkg session.ml adder.ml
opt: adder-opt.fcgi
adder-opt.fcgi: session.ml adder.ml
ocamlfind ocamlopt -o adder-opt.fcgi -package wdialog -linkpkg session.ml adder.ml
clean:
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 www.fastcgi.com.
A sample process managment directive for mod_fastcgi.
Directory /Users/eric/Sites>
Options +ExecCGI
AddHandler fastcgi-script .cgi .fcgi
</Directory>
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)
with
error ->
(* On error: Jump to the error page *)
raise(Change_page "display-error")
)
in
(* 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 *)
()
end
;;
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())
in
Netcgi_fcgi.serv
(Wd_run_fcgi.create_request_handler
~session_manager: (new memory_session_manager)
~charset:`Enc_utf8 ~reg ~uifile:"adder.ui" ())
buffered_transactional_optype
<?xml version="1.0"?>
<!DOCTYPE ui:application
PUBLIC "-//NPC//DTD WDIALOG 2.1//EN" "">
<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">
<html>
<head>
<title>The Ultimative Adder</title>
</head>
<body>
<ui:form>
<h1>The Ultimative Adder</h1>
<p>Please enter the two numbers you want to add:</p>
<p>
<ui:text variable="first_number"/> +
<ui:text variable="second_number"/> =
<ui:button name="add" label="Compute Result" goto="display-result"/>
</p>
</ui:form>
</body>
</html>
</ui:page>
<ui:page name="display-result">
<html>
<head>
<title>The Ultimative Adder</title>
</head>
<body>
<ui:form>
<h1>The Ultimative Adder</h1>
<p>The result is:</p>
<p>
<ui:dynamic variable="first_number"/> +
<ui:dynamic variable="second_number"/> =
<ui:dynamic variable="result"/>
</p>
<p>
<ui:button name="back" label="Go back" goto="enter-numbers"/>
</p>
</ui:form>
</body>
</html>
</ui:page>
<ui:page name="display-error">
<html>
<head>
<title>The Ultimative Adder</title>
</head>
<body>
<ui:form>
<h1>The Ultimative Adder</h1>
<p>Sorry, one of your numbers is not a number. Please go
back and correct your input.</p>
<p>
<ui:button name="back" label="Go back" goto="enter-numbers"/>
</p>
</ui:form>
</body>
</html>
</ui:page>
</ui:dialog>
</ui:application>
open Wd_dialog
open Wd_types
let _ = Random.self_init
let random_char () =
if Random.bool () then
Char.chr ((Random.int 26) + 65)
else
Char.chr ((Random.int 26) + 97)
let make_session_id () =
let length = 128 + (Random.int 128) in
let id = String.create length in
for i=0 to length - 1
do
id.[i] <- random_char ()
done;
id
exception Session_not_found
class memory_session (id: string) (instant_session: session_type) =
object
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
end;;
class memory_session_manager =
object
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;
memses
method unserialize universe env id =
try Hashtbl.find sessions id
with Not_found -> raise Session_not_found
end;;
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