Clojure Web Development



Similar documents
Clojure Web Development

N2O Most Powerful Erlang Web

Table of Contents. Open-Xchange Authentication & Session Handling. 1.Introduction...3

Frontend- Entwicklung mit React/Reacl. Michael

People Data and the Web Forms and CGI CGI. Facilitating interactive web applications

Modern Web Development From Angle Brackets to Web Sockets

A detailed walk through a CAS authentication

Traitware Authentication Service Integration Document

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

CTIS 256 Web Technologies II. Week # 1 Serkan GENÇ

Network Technologies

CloudOYE CDN USER MANUAL

Cloud Elements! Events Management BETA! API Version 2.0

The Web: some jargon. User agent for Web is called a browser: Web page: Most Web pages consist of: Server for Web is called Web server:

10. Java Servelet. Introduction

Outline Definition of Webserver HTTP Static is no fun Software SSL. Webserver. in a nutshell. Sebastian Hollizeck. June, the 4 th 2013

Drupal CMS for marketing sites

Visualizing a Neo4j Graph Database with KeyLines

Startup guide for Zimonitor

Protocolo HTTP. Web and HTTP. HTTP overview. HTTP overview

Modern Web Application Framework Python, SQL Alchemy, Jinja2 & Flask

DRUPAL BASICS WEBSITE DESIGN & DEVELOPMENT. digital.uoregon.edu/drupal-basics

The Other mod_rails: Easy Rails Deployment with JRuby. Nick Sieger Sun Microsystems, Inc

Working with Indicee Elements

Building native mobile apps for Digital Factory

T320 E-business technologies: foundations and practice

Web Container Components Servlet JSP Tag Libraries

Cloud Elements ecommerce Hub Provisioning Guide API Version 2.0 BETA

HOST EUROPE CLOUD STORAGE REST API DEVELOPER REFERENCE

Leveraging Cloud Storage Through Mobile Applications Using Mezeo Cloud Storage Platform REST API. John Eastman Mezeo

Spectrum Technology Platform

Login with Amazon. Getting Started Guide for Websites. Version 1.0

Chapter 27 Hypertext Transfer Protocol

CIS 192: Lecture 10 Web Development with Flask

CONTENT of this CHAPTER

Comparing Dynamic and Static Language Approaches to Web Frameworks

Soft Solutions, Inc. 4-Sight FAX 7.5. Getting Started. Soft Solutions, Inc.

REST services in Domino - Domino Access Services

Ethical Hacking as a Professional Penetration Testing Technique

Android App for SAP Business One. Z3moB1le App Version 1.00 Pagina 1 di 12.

Remote Access API 2.0

Developing ASP.NET MVC 4 Web Applications MOC 20486

Creating a generic user-password application profile

Application layer Web 2.0

Comparing Application Security Tools

How to Get Set Up for the 2014 BE-180 and Request an Extension if Needed

CIS 192: Lecture 10 Web Development with Flask

Hack Yourself First. Troy troyhunt.com

Internet Technologies 4-http. F. Ricci 2010/2011

CREDENTIAL MANAGER IN WINDOWS 7

Design and Functional Specification

css href title software blog domain HTML div style address img h2 tag maintainingwebpages browser technology login network multimedia font-family

The Decaffeinated Robot

REST API Development. B. Mason Netapp E-Series

Getting Started with the icontact API

Authenticate and authorize API with Apigility. by Enrico Zimuel Software Engineer Apigility and ZF2 Team

WIRIS quizzes web services Getting started with PHP and Java

Getting Started Guide for Developing tibbr Apps

Mojolicious. Marcos Rebelo

Mobile development with Apache OFBiz. Ean Schuessler, Brainfood

HTTP - METHODS. Same as GET, but transfers the status line and header section only.

OAuth 2.0 Developers Guide. Ping Identity, Inc th Street, Suite 100, Denver, CO

