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. 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
  2. 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}
  3. 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)
  4. 14 Something that takes a HTTP Request and returns a

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

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

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

    Web Applications ❤ Clojure Ring: Adapter Joy Clark / @iamjoyclark
  10. 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
  11. 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”)
  12. 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”))
  13. 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!”}))
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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