diff options
author | colin <colin@cicadas.surf> | 2023-01-25 17:24:22 -0800 |
---|---|---|
committer | colin <colin@cicadas.surf> | 2023-01-25 17:24:22 -0800 |
commit | 59ae4f81e108eef2a1997f42f5b210e79e3d328a (patch) | |
tree | 381120bc05b89faaba4a660a38c4fae8910b5689 | |
parent | 698fba0a307dda4b28f16f92a44f095c18b97820 (diff) |
Organize: putting utilities first
Its nice to have macros load before defuns that use them.
-rw-r--r-- | endpoints.lisp | 53 |
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)))) |