Compojure HTTP API

Compojure (https://github.com/weavejester/compojure) is a routing library for web applications. It provides a number of methods to handle HTTP requests. There are functions, such as POST, GET, andĀ  PUT, that correspond to HTTP methods.

The final step in writing our microservice is to wire the handler with the compojure-api route:

(def card-routes
[(POST "/cards" []
:body [create-card-req CardRequestSchema]
(create-card-handler create-card-req))])

The first argument is the URI of the route ("/cards"). It matches the URI of a request. The second argument provides a way to retrieve information from the request map if we need it. Next, we instruct Compojure to validate the passed data in body, against CardRequestSchema. The last argument is a handler, which we saw earlier.

In core.clj, we'll use theĀ ring-jetty adapter to expose the card routes:

(ns restful.core
(:require ; ...
[ring.adapter.jetty :refer [run-jetty]]
[compojure.api.sweet :refer [api routes]]
[restful.cards :refer [card-routes]]))
; ...

(def app (apply routes card-routes))

(defn -main
[& args]
; ...
(run-jetty app {:port 3000}))