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
  (resource
  :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 O’Reilly online learning.

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