Clojure-Web-Apps.pdf

Aa3555d7ac4b01847ec9f60a930f5ccd?s=47 Joy Heron
February 22, 2018

 Clojure-Web-Apps.pdf

Clojure, as a Lisp dialect running on the JVM, is a purely functional language that also takes advantage of the great library support available in the Java world.

As a functional language, it provides great abstractions for developing web applications, particularly because of how it handles HTTP, a fundamentally stateless protocol. In this talk, I will explain the fundamental concepts behind Clojure web applications and how the Clojure libraries harness the power of the existing Java infrastructure to provide state of the art server technology.

Aa3555d7ac4b01847ec9f60a930f5ccd?s=128

Joy Heron

February 22, 2018
Tweet

Transcript

  1. Web Applications Clojure BERLIN, 22 Feb 2018
 JOY CLARK ❤

  2. 2 Web Applications Clojure Joy Clark Consultant @ innoQ joy.clark@innoq.com

    @iamjoyclark
  3. Web Applications 3 Web Applications Clojure

  4. retrieving a page 4 Web Applications ❤ Clojure Joy Clark

    / @iamjoyclark
  5. forms 5 Web Applications ❤ Clojure Joy Clark / @iamjoyclark

  6. 6 Web Applications ❤ Clojure Joy Clark / @iamjoyclark redirects

  7. JavaScript 7 Web Applications ❤ Clojure Joy Clark / @iamjoyclark

  8. 8 Web Applications Clojure

  9. 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
  10. 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}
  11. Functions 11 Web Applications ❤ Clojure Joy Clark / @iamjoyclark

    (+ 1 2 3) > 6 (:city {:name “INNOQ” :city “Berlin”}) > “Berlin” (map inc [1 2 3]) > (2 3 4)
  12. 12 Web Applications ❤ Clojure „You’ve just seen it“ -

    Rich Hickey Clojure Syntax:
  13. Web Applications + Clojure = 13 Web Applications Clojure ❤

  14. 14 Something that takes a HTTP Request and returns a

    HTTP Response Web Applications ❤ Clojure What’s a Web Application? Joy Clark / @iamjoyclark
  15. 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
  16. 16 {:uri “/” :request-method :get :headers { … }
 :params

    {:name “Joy”} …} Web Applications ❤ Clojure Ring: Request Joy Clark / @iamjoyclark
  17. 17 {:status 200 :headers { … }
 :body “Hello!”} Web

    Applications ❤ Clojure Ring: Response Joy Clark / @iamjoyclark
  18. 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
  19. 19 • Jetty • Servlet • http-kit • Tomcat …

    Web Applications ❤ Clojure Ring: Adapter Joy Clark / @iamjoyclark
  20. 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
  21. Ring: Middleware 21 • https://github.com/ring-clojure/ring-defaults • https://github.com/ring-clojure/ring-json Web Applications ❤

    Clojure Joy Clark / @iamjoyclark
  22. 22 https://github.com/weavejester/compojure Web Applications ❤ Clojure Compojure Joy Clark /

    @iamjoyclark
  23. 23 Excursion… Clojure Macros Web Applications Clojure

  24. 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”)
  25. 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”))
  26. Back to Compojure… 26 Web Applications ❤ Clojure Joy Clark

    / @iamjoyclark
  27. Compojure: Macro 27 (GET “/hello” [] “Hello, world!”) Web Applications

    ❤ Clojure Joy Clark / @iamjoyclark (fn [req] (if (and (= (:uri req) “/hello”) (= (:request-method req) :get)) {:body “Hello, world!”}))
  28. Compojure: Handler 28 (defroutes app-routes (GET “/” request (list-users request))

    (POST “/” {params :params :as request} (add-user request params)) (GET “/:username” [username :as request] (get-user request username))) Web Applications ❤ Clojure Joy Clark / @iamjoyclark
  29. 29 • https://github.com/juxt/bidi • http://clojure-liberator.github.io/liberator/ Web Applications ❤ Clojure Compojure:

    Alternatives Joy Clark / @iamjoyclark
  30. 30 Functional composition! Web Applications ❤ Clojure Putting it all

    together Joy Clark / @iamjoyclark
  31. 31 HTML Templating Web Applications ❤ Clojure <love> ❤ </love>

  32. Hiccup 32 <div id=“foo”> <span>bar</span> </div> [:div {:id “foo”} [:span

    “bar”]] Web Applications ❤ Clojure Joy Clark / @iamjoyclark https://github.com/weavejester/hiccup
  33. 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
  34. Hiccup: Alternatives 34 • https://github.com/cgrand/enlive • https://github.com/yogthos/Selmer Web Applications ❤

    Clojure Joy Clark / @iamjoyclark
  35. 35 Summary Web Applications ❤ Clojure

  36. 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
  37. 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
  38. “Frameworks” 38 • https://github.com/weavejester/duct • http://www.luminusweb.net • https://github.com/metosin/compojure-api Web Applications

    ❤ Clojure Joy Clark / @iamjoyclark
  39. 39 Live Demo! Web Applications Clojure https://github.com/innoq/tutorial-clj-webapp

  40. 40 State in Clojure… Web Applications Clojure

  41. 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
  42. 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
  43. 43 Web Applications ❤ Clojure swap! Joy Clark / @iamjoyclark

  44. 44 CSRF: Cross-Site Request Forgery Web Applications Clojure

  45. CSRF 45 Web Applications ❤ Clojure Joy Clark / @iamjoyclark

  46. Protect Against CSRF 46 • Don't use GET requests to

    change state! <img src="some http get request”> • SameSite=Strict cookie attribute (default in site-defaults!) • https://github.com/ring-clojure/ring-anti-forgery Web Applications ❤ Clojure Joy Clark / @iamjoyclark
  47. CSRF Protection 47 Web Applications ❤ Clojure Joy Clark /

    @iamjoyclark
  48. 48 Thank you! Joy Clark | @iamjoyclark | joy.clark@innoq.com https://github.com/innoq/tutorial-clj-webapp