From f7abccc38ceda7024ca375d34ed88f4fb561ef02 Mon Sep 17 00:00:00 2001 From: colin Date: Sun, 5 Mar 2023 16:36:44 -0800 Subject: Reorganized codebase --- endpoints.lisp | 158 --------------------------------------------------------- 1 file changed, 158 deletions(-) delete mode 100644 endpoints.lisp (limited to 'endpoints.lisp') diff --git a/endpoints.lisp b/endpoints.lisp deleted file mode 100644 index 96e3876..0000000 --- a/endpoints.lisp +++ /dev/null @@ -1,158 +0,0 @@ -;;;; 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") - -;;; DND ROUTE PROTOCOL - -(defgeneric unique-name (object) - (:documentation "Returns a unique name for an object, or NIL if it does not have one.") - (:method ((ob t)) nil)) - -(defmethod unique-name ((campaign campaign)) - (campaign-title campaign)) - -(defmethod unique-name ((hero hero)) - (hero-name hero)) - -(defgeneric urlpath (inflection object) - (:documentation "Return the path to the object given a particular") - (:method ((inflection t) (object has-uid)) - "If the object has a unique human readable name, urlify that name and -incorporate it into the urlpath. Otherwise use the object's uid. - -Returns /inflection/class/identifier." - (format nil "~a/~a/~a" - (urlify inflection) - (urlify (class-name (class-of object))) - (urlify (or (unique-name object) (uid object)))))) - -;;; 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+ ))) - -(defun text-browser-p (user-agent) - "Returns T if user agent string matches on a list of known text browsers." - (some (lambda (s) (search s user-agent)) '("Emacs" "Lynx" "w3m"))) - -(defun page-render-mode (user-agent) - "Given the USER-AGENT string from request headers, returns a symbol which -indicates which render mode to use. For example if Emacs is the user-agent, -return :text-12mode." - (cond ((text-browser-p user-agent) :text-page) - (t :page))) - -(defmacro with-session ((player &key session (redirect "/tavern-door")) &body body) - (let ((session (or session (gensym "SESSION")))) - `(a:if-let (,session (current-session)) - (let ((,player (session-player ,session))) - (declare (ignorable ,player)) - ,@body) - (redirect-to ,redirect)))) - -(defmacro with-checked-plist (typed-keys plist &rest body) - "Like WITH-PLIST, but allows you to pass a checking function to -automatically tansform plist values into something you actually -want. This is modelled after the way LAZYBONES allows for similar -functions in url parameters in endpoint definitions." - (let* ((plist-var - (gensym)) - (bindings - (loop :for (var key . pred) :in typed-keys - :when pred - :collect `(,var (funcall ,(first pred) (getf ,plist-var ',key))) - :else - :collect `(,var (getf ,plist-var ',key))))) - `(let ((,plist-var ,plist)) - (let ,bindings ,@body)))) - -;;; VALIDATOR TRANSFORMS - -(defun a-valid-nick (name) - "Errors with 400 if the name is not a valid hero name." - (unless (valid-nick-p name) - (lzb:http-err 400 (format nil "Player Nick Invalid"))) - name) - -(defun a-short-string (str) - (unless (and (stringp str) (< (length str) 50)) - (lzb:http-err 400 "The value must be a string at most 50 characters long.")) - str) - - -;;; OPEN ENDPOINTS - - -(defendpoint* :get "/" () () - (redirect-to "/tavern")) - -(defendpoint* :get "/tavern-door" () () - "Tavern door is where the player logs into the system." - (a:if-let (name (flashed-value :tavern-door)) - (doorkeeper :message (format nil "Ne'er 'erd of ye ~a" name)) - (doorkeeper))) - -(defendpoint* :post "/tavern-door" () () - (with-plist ((nick :nick)) (lzb:request-body) - (a:if-let ((player (player-with-nick (string-trim " " nick)))) - (a:when-let ((sesh (new-sesh player))) - (lzb:set-response-cookie +session-cookie-name+ (session-id sesh) - :path "/" :domain "localhost") ; TODO: generalize domain - (redirect-to "/tavern")) - (progn - (flash :tavern-door nick) - (redirect-to "/tavern-door")))) ) - -(defendpoint* :get "/register" () () - (register)) - - - -(defendpoint* :post "/register" () () - "Registers a new player" - (with-checked-plist ((nick :nick 'a-valid-nick)) (lzb:request-body) - (register-player nick) - (redirect-to "/tavern-door"))) - -;;; SESSION ENDPOINTS - -(defendpoint* :get "/tavern" () () - (with-session (me) - (render (page-render-mode (lzb:request-header :user-agent)) - (make-instance 'tavern :player me)))) - -(defendpoint* :get "/godess-shrine" () () - (with-session (player) - (godess-shrine))) - -(defendpoint* :post "/godess-shrine" () () - (with-session (player) - (with-checked-plist ((name :name 'a-short-string)) (lzb:request-body) - (birth-from-the-goddess-loins player name) - (redirect-to "/tavern")))) - -(defendpoint* :post "/new-campaign" () () - (with-session (creator) - (with-checked-plist ((title :title 'a-short-string)) (lzb:request-body) - (let ((campaign - (create-campaign creator title))) - (redirect-to (urlpath :details campaign)))))) - -- cgit v1.2.3