This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-08-30
Channels
- # aleph (15)
- # announcements (4)
- # aws (2)
- # bangalore-clj (7)
- # beginners (236)
- # calva (24)
- # cider (11)
- # cljs-dev (63)
- # clojure (141)
- # clojure-europe (3)
- # clojure-india (2)
- # clojure-italy (8)
- # clojure-nl (3)
- # clojure-spec (8)
- # clojure-uk (52)
- # clojured (1)
- # clojuredesign-podcast (4)
- # clojurescript (35)
- # clojutre (3)
- # community-development (1)
- # cursive (77)
- # data-science (1)
- # datomic (3)
- # emacs (13)
- # fulcro (7)
- # graalvm (78)
- # graphql (2)
- # nrepl (7)
- # off-topic (18)
- # pathom (25)
- # reagent (12)
- # reitit (31)
- # shadow-cljs (178)
- # spacemacs (7)
- # tools-deps (32)
- # xtdb (10)
- # yada (3)
if i have a router like this:
(reitit.ring/router
["" {:middleware
[reitit.ring.middleware.muuntaja/format-middleware
ring.middleware.cookies/wrap-cookies
[ring.middleware.session/wrap-session
{:store (ring.middleware.session.memory/memory-store sessions)}]]}
["/test" {:get test-route}]]
{:reitit.middleware/transform print-request-diffs})
my output structure looks like this:
--- :request---
--- :request---
--- :request---
--- :response---
--- :response---
--- :response---
but it doesn't mention the middleware names, like on the screenshot in the docs:
https://github.com/metosin/reitit/blob/master/doc/ring/transforming_middleware_chain.mdi guess those middlewares must be defined as data with a name attribute and should be put into the middleware registry?
but i was expecting to see at least reitit.ring.middleware.muuntaja/format-middleware
appearing in the diffs
Minimal example:
(-> ["" {:middleware [reitit.ring.middleware.muuntaja/format-middleware]}
["/test" {:get (constantly {:status 200 :body "ok"})}]]
(reitit.ring/router
{:reitit.middleware/transform print-request-diffs})
(reitit.ring/ring-handler
(reitit.ring/create-default-handler))
(apply [{:request-method :get
:uri "/test"}]))
it outputs only this:
--- :request---
{:path-params {}, :request-method :get, :uri "/test"}
--- :response---
{:body "ok", :status 200}
=> {:status 200, :body "ok"}
and doesn't mention the format-middleware
I was just missing the :muuntaja muuntaja.core/instance
from the router config:
(defn router []
(reitit.ring/router
["" {:muuntaja muuntaja.core/instance
:middleware
[reitit.ring.middleware.muuntaja/format-middleware
...
now I can see the muuntaja/format-middleware
being traced both during the request and the response processing phases:
--- :request---
{:body #<[email protected]>,
:headers {"content-length" "11",
"cookie" "ring-session=673143dd-c7dc-4230-a7ac-f761c0da9ffa",
"host" "localhost:8080"},
:path-params {},
:query-string nil,
:remote-addr "127.0.0.1",
:request-method :post,
:scheme :http,
:server-name "localhost",
:server-port 8080,
:uri "/test",
:aleph/keep-alive? true,
:aleph/request-arrived 868329216079431}
--- :request :reitit.ring.middleware.muuntaja/format ---
{:body #<[email protected]>,
:headers {"content-length" "11",
"cookie" "ring-session=673143dd-c7dc-4230-a7ac-f761c0da9ffa",
"host" "localhost:8080"},
:path-params {},
:query-string nil,
:remote-addr "127.0.0.1",
:request-method :post,
:scheme :http,
:server-name "localhost",
:server-port 8080,
:uri "/test",
:aleph/keep-alive? true,
:aleph/request-arrived 868329216079431,
+:muuntaja/request nil,
+:muuntaja/response #muuntaja.core.FormatAndCharset
{:charset "utf-8", :format "application/json", :raw-format nil}}
--- :request---
...
{:body {:response data}, :headers {}, :status 200}
--- :response :reitit.ring.middleware.muuntaja/format ---
{:body {:response data}, :headers {}, :status 200}
--- :response---
{:body -{:response data} +#<[email protected]>,
:headers {+"Content-Type" "application/json; charset=utf-8"},
:status 200}
I find the response traces a bit confusing though.
None of the responses before or after the --- :response :reitit.ring.middleware.muuntaja/format ---
divider are produced by this middleware, but instead the last - unnamed - response is the result of this transformation.
the original :request
is the thing entering the chain. when request enters the format-middleware, it adds the content negotiation results into the request.
when your handler responds, in the :response
phase the middleware uses the content negotiation results and changes the `:body into a stream and adds the negotiation headers into the response.
which middleware does the last, unnamed, --- :response---
line represent?
based on the output, it seem like the :reitit.ring.middleware.muuntaja/format
is not directly doing anything to the response, just delegates the transformation to something else, which is not named
I think the printing of respones is off-by-one. It now reports the response before the middleware is applied.
i've also figured out how can i attach names to the regular ring middlewares:
{:muuntaja muuntaja.core/instance
:middleware [reitit.ring.middleware.muuntaja/format-middleware
{:name :ring.middleware.cookies/wrap-cookies
:wrap ring.middleware.cookies/wrap-cookies}
{:name :ring.middleware.session/wrap-session
:wrap #(ring.middleware.session/wrap-session
% {:store (ring.middleware.session.memory/memory-store sessions)})}]}
it's a really amazing solution!
thank you and whoever else worked on it!(clojure.lang.Compiler/demunge (str identical?))
=> "clojure.core/[email protected]"
:name ::format
could be
:name ::format-middleware
just to be consistent.
I see there is a repetition of the word middleware
in the namespaced keyword, but maybe that's more of an indication that the function shouldn't contain -middleware
yeah, that demunged str of the middleware function should be the default. i wouldn't even bother cleaning it further
it could actually be useful to see if a different instance of a middleware function has been used in different traces, so it's better to leave the address of the function in the name
need to think the naming of default middleware -middleware
or not. But the default mapping from functions could do the basic demunging.
https://github.com/metosin/reitit/blob/master/modules/reitit-core/src/reitit/middleware.cljc#L44-L46