factor/extra/webapps/counter/counter.factor

50 lines
1.3 KiB
Factor
Raw Normal View History

! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2008-06-02 16:38:44 -04:00
USING: math kernel accessors http.server http.server.dispatchers
2008-06-17 06:21:45 -04:00
furnace furnace.actions furnace.sessions furnace.redirection
2008-06-15 19:29:10 -04:00
html.components html.forms html.templates.chloe
fry urls ;
2008-04-29 06:58:34 -04:00
IN: webapps.counter
SYMBOL: count
TUPLE: counter-app < dispatcher ;
2008-05-26 01:47:27 -04:00
M: counter-app init-session* drop 0 count sset ;
2008-04-29 06:58:34 -04:00
2008-05-26 01:47:27 -04:00
: <counter-action> ( quot -- action )
<action>
swap '[
2008-09-10 23:11:40 -04:00
count _ schange
URL" $counter-app" <redirect>
] >>submit ;
2008-04-30 17:12:14 -04:00
2008-04-29 06:58:34 -04:00
: <display-action> ( -- action )
2008-05-26 01:47:27 -04:00
<page-action>
[ count sget "counter" set-value ] >>init
2008-06-02 16:00:03 -04:00
{ counter-app "counter" } >>template ;
2008-04-29 06:58:34 -04:00
: <counter-app> ( -- responder )
counter-app new-dispatcher
[ 1+ ] <counter-action> "inc" add-responder
[ 1- ] <counter-action> "dec" add-responder
<display-action> "" add-responder
<sessions> ;
! Deployment example
USING: db.sqlite db.tuples db furnace.db namespaces ;
: counter-db ( -- params db ) "counter.db" sqlite-db ;
: init-counter-db ( -- )
counter-db [ session ensure-table ] with-db ;
: run-counter ( -- )
init-counter-db
<counter-app>
counter-db <db-persistence>
main-responder set-global
8080 httpd ;
MAIN: run-counter