Web Applications in Clojure

Web Applications in Clojure

Clojure is a JVM language that makes the impossible come true: a Lisp dialect which can actually be read and used by humans. This talk will quickly introduce the Clojure language and describe how to develop web applications with it. Even if you are afraid of parentheses, you will quickly understand why a functional language like Clojure is the perfect choice for implementing web applications, especially since it fits very well with the stateless HTTP approach

Aa3555d7ac4b01847ec9f60a930f5ccd?s=128

Joy Heron

March 01, 2017
Tweet

Transcript

  1. Web Applications in Clojure (Parentheses are nothing to be afraid

    of) Joy Clark Functional Programming Enthusiast
  2. Joy Clark Consultant @ innoQ joy.clark@innoq.com @iamjoyclark

  3. @iamjoyclark http://upload.wikimedia.org/wikipedia/en/1/

  4. @iamjoyclark Clojure > Lisp Variant for the JVM > Functional

    Language > Dynamically Typed > Immutable Data Structures > Simplicity - Separation of Data and Behavior
  5. @iamjoyclark Data Structures “Hello World” 3 3.14 3/2 \a :first

    foo #“Ch.*se” (“Hello” :first) [3 4 3] { :name “Joy” :company “innoQ” } #{3 4 3}
  6. @iamjoyclark Syntax “You’ve just seen it” - Rich Hickey

  7. @iamjoyclark Functions (+ 1 2) > 3 (:city {:name “innoQ”

    :city “Monheim”}) > “Monheim” (map inc [1 2 3]) > (2 3 4)
  8. @iamjoyclark Functions (fn [x y] (+ x y)) (def add

    (fn [x y] (+ x y))) (defn add [x y] (+ x y))
  9. @iamjoyclark Web Applications

  10. @iamjoyclark What’s a Web Application?

  11. @iamjoyclark Ring https://github.com/ring-clojure/ring • HTTP Server abstraction • Request &

    Response are data • Web App is a function
  12. @iamjoyclark Ring

  13. @iamjoyclark Ring: Request {:uri “/”
 :params { :name “Joy” }

    :request-method :get :headers { … } …}
  14. @iamjoyclark Ring: Response {:status 200 :headers { … }
 :body

    “Hello!”}
  15. @iamjoyclark Ring: Handler (defn example-app [request] (let [name (get-in request

    [:params :name])] {:status 200
 :headers {“Content-Type” “text/plain”} :body (str “Hello, ” name “!”)}))
  16. @iamjoyclark Ring: Adapter > Jetty > Servlet > http-kit >

    Tomcat > …
  17. @iamjoyclark Ring: Middleware (defn wrap-logging [handler] (fn [request] (print request)


    (let [response (handler request)] (print response) response))) webapp Java HTTP Server Ring Server Adapter middleware middleware
  18. @iamjoyclark Ring: Middleware > https://github.com/ring-clojure/ring-defaults > https://github.com/ring-clojure/ring-json

  19. @iamjoyclark Compojure https://github.com/weavejester/compojure

  20. @iamjoyclark Compojure

  21. @iamjoyclark Compojure: Routes (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)))
  22. @iamjoyclark Putting it all together

  23. @iamjoyclark HTML Templating

  24. @iamjoyclark Hiccup: Basics [:div {:id “foo”} [:span “bar”]] <div id=“foo”>

    <span>bar</span> </div> https://github.com/weavejester/hiccup
  25. @iamjoyclark Hiccup: Security (defn render-html [input] (hiccup.util/escape-html (hiccup.core/html input))) Make

    sure to escape HTML when using Hiccup!
  26. @iamjoyclark Hiccup: Links (link-to “www.innoq.com” “innoQ”) <a href=“www.innoq.com”> innoQ </a>

    [:a {:href “www.innoq.com”} “innoQ”]
  27. @iamjoyclark Hiccup: Formulare (form-to [:post “/login”] (text-field “Username”) (password-field “Password”)

    (submit-button “Login”))
  28. @iamjoyclark Hiccup: Alternatives > https://github.com/cgrand/enlive > https://github.com/yogthos/Selmer

  29. @iamjoyclark Summary

  30. @iamjoyclark Summary > Functional Language ideal for Web Apps >

    Clojure usage is increasing > Libraries vs. Frameworks > Community is healthy and welcoming
  31. @iamjoyclark Interesting Libraries > https://github.com/technomancy/leiningen > https://github.com/weavejester/environ > https://github.com/krisajenkins/yesql

  32. @iamjoyclark “Frameworks” > https://github.com/weavejester/duct > http://www.luminusweb.net > https://github.com/otto-de/tesla-microservice > https://github.com/metosin/compojure-api

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