Commit 02eb2c1c authored by gremlin43820's avatar gremlin43820

added an example of using the fastcgi adder with the session daemon


git-svn-id: https://godirepo.camlcity.org/svn/wdialog/trunk@200 f54c9a64-0731-4a92-b797-30fd5898f27c
parent 63ed6f7f
all: adder.fcgi
adder.fcgi: adder.ml
ocamlfind ocamlc -g -o adder.fcgi \
-package wdialog,wd-daemon-session \
-linkpkg adder.ml
opt: adder-opt.fcgi
adder-opt.fcgi: adder.ml
ocamlfind ocamlopt -o adder-opt.fcgi \
-package wdialog,wd-daemon-session \
-linkpkg 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 Wd_daemon_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
(* connect to the session management daemon on the local
machine with some default credentials, and a 10 minute
session timeout. Note, for this application to work, you
will need to start the session daemon (wdstated). *)
~session_manager:(new daemon_session_manager
"example_application1"
"password" "localhost" 600)
~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>
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