ring: web apps in idiomatic clojure

52
Ring Web Apps in Idiomatic Clojure Mark McGranaghan January 23, 2010

Upload: mark-mcgranaghan

Post on 17-Jan-2015

14.645 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Ring: Web Apps in Idiomatic Clojure

RingWeb Apps in Idiomatic Clojure

Mark McGranaghan

January 23, 2010

Page 2: Ring: Web Apps in Idiomatic Clojure

About Mark

I Bowdoin College ’10

I Clojure user since 2008

I Open source: http://github.com/mmcgrana

Page 3: Ring: Web Apps in Idiomatic Clojure

Motivation for Ring

I Abstract HTTP

I Write apps in idiomatic Clojure

I Share code across web frameworks

I Deploy on a variety of web servers

I WSGI & Rack

Page 4: Ring: Web Apps in Idiomatic Clojure

Motivation for Ring

I Abstract HTTP

I Write apps in idiomatic Clojure

I Share code across web frameworks

I Deploy on a variety of web servers

I WSGI & Rack

Page 5: Ring: Web Apps in Idiomatic Clojure

Motivation for Ring

I Abstract HTTP

I Write apps in idiomatic Clojure

I Share code across web frameworks

I Deploy on a variety of web servers

I WSGI & Rack

Page 6: Ring: Web Apps in Idiomatic Clojure

Motivation for Ring

I Abstract HTTP

I Write apps in idiomatic Clojure

I Share code across web frameworks

I Deploy on a variety of web servers

I WSGI & Rack

Page 7: Ring: Web Apps in Idiomatic Clojure

Motivation for Ring

I Abstract HTTP

I Write apps in idiomatic Clojure

I Share code across web frameworks

I Deploy on a variety of web servers

I WSGI & Rack

Page 8: Ring: Web Apps in Idiomatic Clojure

Motivation for Ring

I Abstract HTTP

I Write apps in idiomatic Clojure

I Share code across web frameworks

I Deploy on a variety of web servers

I WSGI & Rack

Page 9: Ring: Web Apps in Idiomatic Clojure

The Ring Project

I Spec

I Library

I Ecosystem

Page 10: Ring: Web Apps in Idiomatic Clojure

The Ring Project

I Spec

I Library

I Ecosystem

Page 11: Ring: Web Apps in Idiomatic Clojure

The Ring Project

I Spec

I Library

I Ecosystem

Page 12: Ring: Web Apps in Idiomatic Clojure

The Ring Project

I Spec

I Library

I Ecosystem

Page 13: Ring: Web Apps in Idiomatic Clojure

Overview of Ring

I Handlers are functions

I that take requests as Clojure maps

I and return responses as Clojure map

I Adapters run handlers on a web server

I Middleware augments handlers

Page 14: Ring: Web Apps in Idiomatic Clojure

Overview of Ring

I Handlers are functions

I that take requests as Clojure maps

I and return responses as Clojure map

I Adapters run handlers on a web server

I Middleware augments handlers

Page 15: Ring: Web Apps in Idiomatic Clojure

Overview of Ring

I Handlers are functions

I that take requests as Clojure maps

I and return responses as Clojure map

I Adapters run handlers on a web server

I Middleware augments handlers

Page 16: Ring: Web Apps in Idiomatic Clojure

Overview of Ring

I Handlers are functions

I that take requests as Clojure maps

I and return responses as Clojure map

I Adapters run handlers on a web server

I Middleware augments handlers

Page 17: Ring: Web Apps in Idiomatic Clojure

Overview of Ring

I Handlers are functions

I that take requests as Clojure maps

I and return responses as Clojure map

I Adapters run handlers on a web server

I Middleware augments handlers

Page 18: Ring: Web Apps in Idiomatic Clojure

Overview of Ring

I Handlers are functions

I that take requests as Clojure maps

I and return responses as Clojure map

I Adapters run handlers on a web server

I Middleware augments handlers

Page 19: Ring: Web Apps in Idiomatic Clojure

“Ring”

Page 20: Ring: Web Apps in Idiomatic Clojure

Request Map

{:uri <String>

:query-string <String>

:request-method <Keyword>

:headers <IPersistentMap>

:body <InputStream>

... ...}

Page 21: Ring: Web Apps in Idiomatic Clojure

Response Map

{:status <Integer>

:headers <IPersistentMap>

:body <String, ISeq, File, InputStream>}

Page 22: Ring: Web Apps in Idiomatic Clojure

Example: Hello World

(defn app [req]

{:status 200

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

:body (str "Hi from " (:server-name req))})

(use ’ring.adapter.jetty)

(run-jetty app {:port 8080})

Page 23: Ring: Web Apps in Idiomatic Clojure

Example: Hello World

(defn app [req]

{:status 200

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

:body (str "Hi from " (:server-name req))})

(use ’ring.adapter.jetty)

(run-jetty app {:port 8080})

Page 24: Ring: Web Apps in Idiomatic Clojure

Example: Hello World

Page 25: Ring: Web Apps in Idiomatic Clojure

Example: Stacktraces

(defn app [req]

("o noes"))

(use ’ring.adapter.jetty)

(use ’ring.middleware.stacktrace)

(run-jetty (wrap-stacktrace app) {:port 8080})

Page 26: Ring: Web Apps in Idiomatic Clojure

Example: Stacktraces

(defn app [req]

("o noes"))

(use ’ring.adapter.jetty)

(use ’ring.middleware.stacktrace)

(run-jetty (wrap-stacktrace app) {:port 8080})

Page 27: Ring: Web Apps in Idiomatic Clojure

Example: Stacktraces

Page 28: Ring: Web Apps in Idiomatic Clojure

Example: Static Files

(defn dynamic-app [req]

(...))

(use ’ring.middleware.file)

