From 606d1e99125922442727166a7c922df590174de2 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Mon, 7 Feb 2022 09:06:37 -0600 Subject: endpoint registration --- lazybones.lisp | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/lazybones.lisp b/lazybones.lisp index 92b07d6..d19afd2 100644 --- a/lazybones.lisp +++ b/lazybones.lisp @@ -64,10 +64,10 @@ :reader endpoint-method :initarg :method :initform :get) - (template - :reader endpoint-template - :initarg :template - :initform (error "endpoint template required")) + (route + :reader endpoint-route + :initarg :route + :initform (error "endpoint route required")) (authorizer :reader request-authorizer :initarg :auth @@ -133,10 +133,29 @@ endpoint instanceq and ARGS is a list of arguments to pass to ENDPOINT's handler function." (loop for endpoint in (app-endpoints app) for match = (and (eql method (endpoint-method endpoint)) - (matches-routekey-p endpoint key)) + (matches-routekey-p (endpoint-dispatch-pattern endpoint) key)) when match return (cons endpoint (when (listp match) match)))) + +(defun patterns-match-p (p1 p2) + (and (eql (length p1) (length p2)) + (every 'routekey-term-match-p p1 p2))) + +(defun find-endpoint-matching-pattern (app method pattern) + (loop for ep in (app-endpoints app) + when (and (eql method (endpoint-method ep)) + (patterns-match-p pattern (endpoint-dispatch-pattern ep))) + return ep)) + +(defun unregister-endpoint (app method dispatch-pattern) + (a:when-let (extant-ep (find-endpoint-matching-pattern app method dispatch-pattern)) + (setf (app-endpoints app) (delete extant-ep (app-endpoints app))))) + +(defun register-endpoint (app ep) + (unregister-endpoint app (endpoint-method ep) (endpoint-dispatch-pattern ep)) + (push ep (app-endpoints app))) + (defparameter +http-methods+ (list :get :head :put :post :delete :patch)) @@ -236,7 +255,7 @@ any way to do it, hence NIL is returned." ;;; ENDPOINT DEFINITION (defmacro defendpoint - (appname method route-template + (appname method route (&key (auth nil) (endpoint-class 'lazybones::endpoint) @@ -258,7 +277,7 @@ making a new one if not." method) (a:with-gensyms (the-app auth-method) (let* ((dispatch-pattern - (parse-route-string-template route-template)) + (parse-route-string-template route)) (params (mapcar 'intern (route-variables dispatch-pattern))) (documentation @@ -273,9 +292,11 @@ making a new one if not." ,the-app (make-instance ,endpoint-class - :template ,route-template + :route ,route :pattern ,dispatch-pattern :documentation ,documentation :auth ,auth-method :function (lambda ,params ,@real-body) ,@endpoint-initargs)))))) + + -- cgit v1.2.3