How to add ring to this code


Im now busy to turn this project into a real web project but I do not see how I can add ring to it.
So far I have this :

(ns paintings.core
  (:require [cheshire.core :as json]
            [clj-http.client :as client]
            [compojure.core :refer :all]
            [compojure.route :as route]))

(defn image-url-size [image]
  (let [data (select-keys image [:width :height])
        url (get-in image [:tiles 0 :url])]
    (assoc data :url url)))

(defn take-image-data [image-data object-number]
  (->> image-data
       (sort-by :name)
       (merge {:object-number object-number})))

(defn assoc-image [object-number]
  (-> (client/get (str "" object-number "/tiles")
                  {:query-params {:key "14OGzuak"
                                  :format "json"}})
      (json/parse-string true)
      (take-image-data object-number)))

(defn take-data [api-data]
  (->> api-data
       (map :objectNumber)
       (map assoc-image)))

(defn display-data []
  (-> (client/get ""
                  {:query-params {:key "14OGzuak"
                                  :format "json"
                                  :type "schilderij"
                                  :toppieces "True"}})
      (json/parse-string true)

(defroutes app
  (GET "/" [] (display-data)))

(run-jetty handler {:port  3000
                    :join? false})

You should probably start with a ring tutorial…

Well, the good news is that Ring is already there. Ring is part of Compojure.

By the way, the approach you have taken - attacking the hard part first, and the low-level parts before the layer that serves those parts as a web app - or in the lingo of software engineering, an approach that is both “risk-based” and “bottom-up” - is a powerful combination with Clojure.


But I do not see how to add ring to my code.
So maybe I can better look at the tutorial that is shown to me.

I think you need to require [ring.adapter.jetty :refer [run-jetty]]

Cause now you use the run-jetty function but it’s not coming from anywhere.

Then, you need to pass the routes as the handler to run-jetty:

(run-jetty app {:port 3000 ...})

And you’ll have to have added [ring "1.8.2"] to your project dependencies as well. Or at least add [ring/ring-jetty-adapter "1.8.2"].

The call to run-jetty will start a Jetty web server that will use your compojure defined app routes to direct http requests to your handlers and return their requests.

So after that call, just go to: http://localhost:3000 and you should see your page.

By the way, if you just run your code, set join? to true, but if you’re going to run it at the REPL then set it to false.

1 Like

thanks that worked

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.