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
 (:require
 [ring.middleware.json :refer [wrap-json-response]]
 [ring.util.response :refer [response]]
 [compojure.core :refer [GET defroutes routes]]
 [compojure.route :refer [not-found resources]]
 [hiccup.page :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 []
 (html5
  (head)
  [:body {:class "body-container"}
  [:p "hello world"]]))


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

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

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



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

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s