Install Boot build tool: brew install boot-clj
.
For JS people: Boot has a notion of tasks, so it's kind of similar to Grunt/Gulp.
build.boot
is yours package.json and gulpfile.
ring-jetty-adapter
provides Java's Jetty web framework, compojure
is a routing library and hiccup
is a lib for writing HTML. The task is defined using deftask
macro, it runs run
function which we :refer
to.
(require '[app.core :refer [run]])
in JS is import { run } from 'app/core';
.
(set-env!
:source-paths #{"src"}
:dependencies '[[org.clojure/clojure "1.6.0"]
[ring/ring-jetty-adapter "1.2.1"]
[compojure "1.4.0"]
[hiccup "1.0.4"]])
(require
'[app.core :refer [run]])
(deftask start []
(set-env! :source-paths #{"src"})
(run))
Let's write actual application code, it's going to be in src/app/core.clj
.
Define namespace and import dependencies.
(ns app.core
(:require [compojure.core :refer :all] ; import * from 'path'
[compojure.handler :as handler]
[compojure.route :as route]
[ring.adapter.jetty :as ring]
[hiccup.page :as page]))
Define routes using defroutes
macro.
(defroutes app-routes
(GET "/" [] "Hello World!")
(route/not-found "Not Found"))
Define entry point run
function. This runs Jetty server on port 3000, handler/site
creates request handler out of routes with a set of middleware for a standard website (cookies/params parser, etc.).
(defn run []
(ring/run-jetty (handler/site app-routes) {:port 3000}))
Execute boot start
from the terminal and navigate to localhost:3000.
Let's add some markup. Create a page using hiccup
.
(defn index []
(page/html5
[:head
[:title "Hello World!"]]
[:body
[:h1 "Aye Aye, Captain!"]]))
And modify routes to render the page.
(defroutes app-routes
(GET "/" [] (index))
(route/not-found "Not Found"))
That's it. Read clojuredocs.org to learn Clojure.