Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Joy Clark Consultant @ innoQ joy.clark@innoq.com @iamjoyclark

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

@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}

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

@iamjoyclark Web Applications

Slide 10

Slide 10 text

@iamjoyclark What’s a Web Application?

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

@iamjoyclark Ring

Slide 13

Slide 13 text

@iamjoyclark Ring: Request {:uri “/”
 :params { :name “Joy” } :request-method :get :headers { … } …}

Slide 14

Slide 14 text

@iamjoyclark Ring: Response {:status 200 :headers { … }
 :body “Hello!”}

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

@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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

@iamjoyclark Compojure

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

@iamjoyclark Putting it all together

Slide 23

Slide 23 text

@iamjoyclark HTML Templating

Slide 24

Slide 24 text

@iamjoyclark Hiccup: Basics [:div {:id “foo”} [:span “bar”]]
bar
https://github.com/weavejester/hiccup

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

@iamjoyclark Hiccup: Links (link-to “www.innoq.com” “innoQ”) innoQ [:a {:href “www.innoq.com”} “innoQ”]

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

@iamjoyclark Summary

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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