Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Web Architecture

Functional Web Architecture

Designing web applications with Ring and Compojure

weavejester

July 04, 2012
Tweet

Other Decks in Programming

Transcript

  1. Functional Web Architecture Designing web applications with Ring and Compojure

    twitter: @weavejester code: github.com/weavejester
  2. Example (use 'ring.util.response 'ring.adapter.jetty) (defn handler [request] (-> (response "Hello

    World") (content-type "text/plain"))) (run-jetty handler {:port 3000})
  3. Caching (defn wrap-cache [handler] (fn [request] (or (get-cache request) (let

    [response (handler request)] (put-cache! request response) response))))
  4. Content Types (defn wrap-content-type [handler] (fn [request] (let [ext (get-ext

    (:uri request)) type (ext-types ext)] (-> (handler request) (content-type type)))))
  5. Parameters (defn wrap-params [handler] (fn [request] (let [query (:query-string request)

    params (parse-params query)] (handler (assoc request :params params)))))
  6. Ring core middleware • Query and form parameters • Multipart

    file uploads • Cookies • Sessions • Content-types • Static resources
  7. Basic Routing (use 'ring.util.response) (defn handler [request] (case (:uri request)

    "/hello" (response "Hello") "/world" (response "World") :else (not-found "Page not found")))
  8. Compojure ; (GET "/hello" request "hello") (def hello-route (fn [request]

    (if (and (= (:request-method request) :get) (= (:uri request) "/hello")) "hello" nil))))
  9. Compojure ; (GET "/hello" request "hello") (def hello-route (fn [request]

    (if (and (= (:request-method request) :get) (= (:uri request) "/hello")) "hello" nil))))
  10. Compojure ; (GET "/hello" request "hello") (def hello-route (fn [request]

    (if (and (= (:request-method request) :get) (= (:uri request) "/hello")) "hello" nil))))
  11. Compojure ; (GET "/hello" request "hello") (def hello-route (fn [request]

    (if (and (= (:request-method request) :get) (= (:uri request) "/hello")) "hello" nil))))
  12. Compojure ; (GET "/hello" request "hello") (def hello-route (fn [request]

    (if (and (= (:request-method request) :get) (= (:uri request) "/hello")) "hello" nil))))
  13. Compojure (use 'compojure.core) (require '[compojure.route :as route]) (defroutes handler (GET

    "/hello" _ "hello") (GET "/world" _ "world") (route/not-found "Page Not Found"))
  14. Compojure (use 'compojure.core) (require '[compojure.route :as route]) (defroutes handler (GET

    "/hello" _ "hello") (GET "/world" _ "world") (route/not-found "Page Not Found"))
  15. Compojure (use 'compojure.core) (require '[compojure.route :as route]) (defroutes handler (GET

    "/:word" request (-> request :params :word)) (route/not-found "Page Not Found"))
  16. Compojure (use 'compojure.core) (require '[compojure.route :as route]) (defroutes handler (GET

    "/:word" {{word :word} :params} word) (route/not-found "Page Not Found"))
  17. Compojure (use 'compojure.core) (require '[compojure.route :as route] '[clojure.string :as str])

    (defroutes handler (GET "/:word" [word] word) (GET "/:word/shout" [word] (str/upper-case word)) (route/not-found "Page Not Found"))
  18. Compojure (use 'compojure.core) (require '[compojure.route :as route] '[clojure.string :as str])

    (defroutes handler (context "/:word" [word] (GET "/" word) (GET "/shout" (str/upper-case word))) (route/not-found "Page Not Found"))
  19. Compojure (wrap-authorize (context "/user/:uid" [uid] (let [user (get-user uid)] (routes

    (GET "/" [] @user) (PUT "/" [& data] (put! user data)) (DELETE "/" [] (delete! user))))))
  20. Compojure (defn rest-routes [model] (routes (GET "/" [] @model) (PUT

    "/" [& data] (put! model data)) (DELETE "/" [] (delete! user))) (wrap-authorize (context "/user/:uid" [uid] (rest-routes (get-user uid))