Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH 27.04.10 14:17...

77
We'll take care of it. Personally. Clojure Web Development Philipp Schirmacher | Stefan Tilkov | innoQ Thursday, November 29, 12

Transcript of Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH 27.04.10 14:17...

Page 1: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

We'll take care of it. Personally.

Clojure Web DevelopmentPhilipp Schirmacher | Stefan Tilkov | innoQ

Thursday, November 29, 12

Page 2: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

http://www.innoq.com

Thursday, November 29, 12

Page 3: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

27.04.10 14:17http://upload.wikimedia.org/wikipedia/en/1/1a/Clojure-glyph.svg

Page 1 of 1

Thursday, November 29, 12

Page 4: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

Clojure 27.04.10 14:17http://upload.wikimedia.org/wikipedia/en/1/1a/Clojure-glyph.svg

Page 1 of 1

A practical Lisp variant for the JVM Functional programming

Dynamic Typing Full-featured macro system

Concurrent programming supportBi-directional Java interop

Immutable persistent data structures

Thursday, November 29, 12

Page 5: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

Lisp??

Thursday, November 29, 12

Page 6: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

Lots of irritating silly parentheses?

Thursday, November 29, 12

Page 7: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

Thursday, November 29, 12

Page 8: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

http://xkcd.com/297/

Thursday, November 29, 12

Page 9: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH http://www.flickr.com/photos/nicolasrolland/3063007013/

Thursday, November 29, 12

Page 10: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbHhttp://www.tbray.org/ongoing/When/200x/2008/09/25/-big/R0010774.jpg.html

Rich Hickey

Thursday, November 29, 12

Page 11: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

Clojure Environment

Clojuresque (Gradle)

Leiningen

Thursday, November 29, 12

Page 12: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

Data structuresNumbers 2 3 4 0.234

3/5 -2398989892820093093090292321

Strings "Hello" "World"

Characters \a \b \c

Keywords :first :last

Symbols a b c

Regexps #"Ch.*se"

Lists (a b c)((:first :last "Str" 3) (a b))

Vectors [2 4 6 9 23][2 4 6 [8 9] [10 11] 9 23]

Maps {:de "Deutschland", :fr "France"}

Sets #{"Bread" "Cheese" "Wine"}

Thursday, November 29, 12

Page 13: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

Syntax

Thursday, November 29, 12

Page 14: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

“You’ve just seen it” – Rich Hickey

Thursday, November 29, 12

Page 15: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Generic Data Types

Thursday, November 29, 12

Page 16: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

{:name "Clojure" :features [:functional :jvm :parens] :creator "Rich Hickey" :stable-version {:number "1.4"                  :release "2012/04/18"}}

Thursday, November 29, 12

Page 17: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Functions

Thursday, November 29, 12

Page 18: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(:city {:name "innoQ"        :city "Monheim"})

> "Monheim"

(map inc [1 2 3])

> (2 3 4)

(+ 1 2)> 3

Thursday, November 29, 12

Page 19: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defn activity [weather]  (if (nice? weather)    :surfing    :playstation))

Thursday, November 29, 12

Page 20: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defn make-adder [x]  (fn [y]    (+ x y)))

(def add-two (make-adder 2))

(add-two 3)> 5

Thursday, November 29, 12

Page 21: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Web Development?

Thursday, November 29, 12

Page 22: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

app

request

response

objects object

Thursday, November 29, 12

Page 23: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

public interface Servlet  {        void init(ServletConfig servletConfig)         throws ServletException;        ServletConfig getServletConfig();        void service(ServletRequest servletRequest,                  ServletResponse servletResponse)         throws ServletException, IOException;        String getServletInfo();        void destroy();}

Thursday, November 29, 12

Page 24: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

public interface HttpServletRequest extends ServletRequest {       public String getAuthType();

    public Cookie[] getCookies();              

    public Enumeration<String> getHeaders(String name);

    public Enumeration<String> getHeaderNames();

