From de8646afec9647095c37fabe93500fd8bbebb8aa Mon Sep 17 00:00:00 2001 From: colin Date: Sat, 21 Jan 2023 09:53:27 -0800 Subject: Rename: routs.lisp->endpoints.lisp --- dnd.asd | 2 +- endpoints.lisp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ routes.lisp | 53 ----------------------------------------------------- 3 files changed, 54 insertions(+), 54 deletions(-) create mode 100644 endpoints.lisp delete mode 100644 routes.lisp diff --git a/dnd.asd b/dnd.asd index ce6c4a4..d192838 100644 --- a/dnd.asd +++ b/dnd.asd @@ -22,7 +22,7 @@ (:file "utilities") (:file "init") (:file "model") - (:file "routes") + (:file "endpoints") (:file "pages") (:file "transactions") (:file "dnd"))) diff --git a/endpoints.lisp b/endpoints.lisp new file mode 100644 index 0000000..4d8b85c --- /dev/null +++ b/endpoints.lisp @@ -0,0 +1,53 @@ +;;;; endpoints.lisp -- http endpoints for dnd + +(in-package :dnd) + +(lzb:provision-app () + :title "Dungeons & Deadlines" + :version "0.1.0" + :content-type "text/html") + +(defparameter +session-cookie-name+ "dnd-session") + +(defun redirect-to (location) + (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)))) + +(defendpoint* :get "/godess-shrine" () () + (godess-shrine)) + +(defendpoint* :post "/godess-shrine" () () + (with-plist ((name :name)) (lzb:request-body) + (birth-from-the-goddess-loins name) + (redirect-to "/tavern-door"))) + +(defendpoint* :get "/tavern-door" () () + (doorkeeper)) + +(defendpoint* :post "/tavern-door" () () + (with-plist ((name :name)) (lzb:request-body) + (a:if-let ((hero (hero-known-as name))) + (a:when-let ((sesh (new-sesh hero))) + (lzb:set-response-cookie +session-cookie-name+ (session-id sesh) + :path "/" :domain "localhost") + (redirect-to "/tavern")) + (redirect-to "/tavern-door")))) + +(defendpoint* :get "/tavern" () () + (with-hero-session (hero) + (tavern hero))) diff --git a/routes.lisp b/routes.lisp deleted file mode 100644 index f737c80..0000000 --- a/routes.lisp +++ /dev/null @@ -1,53 +0,0 @@ -;;;; routes.lisp -- http routes for dnd - -(in-package :dnd) - -(lzb:provision-app () - :title "Dungeons & Deadlines" - :version "0.1.0" - :content-type "text/html") - -(defparameter +session-cookie-name+ "dnd-session") - -(defun redirect-to (location) - (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)))) - -(defendpoint* :get "/godess-shrine" () () - (godess-shrine)) - -(defendpoint* :post "/godess-shrine" () () - (with-plist ((name :name)) (lzb:request-body) - (birth-from-the-goddess-loins name) - (redirect-to "/tavern-door"))) - -(defendpoint* :get "/tavern-door" () () - (doorkeeper)) - -(defendpoint* :post "/tavern-door" () () - (with-plist ((name :name)) (lzb:request-body) - (a:if-let ((hero (hero-known-as name))) - (a:when-let ((sesh (new-sesh hero))) - (lzb:set-response-cookie +session-cookie-name+ (session-id sesh) - :path "/" :domain "localhost") - (redirect-to "/tavern")) - (redirect-to "/tavern-door")))) - -(defendpoint* :get "/tavern" () () - (with-hero-session (hero) - (tavern hero))) -- cgit v1.2.3