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

Clojure Web-Application 101

Michael Vitz
November 09, 2016

Clojure Web-Application 101

Clojure is a JVM language which makes the impossible come true: a pragmatic and usable Lisp for humans. Besides an short introduction to the language this talk introduces how to develop Web-Applications in Clojure. If you aren't afraid of parenthesis you will quickly understand why a functional language like Clojure is a perfect choice for implementing Web-Applications and that it fits the stateless HTTP approach very nicely.

Michael Vitz

November 09, 2016
Tweet

More Decks by Michael Vitz

Other Decks in Programming

Transcript

  1. @michaelvitz #Devoxx #Clojure Clojure • Lisp on the JVM •

    Functional • Dynamically typed • Immutable data structures • macros
  2. @michaelvitz #Devoxx #Clojure Data structures “Hello World" 3 3.14 3/2

    \a :first foo #“Ch.*se” (“Hello” :first) [3 4 3] { :name “Michael”, :company “innoQ” } #{3 4 3} true nil false
  3. @michaelvitz #Devoxx #Clojure Functions (+ 1 2) > 3 (:city

    {:name “Devoxx” :city “Antwerp”}) > “Antwerp” (map inc [1 2 3]) > (2 3 4)
  4. @michaelvitz #Devoxx #Clojure Functions (fn [x y] (+ x y))

    (def add (fn [x y] (+ x y))) (defn add
 [x y] (+ x y))
  5. @michaelvitz #Devoxx #Clojure Ring: Handler (defn greet-controller
 [request] {:status 200


    :headers {“Content-Type” “text/plain”} :body “Hello, world!”})
  6. @michaelvitz #Devoxx #Clojure Ring: Middleware (defn wrap-logging
 [handler] (fn [request]

    (print request)
 (let [response (handler request)] (print response) response)))
  7. @michaelvitz #Devoxx #Clojure Compojure: Handler (def handler (GET “/hello” []

    “Hello, world!”)) (handler {:request-method :get, :uri “/hello”}) > {:body “Hello, world!”} (handler {:request-method :post, :uri “/hello”}) > nil
  8. @michaelvitz #Devoxx #Clojure Compojure: Macro “(case 3 1 “one” 2

    “two” “more”)” Reader Text Data Structures (case 3 1 “one” 2 “two” “more”)
  9. @michaelvitz #Devoxx #Clojure Data Structures Compojure: Macro Evaluator (case 3

    1 “one” 2 “two” “more”) Data Structures (if (= 3 1) “one” (if (= 3 2) “two” “more”))
  10. @michaelvitz #Devoxx #Clojure Compojure: Macro (GET “/hello” [] “Hello, world!”)

    (fn [req] (if (and (= (:uri req) “/hello”) (= (:request-method req) :get)) {:body “Hello, world!”}))
  11. @michaelvitz #Devoxx #Clojure Compojure: Routes (def my-routes (routes (GET “/hello”

    [] “Hello!”) (GET “/bye” [] “Bye!”))) (defroutes my-routes (GET “/hello” [] “Hello!”) (GET “/bye” [] “Bye!”))
  12. @michaelvitz #Devoxx #Clojure Wrap-Up • Functional language fits to HTTP

    cycle • Clojure usage increases • Libraries vs. Frameworks • Healthy and welcome community
  13. @michaelvitz #Devoxx #Clojure Interesting libraries • https://github.com/technomancy/leiningen • https://github.com/weavejester/environ •

    https://github.com/clojurewerkz/route-one • https://github.com/krisajenkins/yesql • https://github.com/seancorfield/clj-time