Skip to content

Commit

Permalink
Merge pull request #2 from jeko2000/feature/refactor-middlewares
Browse files Browse the repository at this point in the history
Refactor middleware.lisp into separate logical files
  • Loading branch information
jeko2000 authored Mar 23, 2022
2 parents 56dbe8a + 24d0ecb commit 952c8bc
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 235 deletions.
216 changes: 0 additions & 216 deletions src/middleware.lisp

This file was deleted.

52 changes: 52 additions & 0 deletions src/middleware/builder.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
;;;; builder.lisp
(in-package :cl-user)
(uiop:define-package :tiny-routes.middleware.builder
(:use :cl)
(:export #:wrap-request-predicate
#:wrap-request-mapper
#:wrap-response-mapper
#:wrap-response-mapper*))

(in-package :tiny-routes.middleware.builder)

(defun wrap-request-predicate (handler request-predicate)
"Returns a new handler that calls HANDLER only if the request
satisfies REQUEST-PREDICATE."
(lambda (request)
(when (funcall request-predicate request)
(funcall handler request))))

(defun wrap-request-mapper (handler request-mapper)
"Return a new handler that calls HANDLER with the result of applying
REQUEST-MAPPER to request."
(lambda (request)
(funcall handler (funcall request-mapper request))))

(defun wrap-response-mapper (handler response-mapper)
"Wrap HANDLER such that it returns the result of applying
RESPONSE-MAPPER to response."
(lambda (request)
(let ((response (funcall handler request)))
(typecase response
(null nil)
(cons (funcall response-mapper response))
;; Clack allows async response in the form of a lambda
(function
(lambda (responder)
(funcall response (lambda (res)
(funcall responder
(funcall response-mapper res))))))))))

(defun wrap-response-mapper* (handler bi-mapper)
"Wrap HANDLER such that it returns the result of applying BI-MAPPER
to request and response."
(lambda (request)
(let ((response (funcall handler request)))
(typecase response
(null nil)
(cons (funcall bi-mapper request response))
(function
(lambda (responder)
(funcall response (lambda (res)
(funcall responder
(funcall bi-mapper request res))))))))))
25 changes: 25 additions & 0 deletions src/middleware/method.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
;;;; method.lisp
(in-package :cl-user)
(uiop:define-package :tiny-routes.middleware.method
(:use :cl)
(:import-from :tiny-routes.middleware.builder
#:wrap-request-predicate)
(:import-from :tiny-routes.request
#:request-method)
(:export #:wrap-request-matches-method))

(in-package :tiny-routes.middleware.method)

(defun wrap-request-matches-method (handler method)
"Wrap HANDLER such that it is called only if the request method matches METHOD.
If METHOD is t, nil or `:any', then return HANDLER unchanged.
If METHOD is a list, then wrap handler such that it is called only if
the request method is in the list."
(check-type method (or symbol list))
(cond ((or (null method) (eq method t) (eq method :any)) handler)
((symbolp method) (wrap-request-predicate
handler (lambda (req) (eq (request-method req) method))))
((listp method) (wrap-request-predicate
handler (lambda (req) (member (request-method req) method))))))
13 changes: 13 additions & 0 deletions src/middleware/middleware.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;;;; middleware.lisp
(in-package :cl-user)
(uiop:define-package :tiny-routes.middleware
(:use :cl)
(:use-reexport
:tiny-routes.middleware.builder
:tiny-routes.middleware.method
:tiny-routes.middleware.path-template
:tiny-routes.middleware.query-parameters
:tiny-routes.middleware.request-body
:tiny-routes.middleware.response))

(in-package :tiny-routes.middleware)
Loading

0 comments on commit 952c8bc

Please sign in to comment.