From 2920126da9ef46424d32784412d980772f5d8369 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Thu, 3 Feb 2022 17:27:26 -0600 Subject: hacking on lazybones reboot --- lazybones.lisp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) (limited to 'lazybones.lisp') diff --git a/lazybones.lisp b/lazybones.lisp index 82e5fff..6e1da32 100644 --- a/lazybones.lisp +++ b/lazybones.lisp @@ -2,6 +2,9 @@ (in-package #:lazybones) +(defgeneric handle-request (what request) + (:documentation "Implemented for APP and ENDPOINT instances.")) + (defclass app () ((name :reader app-name @@ -12,7 +15,7 @@ :reader app-version :initarg :vsn :initarg :version :initform "0.0.1" - :type string) + :type string) (root :reader app-root :initarg :root @@ -31,8 +34,63 @@ :accessor app-routes :initform nil))) +(defgeneric dispatch-handler-p (endpoint request) + (:documentation "T if ENDPOINT should handle REQUEST, NIL otherwise")) + +(defclass endpoint () + ((method :reader endpoint-method :initarg :method :initform :get) + (template :reader endpoint-template :initarg :template :initform (error "endpoint template required")) + (dispatch-pattern :reader endpoint-dispatch-pattern) + (handler-function :reader endpoint-request-handler) + (documentation :reader endpoint-documentation :initarg :doc :initform ""))) + + +(defparameter +http-methods+ + (list :get :head :put :post :delete :patch)) + (defun parse-route-string-template (template) - ) + "Routes are of the form + +/foo/bar/<>/blah + +/foo/bar/<>/blah + +On success returns things like: + +(\"foo\" \"bar\" (VARIABLE) \"blah\") +(\"foo\" \"bar\" (VAR PARSE-INTEGER) \"blah\") + +Returns NIL on failure" + (when (stringp template) + (cond ((equal "" template) nil) + (t + (loop for field in (str:split #\/ template) + for var? = (parse-route-variable-string field) + when var? + collect var? + else + collect (string-downcase field)))))) + +(defun parse-route-variable-string (string) + "A route variable string looks like <> or <> + +In the case of a successful parse, a list of one or two symbols is +returned. These symbosl are created using read-from-string, which +allows for these symbols' packages to be specified if desired. + +Returns NIL on failre." + (when (and (a:starts-with-subseq "<<" string) + (a:ends-with-subseq ">>" string)) + (destructuring-bind + (var-name . decoder?) + (re:split " +" + (string-trim " " (subseq string 2 (- (length string) 2)))) + (if decoder? + (list (read-from-string var-name) (read-from-string (first decoder?))) + (list (read-from-string var-name)))))) (defun add-route (method routestring handler-function) + (assert (member method +http-methods+) nil + "~a is not a valid HTTP method indicator." + method) ) -- cgit v1.2.3