Clojure Ring

You are reading the original article parsed for readability by Younews Reader as requested by you. Here is the original article:

Posted on January 5, 2017

Ring provides a common and useful abstaction that builds up a web application like lego bricks. Most web application written in Clojure uses Ring.

Ring Components

  • Adapter
  • Middleware
  • Handler


Adapter adapts existing JVM web server libraries to be compatible with the Ring specification. Adapter primary job is to accept HTTP requests and convert it into Ring requests and pass it to handler. The handler will return a Ring response and adapter convert it to Http response.


Handlers accept aRing request and output Ring response. Handlers are simply Clojure funtions, they take a single argument the Ring request and return single value Ring response, computing that function is upto that function.

Ring response has 3 parts * Status: Http status code which is integer. * Body: Either string, file * Headers: Map of string


Middleware are functions that takes handler and return a different handler, they also takes optional argument to configure them, if needed those option needs to be in end. The most common function to middlerware is to transform the request before it passed to handler or it is passed to adapter.



Compojure takes cares of two things

  • Routing- Running different code based on the URL path
  • Http Method Switchng -Running different code based on Http request methods.

Below is sample code for how Ring and Compojure works!

---End of Article---