Web
Applications
Clojure
BERLIN, 22 Feb 2018
JOY CLARK
❤
Slide 2
Slide 2 text
2
Web Applications Clojure
Joy Clark
Consultant @ innoQ
[email protected]
@iamjoyclark
Slide 3
Slide 3 text
Web Applications
3
Web Applications Clojure
Slide 4
Slide 4 text
retrieving
a page
4
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 5
Slide 5 text
forms
5
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 6
Slide 6 text
6
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
redirects
Slide 7
Slide 7 text
JavaScript
7
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 8
Slide 8 text
8
Web Applications Clojure
Slide 9
Slide 9 text
Clojure
9
• Lisp Variant for the JVM
• Functional Language
• Dynamically Typed
• Immutable Data Structures
• Simplicity - Separation of Data and Behavior
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 10
Slide 10 text
Data Structures
10
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
“Hello World”
3 3.14 3/2
\a
:first
foo
#“Ch.*se”
(“Hello” :first)
[3 4 3]
{:name “Joy”
:company “INNOQ”} #{3 4 3}
12
Web Applications ❤ Clojure
„You’ve just seen it“
- Rich Hickey
Clojure Syntax:
Slide 13
Slide 13 text
Web Applications + Clojure =
13
Web Applications Clojure
❤
Slide 14
Slide 14 text
14
Something that takes a
HTTP Request and returns a
HTTP Response
Web Applications ❤ Clojure
What’s a Web Application?
Joy Clark / @iamjoyclark
Slide 15
Slide 15 text
15
https://github.com/ring-clojure/ring
• HTTP Server abstraction
• Request & Response are data
• Web App is a function
Web Applications ❤ Clojure
Ring
Joy Clark / @iamjoyclark
Slide 16
Slide 16 text
16
{:uri “/”
:request-method :get
:headers { … }
:params {:name “Joy”}
…}
Web Applications ❤ Clojure
Ring: Request
Joy Clark / @iamjoyclark
Slide 17
Slide 17 text
17
{:status 200
:headers { … }
:body “Hello!”}
Web Applications ❤ Clojure
Ring: Response
Joy Clark / @iamjoyclark
Slide 18
Slide 18 text
18
(defn example-app [request]
(let [name (get-in request [:params :name])]
{:status 200
:headers {“Content-Type” “text/plain”}
:body (str “Hello, ” name “!”)})
Web Applications ❤ Clojure
Ring: Handler
Joy Clark / @iamjoyclark
Slide 19
Slide 19 text
19
• Jetty
• Servlet
• http-kit
• Tomcat
…
Web Applications ❤ Clojure
Ring: Adapter
Joy Clark / @iamjoyclark
Slide 20
Slide 20 text
20
(defn wrap-logging [handler]
(fn [request]
(print request)
(let [response (handler
request)]
(print response)
response)))
Web Applications ❤ Clojure
Ring: Middleware
Joy Clark / @iamjoyclark
webapp
Java HTTP Server
Ring Server Adapter
middleware
middleware
Slide 21
Slide 21 text
Ring: Middleware
21
• https://github.com/ring-clojure/ring-defaults
• https://github.com/ring-clojure/ring-json
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 22
Slide 22 text
22
https://github.com/weavejester/compojure
Web Applications ❤ Clojure
Compojure
Joy Clark / @iamjoyclark
Slide 23
Slide 23 text
23
Excursion… Clojure Macros
Web Applications Clojure
Slide 24
Slide 24 text
Clojure: Macros
24
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
“(case 3
1 “one”
2 “two”
“more”)”
Reader
Text Data Structures
(case 3
1 “one”
2 “two”
“more”)
Slide 25
Slide 25 text
Clojure: Macros
25
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Expand Data Structures
(case
1 “one”
2 “two”
“more”)
Data Structures
(if (= 3 1)
“one”
(if (= 3 2)
“two”
“more”))
Slide 26
Slide 26 text
Back to
Compojure…
26
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
29
• https://github.com/juxt/bidi
• http://clojure-liberator.github.io/liberator/
Web Applications ❤ Clojure
Compojure: Alternatives
Joy Clark / @iamjoyclark
Slide 30
Slide 30 text
30
Functional composition!
Web Applications ❤ Clojure
Putting it all together
Joy Clark / @iamjoyclark
Slide 31
Slide 31 text
31
HTML Templating
Web Applications ❤ Clojure
❤
Slide 32
Slide 32 text
Hiccup
32
bar
[:div {:id “foo”}
[:span “bar”]]
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
https://github.com/weavejester/hiccup
Slide 33
Slide 33 text
Hiccup
33
Make sure to escape HTML when using Hiccup!
(defn render-html [input]
(hiccup.util/escape-html
(hiccup.core/html input)))
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 34
Slide 34 text
Hiccup: Alternatives
34
• https://github.com/cgrand/enlive
• https://github.com/yogthos/Selmer
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 35
Slide 35 text
35
Summary
Web Applications ❤ Clojure
Slide 36
Slide 36 text
Summary
36
• Functional language ideal for Web Apps
• Clojure is a stable language
• Libraries vs. Frameworks
• Community is healthy and welcoming
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 37
Slide 37 text
Interesting Libraries
37
• https://github.com/technomancy/leiningen
• https://github.com/boot-clj/boot
• https://github.com/weavejester/environ
• https://github.com/krisajenkins/yesql
• https://github.com/stuartsierra/component
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 38
Slide 38 text
“Frameworks”
38
• https://github.com/weavejester/duct
• http://www.luminusweb.net
• https://github.com/metosin/compojure-api
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 39
Slide 39 text
39
Live Demo!
Web Applications Clojure
https://github.com/innoq/tutorial-clj-webapp
Slide 40
Slide 40 text
40
State in Clojure…
Web Applications Clojure
Slide 41
Slide 41 text
41
Web Applications Clojure
INNOQ /
„No man ever steps in the same river
twice, for it’s not the same river and
he’s not the same man“
- Heraclitus
Slide 42
Slide 42 text
Clojure Identity
42
• The world around us is constantly changing.
• We shouldn’t make the world “stop” so that we can
change it.
• Instead…
• We can give the world a function telling it what we
want to change.
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 43
Slide 43 text
43
Web Applications ❤ Clojure
swap!
Joy Clark / @iamjoyclark
Slide 44
Slide 44 text
44
CSRF: Cross-Site Request Forgery
Web Applications Clojure
Slide 45
Slide 45 text
CSRF
45
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 46
Slide 46 text
Protect Against CSRF
46
• Don't use GET requests to change state!
Slide 47
Slide 47 text
CSRF
Protection
47
Web Applications ❤ Clojure
Joy Clark / @iamjoyclark
Slide 48
Slide 48 text
48
Thank you!
Joy Clark | @iamjoyclark | [email protected]
https://github.com/innoq/tutorial-clj-webapp