Clojure Ring

You are reading the original article parsed for readability by Younews Reader as requested by you. Here is the original article: http://www.abhishekamralkar.com/2017-01-05-clojure-ring

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

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.

Handler

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

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.

Ring

Compojure

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!

https://github.com/abhishekamralkar/clojure-ring-example

---End of Article---