ring-clojure / ring

Clojure HTTP server abstraction
MIT License
3.75k stars 519 forks source link

How a middleware take more than one handlers and return a new handler #449

Closed ljj038 closed 2 years ago

ljj038 commented 2 years ago

Typically middleware will be implemented as a higher-order function that takes one or more handlers and configuration options as arguments and returns a new handler with the desired compound behavior.

I read middleware examples, and I can get it that a middleware take one handler and return a new one.

but how can a middleware take more than one handlers and return a new handler?

weavejester commented 2 years ago

You can take another handler aas an argument. For example:

(defn wrap-random-surprise [handler surprise-handler]
  (rand-nth [handler surprise-handler]))

Often additional handlers are used to respond to some unusual event, such as errors, authentication failures, etc.

ljj038 commented 2 years ago

@weavejester thank you for explain.

At first, I thought maybe there is a way to combine two or more handlers to one. But I can not found a way to do it.

now, I got it. It's like if else to choose one handler from handlers by some condition. not combine.