|
| 1 | +(ns ring.core |
| 2 | + (:use ring.adapter.jetty |
| 3 | + [ring.middleware.content-type] |
| 4 | + [ring.middleware.cookies] |
| 5 | + [ring.middleware.params] |
| 6 | + [ring.middleware.session] |
| 7 | + [ring.middleware.session.cookie] |
| 8 | + [ring.util.response])) |
| 9 | + |
| 10 | +;; Handler that just echos back the string "Hello World" |
| 11 | +(defn simple-handler [request] |
| 12 | + {:status 200 |
| 13 | + :headers {"Content-Type" "text/plain"} |
| 14 | + :body "Hello World"}) |
| 15 | + |
| 16 | +;; Handler that echos back the clients IP Address |
| 17 | +;; This demonstrates building responses properly, and extracting values from the request |
| 18 | +(defn check-ip-handler [request] |
| 19 | + (content-type |
| 20 | + (response (:remote-addr request)) |
| 21 | + "text/plain")) |
| 22 | + |
| 23 | +;; Handler that echos back the incoming parameter "input" |
| 24 | +;; This demonstrates middleware chaining and accessing parameters |
| 25 | +(def echo-handler |
| 26 | + (-> (fn [{params :params}] |
| 27 | + (content-type |
| 28 | + (response (get params "input")) |
| 29 | + "text/plain")) |
| 30 | + (wrap-params {:encoding "UTF-8"}) |
| 31 | + )) |
| 32 | + |
| 33 | +;; Handler that keeps track of how many times each session has accessed the service |
| 34 | +;; This demonstrates cookies and sessions |
| 35 | +(def request-count-handler |
| 36 | + (-> (fn [{session :session}] |
| 37 | + (let [count (:count session 0) |
| 38 | + session (assoc session :count (inc count))] |
| 39 | + (-> (response (str "You accessed this page " count " times.")) |
| 40 | + (assoc :session session)))) |
| 41 | + wrap-cookies |
| 42 | + (wrap-session {:cookie-attrs {:max-age 3600}}) |
| 43 | + )) |
| 44 | + |
| 45 | +;; Run the provided handler on port 3000 |
| 46 | +(defn run |
| 47 | + [h] |
| 48 | + (run-jetty h {:port 3000})) |
0 commit comments