    public String getMethod();

    public String getQueryString();

    public String getRemoteUser();

    public HttpSession getSession(boolean create);

    public boolean authenticate(HttpServletResponse response)         throws IOException,ServletException;        public void login(String username, String password)         throws ServletException;...

Thursday, November 29, 12

Page 25: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

public interface HttpServletResponse extends ServletResponse {

    public void addCookie(Cookie cookie);

    public boolean containsHeader(String name);       public void sendError(int sc, String msg) throws IOException;

    public void sendRedirect(String location) throws IOException;

    public void setDateHeader(String name, long date);

    public void addDateHeader(String name, long date);

    public void setHeader(String name, String value);

    public void addHeader(String name, String value);

    public void setStatus(int sc);

    public int getStatus();...

Thursday, November 29, 12

Page 26: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

app

request

response

data function

Thursday, November 29, 12

Page 27: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Ring

Thursday, November 29, 12

Page 28: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defn hello-world-app [req]  {:status 200   :headers {"Content-Type" "text/plain"}   :body "Hello, World!"})

(hello-world-app {:uri "/foo"                  :request-method :get})> {...}

(run-jetty hello-world-app {:port 8080})

Thursday, November 29, 12

Page 29: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defn my-first-homepage [req]  {:status 200   :headers {"Content-Type" "text/html"}   :body (str "<html><head>"              "<link href=\"/pretty.css\" ...>"              "</head><body>"              "<h1>Welcome to my Homepage</h1>"              (java.util.Date.)              "</body></html>")})

Thursday, November 29, 12

Page 30: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

stat

ic re

sour

ces

request

response

homepage

Thursday, November 29, 12

Page 31: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defn decorate [webapp]  (fn [req]    ...before webapp...    (webapp req)    ...after webapp...))

Thursday, November 29, 12

Page 32: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defn decorate [webapp]  (fn [req]    (if (static-resource? req)      (return-resource req)      (webapp req))))

Thursday, November 29, 12

Page 33: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defn wrap-resource [handler root-path]  (fn [request]    (if-not (= :get (:request-method request))      (handler request)      (let [path (extract-path request)]        (or (resource-response path {:root root-path})            (handler request))))))

Thursday, November 29, 12

Page 34: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defn my-first-homepage [req] ...)

(def webapp  (wrap-resource my-first-homepage "public"))

(run-jetty webapp {:port 8080})

Thursday, November 29, 12

Page 35: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(webapp {:uri "/pretty.css"         :request-method :get         :headers {}})> {:status 200   :headers {}   :body #<File ...resources/public/pretty.css>}

Thursday, November 29, 12

Page 36: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

wra

p-re

sour

cerequest

response

my-!rst-homepagew

rap-!l

e-in

fo

Thursday, November 29, 12

Page 37: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defn homepage [req] ...)

(def webapp  (-> homepage      (wrap-resource "public")      wrap-file-info))

(run-jetty webapp {:port 8080})

(wrap-file-info (wrap-resource  homepage  "public"))

Thursday, November 29, 12

