The :middleware
syntax in reitit-ring
supports also Keywords. Keywords are looked from Middleware Registry, which is a map of keyword => IntoMiddleware
. Middleware registry should be stored under key :reitit.middleware/registry
in the router options. If a middleware keyword isn't found in the registry, router creation fails fast with descriptive error message.
Application using middleware defined in the Middleware Registry:
(require '[reitit.ring :as ring])
(require '[reitit.middleware :as middleware])
(defn wrap-bonus [handler value]
(fn [request]
(handler (update request :bonus (fnil + 0) value))))
(def app
(ring/ring-handler
(ring/router
["/api" {:middleware [[:bonus 20]]}
["/bonus" {:middleware [:bonus10]
:get (fn [{:keys [bonus]}]
{:status 200, :body {:bonus bonus}})}]]
{::middleware/registry {:bonus wrap-bonus
:bonus10 [:bonus 10]}})))
Works as expected:
(app {:request-method :get, :uri "/api/bonus"})
; {:status 200, :body {:bonus 30}}
Router creation fails fast if registry doesn't contain the Middleware:
(def app
(ring/ring-handler
(ring/router
["/api" {:middleware [[:bonus 20]]}
["/bonus" {:middleware [:bonus10]
:get (fn [{:keys [bonus]}]
{:status 200, :body {:bonus bonus}})}]]
{::middleware/registry {:bonus wrap-bonus}})))
;CompilerException clojure.lang.ExceptionInfo: Middleware :bonus10 not found in registry.
;
;Available middleware in registry:
;
;| :id | :description |
;|--------+--------------------------------------|
;| :bonus | reitit.ring_test$wrap_bonus@59fddabb |
Middleware as Keywords helps to keep the routes (all but handlers) as literal data (e.g. data that evaluates to itself) enabling the routes to be persisted in external formats like EDN-files and databases. Duct is a good example where the middleware can be referenced from EDN-files. It should be easy to make Duct configuration a Middleware Registry in reitit-ring
.
The bad thing it's an extra indirection, making things more complex and removed the default IDE-support of "goto definition" or "look source".
reitit-middleware
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close