From 35e00bed40a82760dd1a321480269427c8740b97 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Thu, 3 Feb 2022 19:14:35 -0600 Subject: Filling out a prospective oo protocol --- lazybones.lisp | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/lazybones.lisp b/lazybones.lisp index 2f15a53..735fbd5 100644 --- a/lazybones.lisp +++ b/lazybones.lisp @@ -2,9 +2,30 @@ (in-package #:lazybones) +;;; Generic Functions + (defgeneric handle-request (what request) (:documentation "Implemented for APP and ENDPOINT instances.")) +(defgeneric dispatch-handler-p (endpoint request) + (:documentation "T if ENDPOINT should handle REQUEST, NIL otherwise")) + +(defgeneric uri-path (request) + (:documentation "Returns the path associated with the request")) + +(defgeneric uri-query (request &key rawp) + (:documentation "Returns the whole query associated with a + request. If RAWP is truthy, should return the raw query + string. Otherwise should parse it somehow.")) + +(defgeneric request-body (request) + (:documentation "Returns the body of a request that has one, or NIL if not.")) + +(defgeneric request-authorized-p (endpoint request) + (:documentation "Returns T if the REQUEST has authorization to dispatch the handler for ENDPOINT")) + +;;; LAZYBONES CLASSES + (defclass app () ((name :reader app-name @@ -30,18 +51,24 @@ :documentation "A PLIST with keys being integers that represent HTTP response codes and with values that are symbols naming responder functions.") - (routes - :accessor app-routes + (endpoints + :accessor app-endpoints :initform nil))) -(defgeneric dispatch-handler-p (endpoint request) - (:documentation "T if ENDPOINT should handle REQUEST, NIL otherwise")) +(defmethod handle-request ((app app) request) + (a:if-let (endpoint (lookup-endpoint-for app request)) + (handle-request endpoint request) + (error-response )) + +) (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) + (app :reader endpoint-app :initarg :app :initform (error "every endpoint must have backlink to an app") + :documentation "backlink to the app that this endpoint is a part of.") (documentation :reader endpoint-documentation :initarg :doc :initform ""))) (defparameter +http-methods+ @@ -77,7 +104,7 @@ 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." +Returns NIL on failure." (when (and (a:starts-with-subseq "<<" string) (a:ends-with-subseq ">>" string)) (destructuring-bind -- cgit v1.2.3