Add middleware example, reaadme
This commit is contained in:
parent
dbb28b86e1
commit
60af6a3a94
6 changed files with 44 additions and 1 deletions
|
|
@ -1 +1 @@
|
||||||
Subproject commit ad763a78f1bc327a493ff0b650aa5408ecbf4819
|
Subproject commit 24c5d08eb4795dff04d67cfaed2e4f4ebcf91404
|
||||||
33
examples/user_middleware/Readme.md
Normal file
33
examples/user_middleware/Readme.md
Normal 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.
|
||||||
1
examples/user_middleware/my_middleware/deps.edn
Normal file
1
examples/user_middleware/my_middleware/deps.edn
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
{:paths ["src"]}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
(ns my.middleware)
|
||||||
|
|
||||||
|
(defn println-middleware [handler]
|
||||||
|
(fn [request]
|
||||||
|
(println (:op (:msg request)))
|
||||||
|
(handler request)))
|
||||||
2
examples/user_middleware/my_project/bb.edn
Normal file
2
examples/user_middleware/my_project/bb.edn
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
{:deps
|
||||||
|
{my/middleware {:local/root "../my_middleware/"}}}
|
||||||
1
examples/user_middleware/my_project/hello_middleware.clj
Normal file
1
examples/user_middleware/my_project/hello_middleware.clj
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
(ns hello-middleware)
|
||||||
Loading…
Reference in a new issue