Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Programming in Clojure Barry Demchak CSE294 November 12, 2010.

Similar presentations


Presentation on theme: "Web Programming in Clojure Barry Demchak CSE294 November 12, 2010."— Presentation transcript:

1 Web Programming in Clojure Barry Demchak CSE294 November 12, 2010

2 Agenda Different REPLs – Eclipse with Counterclockwise – Leiningen Compojure Web Framework – Application structure – Framework execution model – High level walkthrough – Addons Alternate Frameworks

3 REPL with Eclipse g_Started_with_Eclipse_and_Counterclockwise g_Started_with_Eclipse_and_Counterclockwise Install Counterclockwise – Plugin for Eclipse – Create and run simple Clojure project (firstClojureProject) – Run REPL in console window Install the labrepl project (and git/maven) – Good online tutorial

4 REPL with Leiningen (“lein”) First, install Leiningen – a build tool for Clojure – ting_Started_with_Leiningen ting_Started_with_Leiningen – Installation downloads itself Use Lein to create a project – Create.clj project file – Use editor to add dependencies – Use Lein to satisfy dependencies – Use Lein to start a REPL https://github.com/weavejester/compojure/wiki/Getting-Started

5 Agenda Different REPLs – Eclipse with Counterclockwise – Leiningen Compojure Web Framework – Application structure – Framework execution model – High level walkthrough – Addons Alternate Frameworks

6 Compojure Components Abstract HTTP as {request} and {response} Enhance {request} and route to user- defined handler Transform {request} into {response} Compojure component Application component DSL for writing HTML

7 Compojure Execution Model What is a “handler”? {request} is a map – :server-port 80 – :server-name “ ” – :remote-addr “ ” – :uri “/” – :scheme :http – :headers {} – :request-method :get {response} is a map – :status 200 – :headers {:X-lang “Clojure” :X- Framework “Compojure”} – :body “Hello world” {request}  handler  {response} Preview: Simplifying handler construction is Compojure’s key value proposition

8 The “Ring”

9 ( ns example-app (:use compojure.server.jetty) ) ( defn hello-world [request] {:status 200 :headers {} :body "Hello World"} ) ( run-server {:port 8080} "/*" (servlet hello-world)) A Simple Servlet Using Ring

10 Getting Started with Compojure https://github.com/weavejester/compojure/w iki/Getting-Started https://github.com/weavejester/compojure/w iki/Getting-Started Use lien to create a project and dependencies

11 Agenda Different REPLs – Eclipse with Counterclockwise – Leiningen Compojure Web Framework – Application structure – Framework execution model – High level walkthrough – Addons Alternate Frameworks

12 A Simple Compojure Web Server (ns hello-www.core (:use compojure.core) (:require [compojure.route :as route]) ) (defroutes example (GET "/" [] " Hello World Wide Web! ") (route/not-found "Page not found") )

13 Clojure Interlude – Useful Macros (def hello-world (-> hello-world (with-header "X-Lang" "Clojure" ) ( with-header "X-Framework" "Compojure") )) (with-header hello-world "X-Lang" "Clojure" ) "X-Framework" "Compojure" ) (decorate hello-world (with-header "X-Lang" "Clojure" ) (with-header "X-Framework" "Compojure" ))

14 Compojure Middleware ( defn with-header [handler header value] (fn [request] ( let [response (handler request)] ( assoc-in response [:headers header] value)))) ( decorate hello-world ( with-header "X-Lang" "Clojure" ) ( with-header "X-Framework" "Compojure")) handler & args  middleware  handler

