Skip to content

Commit c90f76a

Browse files
sazzerpivovarit
authored andcommitted
Examples for Clojure Ring (eugenp#6881)
1 parent 4864f0b commit c90f76a

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

clojure/ring/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/target
2+
/classes
3+
/checkouts
4+
profiles.clj
5+
pom.xml
6+
pom.xml.asc
7+
*.jar
8+
*.class
9+
/.lein-*
10+
/.nrepl-port
11+
.hgignore
12+
.hg/

clojure/ring/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Clojure Ring Examples
2+
3+
This project acts as a set of examples for the Clojure Ring library.
4+
5+
## Runing the examples
6+
7+
The examples can all be run from the Leiningen REPL.
8+
9+
Firstly, start the REPL with `lein repl`. Then the examples can be executed with:
10+
11+
* `(run simple-handler)` - A simple handler that just echos a constant string to the client
12+
* `(run check-ip-handler)` - A handler that echos the clients IP Address back to them
13+
* `(run echo-handler)` - A handler that echos the value of the "input" parameter back
14+
* `(run request-count-handler)` - A handler with a session that tracks how many times this session has requested this handler
15+
16+
In all cases, the handlers can be accessed on http://localhost:3000.

clojure/ring/project.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(defproject baeldung-ring "0.1.0-SNAPSHOT"
2+
:dependencies [[org.clojure/clojure "1.10.0"]
3+
[ring/ring-core "1.7.1"]
4+
[ring/ring-jetty-adapter "1.7.1"]
5+
[ring/ring-devel "1.7.1"]]
6+
:plugins [[lein-ring "0.12.5"]]
7+
:ring {:handler ring.core/simple-handler}
8+
:repl-options {:init-ns ring.core})

clojure/ring/src/ring/core.clj

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)