Add middleware example, reaadme

This commit is contained in:
Benni Schwerdtner 2023-04-07 12:11:29 +02:00
parent dbb28b86e1
commit 60af6a3a94
6 changed files with 44 additions and 1 deletions

@ -1 +1 @@
Subproject commit ad763a78f1bc327a493ff0b650aa5408ecbf4819
Subproject commit 24c5d08eb4795dff04d67cfaed2e4f4ebcf91404

View file

@ -0,0 +1,33 @@
# User Middleware
- Your middleware is defined in bb user sources.
- Middleware is a function in wrap-middleware style.
# Example
Middleware should be a function in the form of:
```clojure
(defn my-middleware [handler]
(fn [request]
;; ...
(handler request)
;; ...
))
```
# Usage
```shell
bb nrepl-server --middleware [my.middleware/println-middleware]
```
--middleware is a vector of fully qualified function symbols.
They are required to be located on the babashka classpath.
This will start babashka with a nrepl server with the middlware defined in `user_middleware/my_middleware/src/my/middleware.clj`.
You can now connect to the nrepl like usual.
It is possible to redefine the middleware function from within the running nrepl, because we keep a reference
to the sci-var of the middlware.

View file

@ -0,0 +1 @@
{:paths ["src"]}

View file

@ -0,0 +1,6 @@
(ns my.middleware)
(defn println-middleware [handler]
(fn [request]
(println (:op (:msg request)))
(handler request)))

View file

@ -0,0 +1,2 @@
{:deps
{my/middleware {:local/root "../my_middleware/"}}}

View file

@ -0,0 +1 @@
(ns hello-middleware)