Programming Autodesk PLM 360 Using REST. Doug Redmond Software Engineer, Autodesk

NoSQL web apps. w/ MongoDB, Node.js, AngularJS. Dr. Gerd Jungbluth, NoSQL UG Cologne,

Themes and Templates Manual FOR ADVANCED USERS

Rails 4 Quickly. Bala Paranj.

MASTERTAG DEVELOPER GUIDE

Handling of "Dynamically-Exchanged Session Parameters"

Easy CramBible Lab DEMO ONLY VERSION Test284,IBM WbS.DataPower SOA Appliances, Firmware V3.6.0

Progressive Enhancement With GQuery and GWT. Ray Cromwell

The Web History (I) The Web History (II)

Rich Internet Applications

Esigate Module Documentation

Domain Name System (DNS)

WP4: Cloud Hosting Chapter Object Storage Generic Enabler

Using Authorize.net for Credit Card Processing in YogaReg

CS615 - Aspects of System Administration

Title page. Alcatel-Lucent 5620 SERVICE AWARE MANAGER 13.0 R7

VoIPon Tel: +44 (0) Fax: +44 (0)

Fachgebiet Technische Informatik, Joachim Zumbrägel

Web Development on the SOEN 6011 Server

Developing ASP.NET MVC 4 Web Applications

Oracle Communications WebRTC Session Controller: Basic Admin. Student Guide

HTTP. Internet Engineering. Fall Bahador Bakhshi CE & IT Department, Amirkabir University of Technology

Developing Web Views for VMware vcenter Orchestrator

Cloud Elements! Marketing Hub Provisioning and Usage Guide!

Oracle Hyperion Financial Management Developer and Customization Guide

Module 45 (More Web Hacking)

Web application Architecture

CIS 455/555: Internet and Web Systems

ULTEO OPEN VIRTUAL DESKTOP OVD WEB APPLICATION GATEWAY

MatrixSSL Getting Started

Link and Sync Guide for Hosted QuickBooks Files

Electronic Ticket and Check-in System for Indico Conferences

Internet Technologies. World Wide Web (WWW) Proxy Server Network Address Translator (NAT)

Transcription:

Clojure Web Development Philipp Schirmacher We'll take care of it. Personally.

groups.google.com/group/clojure-dus 2012 innoq Deutschland GmbH

Agenda Clojure Basics Web Development Libraries Micro Framework Demo

Generic Data Types

{:name "Clojure" :features [:functional :jvm :parens] :creator "Rich Hickey" :stable-version {:number "1.4" :release "2012/04/18"}}

Functions

(+ 1 2) > 3 (:city {:name "innoq" > "Monheim" :city "Monheim"}) (map inc [1 2 3]) > (2 3 4)

(defn activity [weather] (if (nice? weather) :surfing :playstation))

(defn make-adder [x] (fn [y] (+ x y))) (def add-two (make-adder 2)) (add-two 3) > 5

Web Development?

request maps app function response

Ring

(defn hello-world-app [req] {:status 200 :headers {"Content-Type" "text/plain"} :body "Hello, World!"}) (hello-world-app {:uri "/foo" > {...} :request-method :get}) (run-jetty hello-world-app {:port 8080})

(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>")})

request response static resources homepage

(defn decorate [webapp] (fn [req]...before-webapp... (webapp req)...after-webapp...))

(defn decorate [webapp] (fn [req] (if (static-resource? req) (return-resource req) (webapp req))))

(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}) (handler request))))))

(defn my-first-homepage [req]...) (def webapp (wrap-resource my-first-homepage "public")) (run-jetty webapp {:port 8080})

