Created
December 1, 2022 17:53
-
-
Save dnaeon/3a3f86dea1096db5a9231d1f56a565e2 to your computer and use it in GitHub Desktop.
fukamachi/ningle middleware
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
;; ningle application with custom middlewares | |
;; | |
;; The middleware here is used to push additional context to the | |
;; environment which can later be used by ningle routes. | |
;; | |
;; Currently this is not possible in ningle, as ningle routes accept a | |
;; single argument, which represent the HTTP request parameters, and | |
;; this is different than the Lack/Clack routes, which accept the | |
;; surrounding environment instead. | |
;; | |
;; See these issues for more details. | |
;; | |
;; https://github.com/fukamachi/ningle/issues/6 | |
;; https://github.com/fukamachi/ningle/issues/41 | |
(ql:quickload :ningle) | |
(ql:quickload :lack) | |
(ql:quickload :clack) | |
(defparameter *request-env* nil | |
"*REQUEST-ENV* will be dynamically bound to the environment context | |
of HTTP requests") | |
(defun my-middleware (app) | |
"A custom middleware which wraps a NINGLE:APP and injects additional | |
metadata into the environment for HTTP handlers/controllers as part of | |
each HTTP request" | |
(lambda (env) | |
(setf (getf env :my-middleware/message) "my middleware message") | |
(funcall app env))) | |
(defclass app (ningle:app) | |
() | |
(:documentation "Custom application based on NINGLE:APP")) | |
(defmethod lack.component:call ((app app) env) | |
;; Dynamically bind *REQUEST-ENV* for each request, so that ningle | |
;; routes can access the environment. | |
(let ((*request-env* env)) | |
(call-next-method))) | |
(defparameter *app* (make-instance 'app)) | |
(setf (ningle:route *app* "/") | |
(lambda (params) | |
(declare (ignore params)) | |
(getf *request-env* :my-middleware/message))) | |
(defparameter *wrapped-app* (lack:builder #'my-middleware *app*)) | |
(defparameter *server* (clack:clackup *wrapped-app*)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment