diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/dice-roller.lisp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/examples/dice-roller.lisp b/examples/dice-roller.lisp new file mode 100644 index 0000000..403ed55 --- /dev/null +++ b/examples/dice-roller.lisp @@ -0,0 +1,39 @@ +(defpackage #:dice-roller + (:use #:cl) + (:import-from #:weekend #:endpoint #:handle)) + +(in-package #:dice-roller) + +(defconstant +digits+ "([0-9]+)") + +(defclass roller () + ((rolls + :reader rolls + :initarg :rolls + :type integer + :documentation "The number of rolls") + (sides + :reader sides + :initarg :sides + :type integer + :documentation "The number of sides")) + (:documentation "") + (:metaclass endpoint) + (:method . :get) + (:route-parts "roll" +digits+ "d" +digits+) + (:extractors (:rolls parse-integer) (:sides parse-integer)) + (:content-type . "text/plain")) + +(defmethod handle ((req roller)) + (with-slots (rolls sides) req + (format nil "~ad~a ... rolled a ~a" + rolls sides + (loop :repeat rolls :sum (1+ (random sides)))))) + +(defvar *server* (make-instance 'hunchentoot:easy-acceptor + :port 8080)) + +(hunchentoot:start *server*) + +;; now visit http://localhost:8080/roll/10/d/4 + |