]> gitweb.factorcode.org Git - factor.git/commitdiff
New webapps.calculator demo
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Tue, 23 Sep 2008 08:35:09 +0000 (03:35 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Tue, 23 Sep 2008 08:35:09 +0000 (03:35 -0500)
extra/webapps/calculator/calculator.factor [new file with mode: 0644]
extra/webapps/calculator/calculator.xml [new file with mode: 0644]

diff --git a/extra/webapps/calculator/calculator.factor b/extra/webapps/calculator/calculator.factor
new file mode 100644 (file)
index 0000000..f1416fb
--- /dev/null
@@ -0,0 +1,44 @@
+! Copyright (C) 2008 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: furnace.actions furnace.redirection
+http.server.dispatchers html.forms validators urls accessors
+math ;
+IN: webapps.calculator
+
+TUPLE: calculator < dispatcher ;
+
+: <calculator-action> ( -- action )
+    <page-action>
+
+    [
+        { { "z" [ [ v-number ] v-optional ] } } validate-params
+    ] >>init
+
+    { calculator "calculator" } >>template
+
+    [
+        {
+            { "x" [ v-number ] }
+            { "y" [ v-number ] }
+        } validate-params
+
+        URL" $calculator" "x" value "y" value + "z" set-query-param
+        <redirect>
+    ] >>submit ;
+
+: <calculator> ( -- responder )
+    calculator new-dispatcher
+        <calculator-action> >>default ;
+
+! Deployment example
+USING: db.sqlite furnace.alloy namespaces http.server ;
+
+: calculator-db ( -- params db ) "calculator.db" sqlite-db ;
+
+: run-calculator ( -- )
+    <calculator>
+        calculator-db <alloy>
+        main-responder set-global
+    8080 httpd ;
+
+MAIN: run-calculator
diff --git a/extra/webapps/calculator/calculator.xml b/extra/webapps/calculator/calculator.xml
new file mode 100644 (file)
index 0000000..ed8e60d
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version='1.0' ?>
+
+<t:chloe xmlns:t="http://factorcode.org/chloe/1.0">
+
+       <head> <title>Calculator</title> </head>
+
+       <body>
+               <h1>Calculator</h1>
+
+               <t:form t:action="$calculator">
+
+                       <table>
+                               <tr><td>First value:</td><td> <t:field t:name="x" /> </td></tr>
+                               <tr><td>Second value:</td><td> <t:field t:name="y" /> </td></tr>
+                       </table>
+
+                       <input type="SUBMIT" value="Compute" />
+
+                       <t:if t:value="z">
+                               <br/>
+
+                               Result: <t:label t:name="z" />
+                       </t:if>
+
+               </t:form>
+       </body>
+
+</t:chloe>