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 Slide

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

    View Slide

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

    View Slide

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

    View 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  9. @iamjoyclark
    Web Applications

    View Slide

  10. @iamjoyclark
    What’s a Web Application?

    View Slide

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

    View Slide

  12. @iamjoyclark
    Ring

    View Slide

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

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

    View Slide

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

    :body “Hello!”}

    View 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 Slide

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

    View 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 Slide

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

    View Slide

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

    View Slide

  20. @iamjoyclark
    Compojure

    View 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 Slide

  22. @iamjoyclark
    Putting it all together

    View Slide

  23. @iamjoyclark
    HTML Templating

    View Slide

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

    bar

    https://github.com/weavejester/hiccup

    View 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 Slide

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

    innoQ

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

    View Slide

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

    View Slide

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

    View Slide

  29. @iamjoyclark
    Summary

    View Slide

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

    View Slide

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

    View 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 Slide

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

    View Slide