Slide 1

Slide 1 text

Web Applications in Clojure Munich Clojurians February 7, 2017

Slide 2

Slide 2 text

Joy Clark Consultant @ innoQ [email protected] @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 Functions (+ 1 2) > 3 (:city {:name “innoQ” :city “Munich”}) > “Munich” (map inc [1 2 3]) > (2 3 4)

Slide 7

Slide 7 text

@iamjoyclark Web Applications

Slide 8

Slide 8 text

@iamjoyclark What’s a Web Application?

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

@iamjoyclark Ring

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 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 14

Slide 14 text

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

Slide 15

Slide 15 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 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

@iamjoyclark Compojure

Slide 19

Slide 19 text

@iamjoyclark Compojure: Handler (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 20

Slide 20 text

@iamjoyclark Putting it all together

Slide 21

Slide 21 text

@iamjoyclark HTML Templating

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

@iamjoyclark Summary

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 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 29

Slide 29 text

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