Page 38: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(webapp {:uri "/pretty.css"         :request-method :get         :headers {}})> {:status 200   :headers {"Content-Length" "16"             "Last-Modified" "Thu, 14 Jun ..."             "Content-Type" "text/css"}   :body #<File ...resources/public/pretty.css>}

Thursday, November 29, 12

Page 39: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

wrap-resource

wrap-!le

wrap-params

wrap-session

wrap-flash

wrap-etag

wrap-basic-authentication

Thursday, November 29, 12

Page 40: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

mid

dlew

arerequest

response

handlerm

iddl

ewar

e

rout

ing

...

Thursday, November 29, 12

Page 41: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Compojure

Thursday, November 29, 12

Page 42: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(def get-handler  (GET "/hello" []       "Hello, World!"))

(get-handler {:request-method :get              :uri "/hello"})> {:body "Hello, World!" ...}

(get-handler {:request-method :post              :uri "/hello"})> nil

Thursday, November 29, 12

Page 43: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(def get-handler  (GET "/hello/:name" [name]       (str "Hello, " name "!")))

(def post-handler  (POST "/names" [name]        (remember name)        (redirect (str "/hello/" name))))

Thursday, November 29, 12

Page 44: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Digression: Macros

Thursday, November 29, 12

Page 45: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

textCompiler

bytecode

Thursday, November 29, 12

Page 46: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

textReader Evaluator

datastructures

bytecode

(if true  (print "true"  (print "false"))

“(if true  (print "true"  (print "false"))”

Thursday, November 29, 12

Page 47: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

textReader Evaluator

datastructures

bytecode

(cond  (< 4 3) (print "wrong")  (> 4 3) (print "yep"))

(if (< 4 3)  (print "wrong")  (if (> 4 3)    (print "yep")    nil))

cond-Macro

(cond  (< 4 3) (pri...  (> 4 3) (pri...

“(cond  (< 4 3) (print "wrong")  (> 4 3) (print "yep"))”

Thursday, November 29, 12

Page 48: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defmacro my-cond [c1 e1 c2 e2]  (list 'if c1        e1        (list 'if c2              e2              nil)))

(my-cond false (println "won't see this") true (println "it works!"))it works!

Thursday, November 29, 12

Page 49: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(my-cond false (println "won't see this") true (println "it works!"))it works!

(defmacro my-cond [c1 e1 c2 e2]  `(if ~c1     ~e1     (if ~c2       ~e2       nil)))

Thursday, November 29, 12

Page 50: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

text Reader Evaluatordatastructures

bytecode

(GET "/hello" []   "Hello, World!")

(fn [req]  (if (and (match (:uri req) "/hello")           (= (:request-method req) :get))    {:body "Hello, World!" …}    nil))

GET-Macro

“(GET "/hello" []   "Hello, World!")”

Thursday, November 29, 12

Page 51: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Back to Compojure...

Thursday, November 29, 12

Page 52: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(def get-handler  (GET "/hello/:name" [name]       (str "Hello, " name "!")))

(def post-handler  (POST "/names" [name]        (remember name)        (redirect (str "/hello/" name))))

Thursday, November 29, 12

Page 53: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defroutes todo-app (GET "/todos" []     (render (load-all-todos))) (GET "/todos/:id" [id]       (render (load-todo id))) (POST "/todos" {json-stream :body}        (create-todo (read-json (slurp json-stream)))        (redirect "/todos")))

Thursday, November 29, 12

Page 54: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defroutes more-routes  (context "/todos/:id" [id] (DELETE "/" []            (delete-todo id)            (redirect "/todos")) (PUT "/" {json-stream :body}         (update-todo (read-json (slurp json-stream)))         (redirect (str "/todos/" id)))))

Thursday, November 29, 12

Page 55: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defroutes complete-app  todo-app  more-routes  (not-found "Oops."))

(def secure-app  (wrap-basic-authentication complete-app allowed?))

(run-jetty (api secure-app) {:port 8080})

Thursday, November 29, 12

Page 56: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

mid

dlew

arerequest

response

handlerm

iddl

ewar

e

rout

ing

...

JSO

NHT

ML

Thursday, November 29, 12

Page 57: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Hiccup

Thursday, November 29, 12

Page 58: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

<element attribute="foo">  <nested>bar</nested></element>

[:element]

Thursday, November 29, 12

Page 59: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

<element attribute="foo">  <nested>bar</nested></element>

[:element {:attribute "foo"}]

Thursday, November 29, 12

Page 60: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

<element attribute="foo">  <nested>bar</nested></element>

[:element {:attribute "foo"} [:nested]]

Thursday, November 29, 12

Page 61: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

<element attribute="foo">  <nested>bar</nested></element>

[:element {:attribute "foo"} [:nested "bar"]]

Thursday, November 29, 12

Page 62: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

<html>  <head><title>Foo</title></head>  <body><p>Bar</p></body></html>

(def hiccup-example  [:html   [:head [:title "Foo"]]   [:body [:p "Bar"]]])

(html hiccup-example)> "<html>...</html>"

Thursday, November 29, 12

Page 63: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(def paul {:name "Paul" :age 45})

(defn render-person [person]  [:dl   [:dt "Name"] [:dd (:name person)]   [:dt "Age"] [:dd (:age person)]])

(html (render-person paul))> "<dl><dt>Name</dt><dd>Paul</dd>...</dl>"

Thursday, November 29, 12

Page 64: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(defn update [{:keys [id text author time]}]  (list [:img.avatar {:src (avatar-uri author) :alt author}]        [:div.content text]        [:div.meta         [:span.author (link-to (str "?author=" author) author)]         [:span.time (link-to (str "/" id) time)]]))

(defn list-page [items next request]  (common/layout   [:div [:ul.updates (map (fn [item] [:li.post (update item)]) items)]]))

Thursday, November 29, 12

Page 65: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

(link-to "http://www.innoq.com" "click here")> [:a {:href "http://www.innoq.com"} "click here"]

(form-to [:post "/login"]         (text-field "Username")         (password-field "Password")         (submit-button "Login"))> [:form {:action "POST" ...} [:input ...] ...]

Thursday, November 29, 12

Page 66: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

<div id="my-id" class="class1 class2">  foo</div>

[:div#my-id.class1.class2 "foo"]

Thursday, November 29, 12

Page 67: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

mid

dlew

arerequest

response

handlerm

iddl

ewar

e

rout

ing

...

JSO

NHT

ML

Thursday, November 29, 12

Page 68: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

mid

dlew

are{}

{}

handlerm

iddl

ewar

e

rout

ing

...

JSO

NHT

ML

serv

let a

dapt

erjavax.servlet.http.HttpServletRequest

javax.servlet.http.HttpServletRespone

Thursday, November 29, 12

Page 69: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Noir

Thursday, November 29, 12

Page 70: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

www.webnoir.org

‣ Integration of Ring/Compojure/Hiccup

‣ defpage for de!ning routes

‣ Some middleware precon!gured‣ Parameter parsing, static resources, 404 page

etc.

‣ Helpers for form validation etc.

‣ Auto reload in dev mode

Thursday, November 29, 12

Page 71: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Demo

Thursday, November 29, 12

Page 72: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Conclusion

Thursday, November 29, 12

Page 73: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

:-)

‣ Simple basic concepts

‣ Easy to use

‣ Little code (also in libraries)

‣ Helpful community

‣ Mature eco system

Thursday, November 29, 12

Page 74: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

Thank you!

Stefan [email protected]@stilkov

We'll take care of it. Personally.

© 2012 innoQ Deutschland GmbH

Philipp [email protected]

Thursday, November 29, 12

Page 75: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Backup

Thursday, November 29, 12

Page 76: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2012 innoQ Deutschland GmbH

Task Libraries

HTTP Basics Ring

Routing Compojure

Moustache

HTML Hiccup

Enlive

Persistence clojure.java.jdbc

Korma

MongerAsynchronous Server Aleph

JavaScript ClojureScript

Micro Framework Ring!nger

Noir

Thursday, November 29, 12

Page 77: Clojure Web Development - XP Days...2012/11/29  · © 2011 innoQ Deutschland GmbH  27.04.10 14:17 Page 1 of 1 A practical Lisp variant for the JVM ...

© 2011 innoQ Deutschland GmbH

Lots of other cool stu! ‣ Persistent data structures

‣ Sequences

‣ Support for concurrent programming

‣ Destructuring

‣ List comprehensions

‣ Metadata

‣ Optiional type information

‣ Multimethods

‣ Pre & Post Conditions

‣ Records/Protocols

‣ Extensive core and contrib libraries

‣ …

Thursday, November 29, 12