summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--endpoints.lisp53
1 files changed, 29 insertions, 24 deletions
diff --git a/endpoints.lisp b/endpoints.lisp
index 19be125..e947562 100644
--- a/endpoints.lisp
+++ b/endpoints.lisp
@@ -3,13 +3,37 @@
(in-package :dnd)
(lzb:provision-app ()
- :title "Dungeons & Deadlines"
- :version "0.1.0"
- :content-type "text/html")
+ :title "Dungeons & Deadlines"
+ :version "0.1.0"
+ :content-type "text/html")
(defparameter +session-cookie-name+ "dnd-session")
-;;; Endpoints that do not require a session:
+;;; UTILITIES
+
+(defun redirect-to (location)
+ "Set the lazybones response header and response code for redirecting to LOCATION.
+This procedure will error if lazybones:*request* is not currently bound."
+ (setf (lzb:response-header :location) location
+ (lzb:response-code) "303"))
+
+(defun current-session ()
+ "Get the session associated with the current request. Will throw an
+error if lazybones:*request* is not currently bound. It will return
+NIL if there is no session for the current request.
+
+I.e. It should be called within the scope of a request handler."
+ (session-with-id (lzb:request-cookie +session-cookie-name+ )))
+
+(defmacro with-hero-session ((hero &key session (redirect "/tavern-door")) &body body)
+ (let ((session (or session (gensym "SESSION"))))
+ `(a:if-let (,session (current-session))
+ (let ((,hero (session-hero ,session)))
+ ,@body)
+ (redirect-to ,redirect))))
+
+
+;;; OPEN ENDPOINTS
(defendpoint* :get "/" () ()
(redirect-to "/tavern-door"))
@@ -35,7 +59,7 @@
(birth-from-the-goddess-loins name)
(redirect-to "/tavern-door")))
-;;; Endpoints that do require a session:
+;;; SESSION ENDPOINTS
(defendpoint* :get "/tavern" () ()
(with-hero-session (hero)
@@ -45,23 +69,4 @@
"A String"
string)
-(defun redirect-to (location)
- "Set the lazybones response header and response code for redirecting to LOCATION.
-This procedure will error if lazybones:*request* is not currently bound."
- (setf (lzb:response-header :location) location
- (lzb:response-code) "303"))
-(defun current-session ()
- "Get the session associated with the current request. Will throw an
-error if lazybones:*request* is not currently bound. It will return
-NIL if there is no session for the current request.
-
-I.e. It should be called within the scope of a request handler."
- (session-with-id (lzb:request-cookie +session-cookie-name+ )))
-
-(defmacro with-hero-session ((hero &key session (redirect "/tavern-door")) &body body)
- (let ((session (or session (gensym "SESSION"))))
- `(a:if-let (,session (current-session))
- (let ((,hero (session-hero ,session)))
- ,@body)
- (redirect-to ,redirect))))