-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from jeko2000/feature/refactor-middlewares
Refactor middleware.lisp into separate logical files
- Loading branch information
Showing
13 changed files
with
356 additions
and
235 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.