(webapp {:uri "/pretty.css" :request-method :get :headers {}}) > {:status 200 :headers {} :body #<File...resources/public/pretty.css>}

request response wrap-file-info wrap-resource my-first-homepage

(defn homepage [req]...) (def webapp (-> homepage (wrap-resource "public") wrap-file-info)) (wrap-file-info (wrap-resource homepage "public")) (run-jetty webapp {:port 8080})

(webapp {:uri "/pretty.css" :request-method :get :headers {}}) > {:status 200 :headers {"Content-Length" "16" "Last-Modified" "Thu, 14 Jun..." "Content-Type" "text/css"} :body #<File...resources/public/pretty.css>}

wrap-resource wrap-file wrap-params wrap-session wrap-flash wrap-etag wrap-basic-authentication

request response middleware... middleware routing handler

Compojure

(def get-handler (GET "/hello" [] "Hello, World!")) (get-handler {:request-method :get :uri "/hello"}) > {:body "Hello, World!"...} (get-handler {:request-method :post > nil :uri "/hello"})

(def get-handler (GET "/hello/:name" [name] (str "Hello, " name "!"))) (def post-handler (POST "/names" [name] (remember name) (redirect (str "/hello/" name))))

Digression: Macros

text Compiler bytecode

text Reader data structures Evaluator bytecode (if true (if true "this is true" "this is false") "this is true" "this is false")

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

(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!")) it works!

text Reader data structures Evaluator bytecode (GET "/hello" [] "Hello, World!")) (GET "/hello" [] "Hello, World!") (fn [req] GET-Macro (if (and (match (:uri req) "/hello") (= (:request-method req) :get)) {:body "Hello, World!" } nil))

Back to Compojure... 2012 innoq Deutschland GmbH

(def get-handler (GET "/hello/:name" [name] (str "Hello, " name "!"))) (def post-handler (POST "/names" [name] (remember name) (redirect (str "/hello/" name))))

(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))) (redirect "/todos")))

(defroutes more-routes (context "/todos/:id" [id] (DELETE "/" [] (delete-todo id) (redirect "/todos")) (PUT "/" {json-stream :body} (update-todo (read-json (slurp json-stream))) (redirect (str "/todos/" id)))))

(defroutes complete-app todo-app more-routes (not-found "Oops.")) (def secure-app (wrap-basic-authentication complete-app allowed?)) (run-jetty (api secure-app) {:port 8080})

request response middleware... middleware routing JSON HTML handler

Hiccup

<element attribute="foo"> <nested>bar</nested> </element> [:element]

<element attribute="foo"> <nested>bar</nested> </element> [:element {:attribute "foo"}]

<element attribute="foo"> <nested>bar</nested> </element> [:element {:attribute "foo"} [:nested]]

<element attribute="foo"> <nested>bar</nested> </element> [:element {:attribute "foo"} [:nested "bar"]]

<html> 2012 innoq Deutschland GmbH <head><title>foo</title></head> <body><p>bar</p></body> </html> (def hiccup-example [:html [:head [:title "Foo"]] [:body [:p "Bar"]]]) (html hiccup-example) > "<html>...</html>"

(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)) > "<dl><dt>name</dt><dd>paul</dd>...</dl>"

(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")) > [:form {:action "POST"...} [:input...]...]

<div id="my-id" class="class1 class2"> foo </div> [:div#my-id.class1.class2 "foo"]

request response middleware... middleware routing JSON HTML handler

javax.servlet.http. HttpServletRequest javax.servlet.http. HttpServletRespone servlet adapter {} {} middleware... middleware routing JSON HTML handler

Noir

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. Auto reload in dev mode

Conclusion

:-) Simple basic concepts Easy to use Little code (also in libraries) Helpful community Mature eco system

2011 innoq Deutschland GmbH Thank you! Philipp Schirmacher philipp.schirmacher@innoq.com http://www.innoq.com Phone: +49 151 4673 2018 We'll take care of it. Personally.

Task HTTP Basics Routing Libraries Ring Compojure Moustache HTML Hiccup Enlive Persistence clojure.java.jdbc Korma CongoMongo Asynchronous Server JavaScript Micro Framework Aleph ClojureScript Ringfinger Noir