Hello world
This is a HelloWorld demo with auto reloading function, a minimal clojure ring web server:
sdk install leiningen
lein new hello
cd hello
cat << EOF > project.clj
(defproject hello "0.1.0-SNAPSHOT"
:description "My Hello World"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
[ring "1.4.0"]
[compojure "1.5.0"]]
:plugins [[lein-ring "0.11.0"]]
:ring {:handler hello.core/app
:auto-reload? true
:auto-refresh? true})
EOF
cat << EOF > src/hello/core.clj
(ns hello.core
(:use compojure.core))
(defroutes app
(GET "/" []
"Hello World"))
EOF
lein ring server
Now a browser is opened at http://localhost:3000/.
Modify Hello World
in file src/hello/core.clj to Hello Ring
,
and refresh browser, you can see the changes.
Note 1: Ring server has no console outputs to let you known it's reloaded.
Note 2: For clojure file editing in vim, see dsnote "Use vim as Clojure Development Environment".
IPython style workflow
You can create and debug functions in REPL, then add them into project files at rumtime. Or load existing functions into REPL and modify them.
For example, zap-ring:
List available ns: (all-ns) Load a ns defined in a file: (load "zap/views") List all functions in a ns: (dir zap.views) Switch to another ns (with all the imported functions): (in-ns 'zap.views)
git clone git@github.com:leetschau/zap-ring.git
cd zap-ring
lein repl
user=> (all-ns)
...
user=> (load "zap/views")
user=> (all-ns)
... # you can find "zap.views", "zap.models" are added into available namespaces
user=> (dir zap.views)
user=> (keys (ns-publics 'zap.views))
user=> (in-ns 'zap.views)
zap.views=> (html [:h1 "Project List"])
zap.views=> (def h1 (html [:h1 "Project List"]))
#'zap.views/h1
zap.views=> (type h1)
clojure.lang.LazySeq
zap.views=> (first h1)
{:tag :h1, :attrs {}, :content ("Project List")}
zap.views=> (type (first h1))
clojure.lang.PersistentArrayMap
zap.views=> (keys (first h1))
(:tag :attrs :content)
zap.views=> (vals (first h1))
(:h1 {} ("Project List"))
Ref: Loading and using namespaces in the Clojure REPL