• No results found

Clojure Web Development

N/A
N/A
Protected

Academic year: 2021

Share "Clojure Web Development"

Copied!
57
0
0

Loading.... (view fulltext now)

Full text

(1)

We'll take care of it. Personally.

Clojure Web Development

(2)
(3)

© 2012 innoQ Deutschland GmbH

Agenda

Clojure Basics

Web Development

Libraries

Micro Framework

Demo

(4)
(5)

© 2012 innoQ Deutschland GmbH

{:name "Clojure"

:features [:functional :jvm :parens]

:creator "Rich Hickey"

:stable-version {:number "1.4"

(6)
(7)

© 2012 innoQ Deutschland GmbH

(+ 1 2)

> 3

(:city {:name "innoQ"

:city "Monheim"})

> "Monheim"

(map inc [1 2 3])

(8)

(defn activity [weather]

(if (nice? weather)

:surfing

(9)

© 2012 innoQ Deutschland GmbH

(defn make-adder [x]

(fn [y]

(+ x y)))

(def add-two (make-adder 2))

(add-two 3)

(10)
(11)

© 2012 innoQ Deutschland GmbH

app request

response

(12)
(13)

© 2012 innoQ Deutschland GmbH

(defn hello-world-app [req]

{:status 200

:headers {"Content-Type" "text/plain"}

:body "Hello, World!"})

(hello-world-app {:uri "/foo"

:request-method :get})

> {...}

(14)

(defn my-first-homepage [req]

{:status 200

:headers {"Content-Type" "text/html"}

:body (str "<html><head>" "<link href=\"/pretty.css\" ...>" "</head><body>" "<h1>Welcome to my Homepage</h1>" (java.util.Date.) "</body></html>")})

(15)

© 2012 innoQ Deutschland GmbH st at ic re so ur ce s request response homepage

(16)

(defn decorate [webapp]

(fn [req]

...before-webapp...

(webapp req)

(17)

© 2012 innoQ Deutschland GmbH

(defn decorate [webapp]

(fn [req]

(if (static-resource? req)

(return-resource req)

(18)

(defn wrap-resource [handler root-path]

(fn [request]

(if-not (= :get (:request-method request))

(handler request)

(let [path (extract-path request)]

(or (resource-response path {:root root-path})

(19)

© 2012 innoQ Deutschland GmbH

(defn my-first-homepage [req] ...)

(def webapp

(wrap-resource my-first-homepage "public"))

(20)

(webapp {:uri "/pretty.css"

:request-method :get

:headers {}})

> {:status 200 :headers {}

(21)

© 2012 innoQ Deutschland GmbH w ra p-re so ur ce request response my-first-homepage w ra p-fil e-in fo

(22)

(defn homepage [req] ...)

(def webapp

(-> homepage

(wrap-resource "public")

wrap-file-info))

(run-jetty webapp {:port 8080})

(wrap-file-info (wrap-resource homepage

(23)

© 2012 innoQ Deutschland GmbH

(webapp {:uri "/pretty.css"

:request-method :get

:headers {}})

> {:status 200

:headers {"Content-Length" "16"

"Last-Modified" "Thu, 14 Jun ..." "Content-Type" "text/css"}

(24)

wrap-resource

wrap-file

wrap-params

wrap-session

wrap-flash

wrap-etag

wrap-basic-authentication

(25)

© 2012 innoQ Deutschland GmbH m id dl ew ar e request response handler m id dl ew ar e ro ut in g ...

(26)
(27)

© 2012 innoQ Deutschland GmbH

(def get-handler (GET "/hello" []

"Hello, World!"))

(get-handler {:request-method :get

:uri "/hello"})

> {:body "Hello, World!" ...}

(get-handler {:request-method :post

:uri "/hello"})

(28)

(def get-handler

(GET "/hello/:name" [name]

(str "Hello, " name "!")))

(def post-handler

(POST "/names" [name]

(remember name)

(29)

© 2012 innoQ Deutschland GmbH

(30)
(31)

© 2012 innoQ Deutschland GmbH

text Reader data Evaluator

structures bytecode “(if true "this is true" "this is false")” (if true "this is true" "this is false")

(32)

text Reader data Evaluator structures bytecode “(cond (< 4 3) (print "wrong") (> 4 3) (print "yep"))” (cond (< 4 3) (print "wrong") (> 4 3) (print "yep")) (if (< 4 3) (print "wrong") (if (> 4 3) cond-Macro (cond (< 4 3) (print..) (> 4 3) (print..))

(33)

© 2012 innoQ Deutschland GmbH (defmacro my-cond [c1 e1 c2 e2] (list 'if c1 e1 (list 'if c2 e2 nil))) (my-cond

false (println "won't see this") true (println "it works!"))

(34)

text Reader data Evaluator structures bytecode “(GET "/hello" [] "Hello, World!"))” (GET "/hello" [] "Hello, World!") (fn [req]

(if (and (match (:uri req) "/hello")

(= (:request-method req) :get))

{:body "Hello, World!" …}

(35)

© 2012 innoQ Deutschland GmbH

(36)

(def get-handler

(GET "/hello/:name" [name]

(str "Hello, " name "!")))

(def post-handler

(POST "/names" [name]

(remember name)

(37)

© 2012 innoQ Deutschland GmbH (defroutes todo-app (GET "/todos" [] (render (load-all-todos))) (GET "/todos/:id" [id] (render (load-todo id)))

(POST "/todos" {json-stream :body}

(create-todo (read-json (slurp json-stream)))

(38)

(defroutes more-routes

(context "/todos/:id" [id]

(DELETE "/" []

(delete-todo id)

(redirect "/todos"))

(PUT "/" {json-stream :body}

(update-todo (read-json (slurp json-stream)))

(39)

© 2012 innoQ Deutschland GmbH (defroutes complete-app todo-app more-routes (not-found "Oops.")) (def secure-app

(wrap-basic-authentication complete-app allowed?))

(40)

m id dl ew ar e request response handler m id dl ew ar e ro ut in g ... JS O N H TM L

(41)

© 2012 innoQ Deutschland GmbH

(42)

<element attribute="foo"> <nested>bar</nested>

</element>

(43)

© 2012 innoQ Deutschland GmbH

<element attribute="foo"> <nested>bar</nested>

</element>

(44)

<element attribute="foo"> <nested>bar</nested>

</element>

[:element {:attribute "foo"}

(45)

© 2012 innoQ Deutschland GmbH

<element attribute="foo"> <nested>bar</nested>

</element>

[:element {:attribute "foo"}

(46)

<html> <head><title>Foo</title></head> <body><p>Bar</p></body> </html> (def hiccup-example [:html

[:head [:title "Foo"]]

(47)

© 2012 innoQ Deutschland GmbH

(def paul {:name "Paul" :age 45})

(defn render-person [person]

[:dl

[:dt "Name"] [:dd (:name person)]

[:dt "Age"] [:dd (:age person)]])

(html (render-person paul))

(48)

(link-to "http://www.innoq.com" "click here")

> [:a {:href "http://www.innoq.com"} "click here"]

(form-to [:post "/login"]

(text-field "Username")

(password-field "Password")

(submit-button "Login"))

(49)

© 2012 innoQ Deutschland GmbH

<div id="my-id" class="class1 class2"> foo

</div>

(50)

m id dl ew ar e request response handler m id dl ew ar e ro ut in g ... JS O N H TM L

(51)

© 2012 innoQ Deutschland GmbH m id dl ew ar e {} {} handler m id dl ew ar e ro ut in g ... JS O N H TM L se rv le t a da pt er javax.servlet.http. HttpServletRequest javax.servlet.http. HttpServletRespone

(52)
(53)

© 2012 innoQ Deutschland GmbH

www.webnoir.org

Integration of Ring/Compojure/Hiccup

defpage

for defining routes

Some middleware preconfigured

Parameter parsing, static resources, 404 page

etc.

Helpers for form validation etc.

(54)
(55)

© 2012 innoQ Deutschland GmbH

:-)

Simple basic concepts

Easy to use

Little code (also in libraries)

Helpful community

(56)

Thank you!

Philipp Schirmacher

[email protected]

http://www.innoq.com

(57)

© 2012 innoQ Deutschland GmbH Task Libraries HTTP Basics Ring Routing Compojure Moustache HTML Hiccup Enlive Persistence clojure.java.jdbc Korma CongoMongo

Asynchronous Server Aleph

JavaScript ClojureScript

Micro Framework Ringfinger

References

Related documents

EagleView’s complaint touted the close competition between Respondents, alleging, “Xactware has developed a product, known as Aerial Sketch, which enables it to compete directly with

(In the Aconcagua, even the charterers’ expert, an experienced container ship master, said that stow plans were normally checked after the vessel had sailed and if something was

Biogas Valve Biogas CHP Exaust Gas (Stack) Timber flooring Biogas Valve Case Study 1:. Fire

It compares the employees who were “hired before automatic enrollment” with the employees who were “hired after automatic enrollment.” (Recall that in Companies A and B, auto-

Table 5.3: Comparing Key Narratives in Open Public Services, The Way Ahead, Scottish Enterprise Business Plan and Renewing Scotland’s Public Services 177.. Table 8.1:

Therefore, when leaving the domestic environment, organizations are motivation to leave the domestic political arena more so by the need to protect or promote a specific industry or

Table 1.0 shows that there exists a direct relationship between the revenue allocation formula as prioxies by the share of federal, state and local government

◆ Guidelines for planning, implementing, and reviewing a comprehensive maintenance training program ◆ Easy-to-use techniques for improving your “Maintenance IQ” and your value to