Fork me on GitHub
#beginners
<
2024-06-18
>
valerauko05:06:15

Anyone using the Lapce editor for Clojure?

M3tti07:06:21

Anyone knows how to add a ring middleware to http-kit ? I need especially the wrap-params middleware

M3tti07:06:00

nevermind solved it allready 😄

M3tti07:06:10

(def my-server (hk-server/run-server (wrap-params #(ruuter/route (routes) %)) {:port 8080}))

💯 1
M3tti07:06:49

so just to wrap my head around it. the wrap-params function gets a function and returns the function with extra stuff around correct? So that it gives the underlying functions extra params in the request? Correct

teodorlu09:06:34

100 % correct, as far as I can tell! > Middleware are higher-level functions that add additional functionality to handlers. The first argument of a middleware function should be a handler, and its return value should be a new handler function that will call the original handler. https://github.com/ring-clojure/ring/wiki/Concepts#middleware

M3tti10:06:45

i did a lot fp in js in the past but in lisp no clue why it feels harder for me to understand and grok 😄 i guess i just have to get even further around this parens. Even though i don't have struggle to read code and stuff no clue what is missing currently 😄

teodorlu11:06:44

Personally, I’m not super comfortable writing middleware either 😅 I find that it can be hard to get the order of the middleware right, and when something goes wrong, the stack trace is just one big anonymous function. In my own code, I’ve sometimes introduced my own request-enter and response-exit functions. In your code, it could look something like this:

(defn request-enter [...])
(defn response-exit [...])
(defn app [req] (ruuter/route (routes) req))
(defn wrapped-app [req] (-> req request-enter app response-exit))
ring.middleware.params has a function ring.middleware.params/params-request you can be used in request-enter.

teodorlu11:06:25

I’ve had plenty of times where there’s an advanced Clojure solution that I simply avoid. I accept writing more code, so that the code is more explicit, and I can understand what’s going on.

💯 1
M3tti12:06:39

i always try to make it easy to read cause i'm stupid 😄 and i know 2 weeks from now i would have no clue what i did 😄 so readability first imho.

❤️ 1