From 59ae4f81e108eef2a1997f42f5b210e79e3d328a Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 25 Jan 2023 17:24:22 -0800 Subject: Organize: putting utilities first Its nice to have macros load before defuns that use them. --- endpoints.lisp | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) (limited to 'endpoints.lisp') 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)))) -- cgit v1.2.3