]> gitweb.factorcode.org Git - factor.git/blob - extra/webapps/calculator/calculator.factor
Make sure URLs are cloned before using set-query-param on them
[factor.git] / extra / webapps / calculator / calculator.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors furnace.actions furnace.alloy furnace.redirection
4 html.forms http.server http.server.dispatchers math namespaces urls
5 validators webapps.utils ;
6 IN: webapps.calculator
7
8 TUPLE: calculator < dispatcher ;
9
10 : <calculator-action> ( -- action )
11     <page-action>
12
13     [
14         { { "z" [ [ v-number ] v-optional ] } } validate-params
15     ] >>init
16
17     { calculator "calculator" } >>template
18
19     [
20         {
21             { "x" [ v-number ] }
22             { "y" [ v-number ] }
23         } validate-params
24
25         URL" $calculator" clone "x" value "y" value + "z" set-query-param
26         <redirect>
27     ] >>submit ;
28
29 : <calculator> ( -- responder )
30     calculator new-dispatcher
31         <calculator-action> >>default ;
32
33 ! Deployment example
34 : calculator-db ( -- db ) "calculator.db" <temp-sqlite-db> ;
35
36 : <calculator-app> ( -- dispatcher )
37     <calculator> calculator-db <alloy> ;
38
39 ! Calculator runs at port 8081 and 8431
40 : run-calculator ( -- )
41     <calculator-app> main-responder set-global
42     run-test-httpd ;
43
44 MAIN: run-calculator