Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clojure-Web-Apps.pdf

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.

Joy Heron

February 22, 2018
Tweet

More Decks by Joy Heron

Other Decks in Technology

Transcript

  1. Web
    Applications
    Clojure
    BERLIN, 22 Feb 2018

    JOY CLARK

    View Slide

  2. 2
    Web Applications Clojure
    Joy Clark
    Consultant @ innoQ
    [email protected]
    @iamjoyclark

    View Slide

  3. Web Applications
    3
    Web Applications Clojure

    View Slide

  4. retrieving
    a page
    4
    Web Applications ❤ Clojure
    Joy Clark / @iamjoyclark

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  8. 8
    Web Applications Clojure

    View Slide

  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

    View Slide

  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}

    View Slide

  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)

    View Slide

  12. 12
    Web Applications ❤ Clojure
    „You’ve just seen it“
    - Rich Hickey
    Clojure Syntax:

    View Slide

  13. Web Applications + Clojure =
    13
    Web Applications Clojure

    View Slide

  14. 14
    Something that takes a
    HTTP Request and returns a
    HTTP Response
    Web Applications ❤ Clojure
    What’s a Web Application?
    Joy Clark / @iamjoyclark

    View Slide

  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

    View Slide

  16. 16
    {:uri “/”
    :request-method :get
    :headers { … }

    :params {:name “Joy”}
    …}
    Web Applications ❤ Clojure
    Ring: Request
    Joy Clark / @iamjoyclark

    View Slide

  17. 17
    {:status 200
    :headers { … }

    :body “Hello!”}
    Web Applications ❤ Clojure
    Ring: Response
    Joy Clark / @iamjoyclark

    View Slide

  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

    View Slide

  19. 19
    • Jetty
    • Servlet
    • http-kit
    • Tomcat

    Web Applications ❤ Clojure
    Ring: Adapter
    Joy Clark / @iamjoyclark

    View Slide

  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

    View Slide

  21. Ring: Middleware
    21
    • https://github.com/ring-clojure/ring-defaults
    • https://github.com/ring-clojure/ring-json
    Web Applications ❤ Clojure
    Joy Clark / @iamjoyclark

    View Slide

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

    View Slide

  23. 23
    Excursion… Clojure Macros
    Web Applications Clojure

    View Slide

  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”)

    View Slide

  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”))

    View Slide

  26. Back to
    Compojure…
    26
    Web Applications ❤ Clojure
    Joy Clark / @iamjoyclark

    View Slide

  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!”}))

    View Slide

  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

    View Slide

  29. 29
    • https://github.com/juxt/bidi
    • http://clojure-liberator.github.io/liberator/
    Web Applications ❤ Clojure
    Compojure: Alternatives
    Joy Clark / @iamjoyclark

    View Slide

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

    View Slide

  31. 31
    HTML Templating
    Web Applications ❤ Clojure

    View Slide

  32. Hiccup
    32

    bar

    [:div {:id “foo”}
    [:span “bar”]]
    Web Applications ❤ Clojure
    Joy Clark / @iamjoyclark
    https://github.com/weavejester/hiccup

    View Slide

  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

    View Slide

  34. Hiccup: Alternatives
    34
    • https://github.com/cgrand/enlive
    • https://github.com/yogthos/Selmer
    Web Applications ❤ Clojure
    Joy Clark / @iamjoyclark

    View Slide

  35. 35
    Summary
    Web Applications ❤ Clojure

    View Slide

  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

    View Slide

  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

    View Slide

  38. “Frameworks”
    38
    • https://github.com/weavejester/duct
    • http://www.luminusweb.net
    • https://github.com/metosin/compojure-api
    Web Applications ❤ Clojure
    Joy Clark / @iamjoyclark

    View Slide

  39. 39
    Live Demo!
    Web Applications Clojure

    https://github.com/innoq/tutorial-clj-webapp

    View Slide

  40. 40
    State in Clojure…
    Web Applications Clojure

    View Slide

  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

    View Slide

  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

    View Slide

  43. 43
    Web Applications ❤ Clojure
    swap!
    Joy Clark / @iamjoyclark

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  47. CSRF
    Protection
    47
    Web Applications ❤ Clojure
    Joy Clark / @iamjoyclark

    View Slide

  48. 48
    Thank you!
    Joy Clark | @iamjoyclark | [email protected]
    https://github.com/innoq/tutorial-clj-webapp

    View Slide