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

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

Joy Heron

March 01, 2017
Tweet

More Decks by Joy Heron

Other Decks in Technology

Transcript

  1. Web Applications in Clojure
    (Parentheses are nothing to be afraid of)
    Joy Clark
    Functional Programming Enthusiast

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  4. @iamjoyclark
    Clojure
    > Lisp Variant for the JVM
    > Functional Language
    > Dynamically Typed
    > Immutable Data Structures
    > Simplicity - Separation of Data and Behavior

    View full-size slide

  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}

    View full-size slide

  6. @iamjoyclark
    Syntax
    “You’ve just seen it”
    - Rich Hickey

    View full-size slide

  7. @iamjoyclark
    Functions
    (+ 1 2)
    > 3
    (:city {:name “innoQ”
    :city “Monheim”})
    > “Monheim”
    (map inc [1 2 3])
    > (2 3 4)

    View full-size slide

  8. @iamjoyclark
    Functions
    (fn [x y] (+ x y))
    (def add
    (fn [x y] (+ x y)))
    (defn add [x y]
    (+ x y))

    View full-size slide

  9. @iamjoyclark
    Web Applications

    View full-size slide

  10. @iamjoyclark
    What’s a Web Application?

    View full-size slide

  11. @iamjoyclark
    Ring
    https://github.com/ring-clojure/ring
    • HTTP Server abstraction
    • Request & Response are data
    • Web App is a function

    View full-size slide

  12. @iamjoyclark
    Ring

    View full-size slide

  13. @iamjoyclark
    Ring: Request
    {:uri “/”

    :params { :name “Joy” }
    :request-method :get
    :headers { … }
    …}

    View full-size slide

  14. @iamjoyclark
    Ring: Response
    {:status 200
    :headers { … }

    :body “Hello!”}

    View full-size slide

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

    View full-size slide

  16. @iamjoyclark
    Ring: Adapter
    > Jetty
    > Servlet
    > http-kit
    > Tomcat
    > …

    View full-size slide

  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

    View full-size slide

  18. @iamjoyclark
    Ring: Middleware
    > https://github.com/ring-clojure/ring-defaults
    > https://github.com/ring-clojure/ring-json

    View full-size slide

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

    View full-size slide

  20. @iamjoyclark
    Compojure

    View full-size slide

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

    View full-size slide

  22. @iamjoyclark
    Putting it all together

    View full-size slide

  23. @iamjoyclark
    HTML Templating

    View full-size slide

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

    bar

    https://github.com/weavejester/hiccup

    View full-size slide

  25. @iamjoyclark
    Hiccup: Security
    (defn render-html [input]
    (hiccup.util/escape-html
    (hiccup.core/html input)))
    Make sure to escape HTML when using
    Hiccup!

    View full-size slide

  26. @iamjoyclark
    Hiccup: Links
    (link-to “www.innoq.com” “innoQ”)

    innoQ

    [:a {:href “www.innoq.com”} “innoQ”]

    View full-size slide

  27. @iamjoyclark
    Hiccup: Formulare
    (form-to [:post “/login”]
    (text-field “Username”)
    (password-field “Password”)
    (submit-button “Login”))

    View full-size slide

  28. @iamjoyclark
    Hiccup: Alternatives
    > https://github.com/cgrand/enlive
    > https://github.com/yogthos/Selmer

    View full-size slide

  29. @iamjoyclark
    Summary

    View full-size slide

  30. @iamjoyclark
    Summary
    > Functional Language ideal for Web Apps
    > Clojure usage is increasing
    > Libraries vs. Frameworks
    > Community is healthy and welcoming

    View full-size slide

  31. @iamjoyclark
    Interesting Libraries
    > https://github.com/technomancy/leiningen
    > https://github.com/weavejester/environ
    > https://github.com/krisajenkins/yesql

    View full-size slide

  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

    View full-size slide

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

    View full-size slide