commit 0104080f47cd4ac780533e7d4c8d0be749960ccf
parent 557932e647ecb8d8a1ae16116d8e600d6c3292aa
Author: Tomas Hlavaty <tom@logand.com>
Date: Sun, 16 Feb 2014 19:41:10 +0100
updated example counter
Diffstat:
M | counter.lisp | | | 68 | +++++++++++++++++++++++++++++++++++++++++++++----------------------- |
1 file changed, 45 insertions(+), 23 deletions(-)
diff --git a/counter.lisp b/counter.lisp
@@ -1,19 +1,19 @@
-(defpackage :jara2wad4cl.counter
+(defpackage :rw.example.counter
(:use :cl))
-(in-package :jara2wad4cl.counter)
+(in-package :rw.example.counter)
(defun counter-widget (i rvar)
- (jara2wad4cl:slet ((n 0 rvar))
+ (let ((n 0)) ;;rw.ui:slet ((n 0 rvar))
(lambda ()
`(:p ,i ": "
- " " ,(jara2wad4cl:link "up" (lambda () (incf n)))
- " " ,(jara2wad4cl:link "down" (lambda () (decf n)))
+ " " ,(rw.ui:link "up" (lambda () (incf n)))
+ " " ,(rw.ui:link "down" (lambda () (decf n)))
" " (:b ,n)))))
(defun toplevel-widget ()
(let ((w (mapcar 'counter-widget '(1 2 3 4) '(i j x y)))
- (w2 (jara2wad4cl::calendar-widget 2012 7)))
+ (w2 (rw.ui:calendar-widget 2012 7)))
(lambda ()
`(:html
(:head
@@ -25,17 +25,8 @@
(:title "counter"))
(:body ,@(mapcar #'funcall w) ,(funcall w2))))))
-(defun deconstruct ()
- ;;(print (hunchentoot:script-name*)) => parse REST rvars
- (values (hunchentoot:get-parameter "s")
- (hunchentoot:get-parameter "a")
- (list 'i (hunchentoot:get-parameter "i")
- 'j (hunchentoot:get-parameter "j")
- 'x (hunchentoot:get-parameter "x")
- 'y (hunchentoot:get-parameter "y"))))
-
(defun construct (sid aid renv)
- (let ((prefix "/counter/"))
+ (let ((prefix "/"))
(with-output-to-string (s)
(format s "~a?s=~a&a=~a" prefix sid aid)
(loop
@@ -43,11 +34,42 @@
when v
do (format s "&~(~a~)=~a" k v)))))
-(hunchentoot:define-easy-handler (counter :uri "/counter/") ()
- (jara2wad4cl:draw (lambda () (toplevel-widget)) 'deconstruct 'construct))
+(defvar *query-parameters*)
+
+(defun query-parameter (key)
+ (cdr (assoc key *query-parameters* :test #'equal)))
+
+(defun deconstruct ()
+ (values (query-parameter "s")
+ (query-parameter "a")))
+
+(defun draw-counter ()
+ (rw.ui:draw (lambda ()
+ (let ((w (toplevel-widget)))
+ (lambda ()
+ `(:http-1.0
+ :code 200
+ :headers (("Content-Type" . "text/html;charset=utf-8")
+ ("cache-control" . "no-cache,no-store")
+ ("pragma" . "no-cache")
+ ("expires" . "-1"))
+ :body ,(funcall w)))))
+ 'construct
+ 'deconstruct))
+
+(defun counter-handler (msg stream method query protocol headers &optional body)
+ (declare (ignore protocol headers))
+ (ecase msg
+ (:read (rw:till (rw:peek-reader stream)))
+ (:write
+ (let ((rw.ui:*http-server*
+ (let ((pp (rw.http::post-parameters method body)))
+ (lambda (msg &rest args)
+ (declare (ignore args))
+ (ecase msg
+ (:method method)
+ (:post-parameters pp)))))
+ (*query-parameters* (nth-value 1 (rw.uri:parse-query-string query))))
+ (draw-counter)))))
-;;http://ondoc.logand.com/d/1129/1/
-;;/d/1129/1/ => doc 1129 pg 1
-;;/product/show/1
-;;/people/new
-;;/people/1/edit
+(rw.http:server "0.0.0.0" 2349 'counter-handler :quit (lambda () nil))