(use ’ring.middleware.file-info)

(def app

(-> dynamic-app

(wrap-file "public")

(wrap-file-info)))

Page 29: Ring: Web Apps in Idiomatic Clojure

Example: Static Files

(defn dynamic-app [req]

(...))

(use ’ring.middleware.file)

(use ’ring.middleware.file-info)

(def app

(-> dynamic-app

(wrap-file "public")

(wrap-file-info)))

Page 30: Ring: Web Apps in Idiomatic Clojure

Example: Testability

(defn my-app [req]

(...))

(deftest not-found-error

(let [req {:uri "/bogus/uri"}

resp (my-app req)]

(is (= 404 (:status resp)))))

Page 31: Ring: Web Apps in Idiomatic Clojure

Example: Testability

(defn my-app [req]

(...))

(deftest not-found-error

(let [req {:uri "/bogus/uri"}

resp (my-app req)]

(is (= 404 (:status resp)))))

Page 32: Ring: Web Apps in Idiomatic Clojure

Example: Testability

(defn my-app [req]

(...))

(deftest not-found-error

(let [req {:uri "/bogus/uri"}

resp (my-app req)]

(is (= 404 (:status resp)))))

Page 33: Ring: Web Apps in Idiomatic Clojure

Ring and Web Frameworks

I Foundation, not replacement

I Avoid duplication and share code

I Compojure, Conjure, Weld all targeting Ring

I Compojure case study

Page 34: Ring: Web Apps in Idiomatic Clojure

Ring and Web Frameworks

I Foundation, not replacement

I Avoid duplication and share code

I Compojure, Conjure, Weld all targeting Ring

I Compojure case study

Page 35: Ring: Web Apps in Idiomatic Clojure

Ring and Web Frameworks

I Foundation, not replacement

I Avoid duplication and share code

I Compojure, Conjure, Weld all targeting Ring

I Compojure case study

Page 36: Ring: Web Apps in Idiomatic Clojure

Ring and Web Frameworks

I Foundation, not replacement

I Avoid duplication and share code

I Compojure, Conjure, Weld all targeting Ring

I Compojure case study

Page 37: Ring: Web Apps in Idiomatic Clojure

Ring and Web Frameworks

I Foundation, not replacement

I Avoid duplication and share code

I Compojure, Conjure, Weld all targeting Ring

I Compojure case study

Page 38: Ring: Web Apps in Idiomatic Clojure

Ring Middleware

I ring.middleware.reload: Automaticallyreload namespaces

I ring.middleware.params: Get requestparams as a map

I ring.middleware.cookies: Cookie jarabstraction

Page 39: Ring: Web Apps in Idiomatic Clojure

Ring Middleware

I ring.middleware.reload: Automaticallyreload namespaces

I ring.middleware.params: Get requestparams as a map

I ring.middleware.cookies: Cookie jarabstraction

Page 40: Ring: Web Apps in Idiomatic Clojure

Ring Middleware

I ring.middleware.reload: Automaticallyreload namespaces

I ring.middleware.params: Get requestparams as a map

I ring.middleware.cookies: Cookie jarabstraction

Page 41: Ring: Web Apps in Idiomatic Clojure

Ring Middleware

I ring.middleware.reload: Automaticallyreload namespaces

I ring.middleware.params: Get requestparams as a map

I ring.middleware.cookies: Cookie jarabstraction

Page 42: Ring: Web Apps in Idiomatic Clojure

Other Components

I ring.util.servlet: If you really want aServlet

I ring.adapter.httpcore: Adapter for HTTPCore library

I ring.handler.dump: See the request map

I ring.examples.*: Runnable examples

Page 43: Ring: Web Apps in Idiomatic Clojure

Other Components

I ring.util.servlet: If you really want aServlet

I ring.adapter.httpcore: Adapter for HTTPCore library

I ring.handler.dump: See the request map

I ring.examples.*: Runnable examples

Page 44: Ring: Web Apps in Idiomatic Clojure

Other Components

I ring.util.servlet: If you really want aServlet

I ring.adapter.httpcore: Adapter for HTTPCore library

I ring.handler.dump: See the request map

I ring.examples.*: Runnable examples

Page 45: Ring: Web Apps in Idiomatic Clojure

Other Components

I ring.util.servlet: If you really want aServlet

I ring.adapter.httpcore: Adapter for HTTPCore library

I ring.handler.dump: See the request map

I ring.examples.*: Runnable examples

Page 46: Ring: Web Apps in Idiomatic Clojure

Other Components

I ring.util.servlet: If you really want aServlet

I ring.adapter.httpcore: Adapter for HTTPCore library

I ring.handler.dump: See the request map

I ring.examples.*: Runnable examples

Page 47: Ring: Web Apps in Idiomatic Clojure

Future Work

I Middleware and utility extraction fromframeworks

I Not a framework itself

I More use in production?

I User needs?

Page 48: Ring: Web Apps in Idiomatic Clojure

Future Work

I Middleware and utility extraction fromframeworks

I Not a framework itself

I More use in production?

I User needs?

Page 49: Ring: Web Apps in Idiomatic Clojure

Future Work

I Middleware and utility extraction fromframeworks

I Not a framework itself

I More use in production?

I User needs?

Page 50: Ring: Web Apps in Idiomatic Clojure

Future Work

I Middleware and utility extraction fromframeworks

I Not a framework itself

I More use in production?

I User needs?

Page 51: Ring: Web Apps in Idiomatic Clojure

Future Work

I Middleware and utility extraction fromframeworks

I Not a framework itself

I More use in production?

I User needs?

Page 52: Ring: Web Apps in Idiomatic Clojure

Learn More

I http://github.com/mmcgrana/ring

I ring/example/*.clj

I ring/SPEC