Defining Resources

Liberator uses the concept of resources to interact with the client. The resources are simply Ring-compliant handlers that can be used inside your Compojure routes. These resources are defined using the resource and the defresource macros. We need to reference these functions in the liberator-service.handler namespace in order to start working with Liberator.

 (ns liberator-service.handler
  (:require ...
  [liberator.core :refer [defresource resource]]))

Now we can replace our routes definition with a resource, as follows:

 (defroutes routes
  (ANY ​"/"​ request
  :handle-ok home-page
  :etag ​"fixed-etag"
  :available-media-types [​"text/html"​])))

Note that we’re using the ANY ...

Get Web Development with Clojure, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.