compojure, create routes for HTML and JSON

I’m studying clojure, and trying to develop a web-app backend I wanted to define routes for both HTML and JSON responses.

Using ring, compojure, I found this solution:

(ns reagent01.handler
 [ring.middleware.json :refer [wrap-json-response]]
 [ring.util.response :refer [response]]
 [compojure.core :refer [GET defroutes routes]]
 [compojure.route :refer [not-found resources]]
 [ :refer [include-js include-css html5]]
 [reagent01.middleware :refer [wrap-middleware]]
 [ring.middleware.defaults :refer [wrap-defaults site-defaults api-defaults]]
 [config.core :refer [env]]) )

(defn api1 []
 (let [name "Enrico Doe"]
 {:status 200
 :body {:name name
 :desc (str "The name you sent to me was " name)}}))

(defn page1 []
  [:body {:class "body-container"}
  [:p "hello world"]]))

(defroutes apiroutes
 (GET "/api1" [] (api1 )) )

(defroutes approutes
 (GET "/page1" [] (page1))
 (resources "/")
 (not-found "Not Found"))

(def app
 (wrap-json-response apiroutes api-defaults )
 (wrap-defaults approutes site-defaults)))

Probaly a better way exixts, let me know in the comments!



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s