aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorcolin <colin@cicadas.surf>2024-05-11 12:05:54 -0700
committercolin <colin@cicadas.surf>2024-05-11 12:05:54 -0700
commit887818ec9bcfcf288ed932a566ed6219f5b9f212 (patch)
tree5d6797c148d09a3f919eda86a5b7fe87c7bfb86c /examples
parent0ed31297b4ed67ac86f683b4806acbfab73190ec (diff)
Add: kitchensink example
Diffstat (limited to 'examples')
-rw-r--r--examples/dice-roller.lisp2
-rw-r--r--examples/kitchensink.lisp87
2 files changed, 88 insertions, 1 deletions
diff --git a/examples/dice-roller.lisp b/examples/dice-roller.lisp
index 3cd6ac3..d87faf4 100644
--- a/examples/dice-roller.lisp
+++ b/examples/dice-roller.lisp
@@ -27,7 +27,7 @@
(:extractors (:rolls parse-integer) (:sides parse-integer))
(:content-type . "text/plain"))
-(defmethod handle ((req roller))
+(defmethod wknd::handle ((req roller))
(with-slots (rolls sides) req
(format nil "~ad~a ... rolled a ~a"
rolls sides
diff --git a/examples/kitchensink.lisp b/examples/kitchensink.lisp
new file mode 100644
index 0000000..788334d
--- /dev/null
+++ b/examples/kitchensink.lisp
@@ -0,0 +1,87 @@
+(defpackage #:kitchensink
+ (:use #:cl)
+ (:local-nicknames
+ (#:wknd #:weekend)))
+
+(in-package #:kitchensink)
+
+(defparameter +fname+ "([^/]+)"
+ "Match any ole character except /")
+
+(defclass file ()
+ ((name
+ :reader name
+ :initarg :name
+ :initform (wknd::slot-required 'file 'name)))
+ (:documentation "Servies files in the weekend/examples dir.")
+ (:metaclass wknd::endpoint)
+ (:method . :get)
+ (:route-parts "file" +fname+)
+ (:extractors :name))
+
+(defun file-in-examples-dir (name)
+ (merge-pathnames
+ name
+ (asdf:system-relative-pathname 'weekend "examples/")))
+
+(defmethod wknd::handle ((req file))
+ (let ((file (file-in-examples-dir (name req))))
+ (unless (uiop:file-exists-p file)
+ (wknd::not-found req))
+ (wknd:handle-static-file
+ file
+ (or (wknd:mime-type file) "text/plain"))))
+
+(defvar *identified-persons* nil)
+
+(defclass identify-form ()
+ ()
+ (:documentation "Page to serve an identify form")
+ (:metaclass wknd::endpoint)
+ (:method . :get)
+ (:route-parts "identify")
+ (:content-type . "text/html"))
+
+(defmethod wknd::handle ((req identify-form))
+ "<html>
+<head></head>
+<body>
+<form method='POST' action='/identify'>
+<input name='name' placeholder='name'/>
+</form>
+</body>
+</html>")
+
+(defclass hello ()
+ ()
+ (:documentation "A Page that just says hello.")
+ (:metaclass wknd:endpoint)
+ (:method . :get)
+ (:route-parts "hello")
+ (:content-type . "text/html"))
+
+(defmethod wknd:authenticate ((req hello))
+ (or (plusp (length (wknd:get-cookie "name")))
+ (wknd:endpoint-redirect 'identify-form)))
+
+(defmethod wknd::handle ((req hello))
+ (with-output-to-string (*standard-output*)
+ (princ "<html><head></head><body>")
+ (format t "<p>hello ~a</p>" (wknd:get-cookie "name"))
+ (princ "</body></html>")))
+
+(defclass identify ()
+ ((name :reader name :initarg :name :type string))
+ (:metaclass wknd::endpoint)
+ (:method . :post)
+ (:route-parts "identify"))
+
+(defmethod wknd::handle ((req identify))
+ (wknd:set-cookie "name" :value (name req))
+ (wknd:endpoint-redirect 'hello))
+
+(defvar *server* (make-instance 'hunchentoot:easy-acceptor
+ :port 8888))
+
+(hunchentoot:start *server*)
+