diff options
author | colin <colin@cicadas.surf> | 2023-01-21 09:49:39 -0800 |
---|---|---|
committer | colin <colin@cicadas.surf> | 2023-01-21 09:49:39 -0800 |
commit | 6b1c9d7ae508c41848828939454945bf2addb994 (patch) | |
tree | 05210e5b7bc1735031ae3df0c7f75becdbe6b6e1 | |
parent | b1c6b6ecf3476beb82f7b39516b94863e12c37f8 (diff) |
Refactor+Fix: with-hero-session macro; packaging and init
-rw-r--r-- | dnd.lisp | 3 | ||||
-rw-r--r-- | init.lisp | 13 | ||||
-rw-r--r-- | model.lisp | 2 | ||||
-rw-r--r-- | pages.lisp | 9 | ||||
-rw-r--r-- | routes.lisp | 21 |
5 files changed, 30 insertions, 18 deletions
@@ -6,6 +6,7 @@ "The instance of the HTTP server") (defun conjure-arena () + (init-db) (setf *dnd-arena* (lzb:create-server)) - (lzb:install-app *dnd-arena* (lzb:app)) + (lzb:install-app *dnd-arena* (lzb:app 'dnd)) (lzb:start-server *dnd-arena*)) @@ -3,9 +3,10 @@ (in-package #:dnd) (defun init-db (&optional config) - (if config - nil - (make-instance - 'db:mp-store - :directory (merge-pathnames "dnd-store/" (user-homedir-pathname)) - :subsystems (list (make-instance 'db:store-object-subsystem))))) + (unless (boundp 'db:*store*) + (if config + nil + (make-instance + 'db:mp-store + :directory (merge-pathnames "dnd-store/" (user-homedir-pathname)) + :subsystems (list (make-instance 'db:store-object-subsystem)))))) @@ -1,7 +1,5 @@ ;;;; model.lisp -- bknr.datastore class definitions for dnd - - (in-package :dnd) (deftype title () @@ -1,5 +1,7 @@ ;;;; pages.lisp -- html generation functions for dnd +(in-package :dnd) + (defmacro with-page ((&key title) &body body) `(with-html-string (:doctype) @@ -15,7 +17,7 @@ (:form :method "POST" :action "/godess-shrine" (:label :for "NAME" "Enter the epithet thy hero shall be called:") (:input :name "NAME") - (:button :type "submit")))) + (:button :type "submit" "Pry To The Goddess")))) (defun doorkeeper () (with-page (:title "Tavern Door") @@ -23,11 +25,10 @@ (:form :method "POST" :action "/tavern-door" (:label :for "NAME" "Thy Hero's Appelation:") (:input :name "NAME") - (:button :type "submit")) + (:button :type "submit" "Enter")) (:h2 "Eh? Ye need to birth a new hero?") (:a :href "/godess-shrine" "Follow me..."))) (defun tavern (hero) (with-page (:title "A Bustling Tavern") - (:h1 "Aye! Welcome " (hero-name hero)) - ())) + (:h1 "Aye! Welcome " (hero-name hero)))) diff --git a/routes.lisp b/routes.lisp index b0b81ac..f737c80 100644 --- a/routes.lisp +++ b/routes.lisp @@ -13,10 +13,20 @@ (setf (lzb:response-header :location) location (lzb:response-code) "303")) -(defmacro with-session (&body body) - `(a:if-let ((sesh (session-with-id (lzb:request-cookie +session-cookie-name+)))) - ,@body - (redirect-to "/tavern-door"))) +(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)) @@ -39,4 +49,5 @@ (redirect-to "/tavern-door")))) (defendpoint* :get "/tavern" () () - (with-session (tavern (session-hero sesh)))) + (with-hero-session (hero) + (tavern hero))) |