15 Compojure Middleware (cont’d) Route Handlers return nil if criteria not met – (defn index-route [request] (if (and (= (:request-method request) :get) (= ( :uri request) "/" )) {:status 200 :headers {} :body "The index page")) – (def index-route (GET “/” “The index page” ))

16 Route Components HTTP command to match Pattern of URI to match Extract attributes into local values Generate response

17 Index Routes ( def index-route ( GET "/" "The index page" )) … ( GET "/product/:id" ( str "You chose product: " ( -> request :route-params :id ))) … ( GET #"/product/(\d+)" ( str "You chose product: " (( :route-params request ) 0 ))) Method macro (GET, PUT, POST …) Variable capture Regular expression matching

18 Index Routes (cont’d) (def echo-typed-url-route (GET "*" {:keys [scheme server-name server-port uri]} (str (name scheme) "://" server-name ":“ server-port uri)) ) (defroutes main-routes (POST "/save" {form-params :form-params} (str form-params )) Destructuring defroutes wraps wrap-params, wrap-cookies around handler

19 Destructuring and Local Bindings Additional functions update namespace local vars – with-params (:params) – with-cookies (:cookies) – with-multipart – with-session (:session) – with-stacktrace Many more documented at ub-freebit.net:8080/docs/api

20 Response Values Base class - {:status 200, :headers {}} Modification depends on class of modifier – Integer sets status code – String adds to response body – ISeq, File, and InputStream set response body – URL opens stream and sets response body – Map merges into return value map – Fn produces response from request & response maps – IPersistentVector updates response map

21 Response Value Examples …( GET “/” “Index page” ) …( ANY “*” [404 “Page not found”] ) …( GET “/image” (File. “./public/image.png”) ) …( GET “/new-product” (if product-released? “Cool!” :next) ) …( GET “/map-example” {:body “Hi there!”} )

22 HTML Compojure (Hiccup) [:h1 {:id “title”} “Hello”] [:h1#id “title” “Hello”] [:div (list “Hello” “ Ingolf”)] [:div [:span “Hello”] [:span “ Ingolf”]] {tag attribute-map content} Hello Hello Ingolf Hello Ingolf Clojure HTML

23 Agenda Different REPLs – Eclipse with Counterclockwise – Leiningen Compojure Web Framework – Application structure – Framework execution model – High level walkthrough – Addons Alternate Frameworks

24 Compojure Addons Sandbar – Session and flash as a global map – Authorization and authentication (form-based) – Forms and form validation https://github.com/brentonashworth/sandbar

25 Other Web Frameworks Enlive – light weight, non-inference routing Webjure – light weight, provides routing and parsing on top of Java servlets, Clojure Page Templates Conjure – Rails-like framework for Clojure Moustache – a micro web framework/internal DSL to wire Ring handlers and middlewares Funkyweb – ring based framework with route inference https://github.com/cgrand/moustache https://github.com/tatut/Webjure

26 Enlive Example (ns sprout.core) (defn router [req] (condp = (:uri req) "/error" {:status 404 :headers {"Content-Type" "text/html"} :body (str "Error")} "/info" {:status 200 :headers {"Content-Type" "text/html"} :body (str "Ring on " (:server-name req)) } {:status 200 :headers {"Content-Type" "text/html"} :body (str "Welcome")} )) (defn app [req] (router req))

27 Questions Why is Compojure named Compojure? What are the requirements of the underlying Java- based web server to support? – Scalability – Extensibility How would Compojure participate in web standards? – Web Services (non-Rest) – WS-* In the long run, does Compojure need Java? Does Java need Compojure?

28 Other References Clojure Web Development with Ring Compojure: Clojure Web Framework https://github.com/blog/308-compojure- clojure-web-frameworkhttps://github.com/blog/308-compojure- clojure-web-framework How do you make a web application in Clojure? application-in-clojure application-in-clojure Introduction to the Compojure web Framework pli=1 pli=1 mmcgrana’s ring at master https://github.com/mmcgrana/ringhttps://github.com/mmcgrana/ring weavejester‘s compojure at master https://github.com/weavejester/compojurehttps://github.com/weavejester/compojure weavejester’s hiccup at master https://github.com/weavejester/hiccuphttps://github.com/weavejester/hiccup WEB DEVELOPMENT – Mature Clojure web frameworks arbscht‘s clout at string-keys https://github.com/arbscht/clout/tree/string-keyshttps://github.com/arbscht/clout/tree/string-keys cgrand‘s enlive at master https://github.com/cgrand/enlivehttps://github.com/cgrand/enlive


Download ppt "Web Programming in Clojure Barry Demchak CSE294 November 12, 2010."

Similar presentations


Ads by Google