1 ! Copyright (C) 2008, 2009 Slava Pestov.
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
3 USING: accessors sequences kernel assocs combinators
\r
4 validators http hashtables namespaces fry continuations locals
\r
5 io arrays math boxes splitting urls
\r
8 http.server.responses
\r
11 furnace.conversations
\r
15 html.templates.chloe
\r
16 html.templates.chloe.syntax
\r
17 html.templates.chloe.compiler ;
\r
22 TUPLE: action rest init authorize display validate submit ;
\r
24 : new-action ( class -- action )
\r
25 new [ ] >>init [ ] >>validate [ ] >>authorize ; inline
\r
27 : <action> ( -- action )
\r
30 : merge-forms ( form -- )
\r
32 [ [ errors>> ] bi@ append! drop ]
\r
33 [ [ values>> ] bi@ assoc-union! drop ]
\r
34 [ validation-failed>> >>validation-failed drop ]
\r
37 : set-nested-form ( form name -- )
\r
41 unclip [ set-nested-form ] nest-form
\r
44 : restore-validation-errors ( -- )
\r
46 nested-forms cget set-nested-form
\r
49 : handle-get ( action -- response )
\r
53 [ init>> call( -- ) ]
\r
54 [ authorize>> call( -- ) ]
\r
55 [ drop restore-validation-errors ]
\r
56 [ display>> call( -- response ) ]
\r
59 ] with-exit-continuation ;
\r
61 CONSTANT: revalidate-url-key "__u"
\r
63 : revalidate-url ( -- url/f )
\r
64 revalidate-url-key param
\r
65 dup [ >url ensure-port [ same-host? ] keep and ] when ;
\r
67 : validation-failed ( -- * )
\r
68 post-request? revalidate-url and [
\r
70 nested-forms-key param " " split harvest nested-forms cset
\r
72 <continue-conversation>
\r
76 : handle-post ( action -- response )
\r
79 [ validate>> call( -- ) ]
\r
80 [ authorize>> call( -- ) ]
\r
81 [ submit>> call( -- response ) ]
\r
84 ] with-exit-continuation ;
\r
86 : handle-rest ( path action -- )
\r
87 rest>> dup [ [ "/" join ] dip set-param ] [ 2drop ] if ;
\r
89 : init-action ( path action -- )
\r
93 M: action call-responder* ( path action -- response )
\r
94 [ init-action ] keep
\r
95 request get method>> {
\r
96 { "GET" [ handle-get ] }
\r
97 { "HEAD" [ handle-get ] }
\r
98 { "POST" [ handle-post ] }
\r
101 M: action modify-form
\r
102 drop url get revalidate-url-key hidden-form-field ;
\r
104 : check-validation ( -- )
\r
105 validation-failed? [ validation-failed ] when ;
\r
107 : validate-params ( validators -- )
\r
108 params get swap validate-values check-validation ;
\r
110 : validate-integer-id ( -- )
\r
111 { { "id" [ v-number ] } } validate-params ;
\r
113 TUPLE: page-action < action template ;
\r
115 : <chloe-content> ( path -- response )
\r
116 resolve-template-path <chloe> <html-content> ;
\r
118 : <page-action> ( -- page )
\r
119 page-action new-action
\r
120 dup '[ _ template>> <chloe-content> ] >>display ;
\r