Skip to content

Commit

Permalink
Prague Clojure demo import
Browse files Browse the repository at this point in the history
  • Loading branch information
danskarda committed Sep 26, 2012
0 parents commit e649977
Show file tree
Hide file tree
Showing 29 changed files with 620 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/target
/lib
/classes
/checkouts
pom.xml
*.jar
*.class
.lein-deps-sum
.lein-failures
.lein-plugins
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# demo

project.clj
-----------

For the second demo we included two libraries: hiccup and
compojure.

https://github.com/weavejester/hiccup
https://github.com/weavejester/compojure

For easier request handling we also added ring-core and
ring-devel. Both include definition of ring "middleware".

Hiccup
------

Hiccup is a library for HTML rendering from Clojure data
structures

Compojure
---------

Is a DSL (Domain Specific Language) for writing routing
information for Ring.
10 changes: 10 additions & 0 deletions demo/demo-1-jetty/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/target
/lib
/classes
/checkouts
pom.xml
*.jar
*.class
.lein-deps-sum
.lein-failures
.lein-plugins
35 changes: 35 additions & 0 deletions demo/demo-1-jetty/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# demo

This is first example of

1. leiningen2
-------------

We executed

`lein new demo`

Command created project.clj and standard file structure:

- project.clj
- .gitignore
- doc/
- src/demo1/core.clj
- test

project.clj
-----------

File project.clj describes the project. We added dependencies on ring (and jetty) here.

src/demo/core.clj
------------------

First humble example of ring application. Execution

`lein run`

or

`lein repl

3 changes: 3 additions & 0 deletions demo/demo-1-jetty/doc/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction to demo

TODO: write [great documentation](http://jacobian.org/writing/great-documentation/what-to-write/)
10 changes: 10 additions & 0 deletions demo/demo-1-jetty/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(defproject demo1 "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}

:dependencies [[org.clojure/clojure "1.4.0"]
[ring/ring-jetty-adapter "1.1.5"]]

:main "demo.core/-main")
17 changes: 17 additions & 0 deletions demo/demo-1-jetty/src/demo/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns demo.core
(:require [ring.adapter.jetty :as jetty]))

(defn hello-world
"Return hello world page"
[REQ]
{:status 200,
:body (str "Hello, world!\n"
"It is " (java.util.Date.))})

(defn -main [& [PORT]]
(jetty/run-jetty #'hello-world
;; join? false will return to command line
{:join? false
:port (if PORT
(Integer/parseInt PORT)
8080)}))
7 changes: 7 additions & 0 deletions demo/demo-1-jetty/test/demo/core_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns demo.core-test
(:use clojure.test
demo.core))

(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))
10 changes: 10 additions & 0 deletions demo/demo-2-compojure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/target
/lib
/classes
/checkouts
pom.xml
*.jar
*.class
.lein-deps-sum
.lein-failures
.lein-plugins
25 changes: 25 additions & 0 deletions demo/demo-2-compojure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# demo

project.clj
-----------

For the second demo we included two libraries: hiccup and
compojure.

https://github.com/weavejester/hiccup
https://github.com/weavejester/compojure

For easier request handling we also added ring-core and
ring-devel. Both include definition of ring "middleware".

Hiccup
------

Hiccup is a library for HTML rendering from Clojure data
structures

Compojure
---------

Is a DSL (Domain Specific Language) for writing routing
information for Ring.
3 changes: 3 additions & 0 deletions demo/demo-2-compojure/doc/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction to demo

TODO: write [great documentation](http://jacobian.org/writing/great-documentation/what-to-write/)
14 changes: 14 additions & 0 deletions demo/demo-2-compojure/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(defproject demo "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}

:dependencies [[org.clojure/clojure "1.4.0"]
[ring/ring-jetty-adapter "1.1.5"]
[ring/ring-core "1.1.5"] ;; parsing parameters etc
[ring/ring-devel "1.1.5"] ;; dev middleware like stactrace
[hiccup "1.0.1"]
[compojure "1.1.3"]]

:main "demo.core/-main")
27 changes: 27 additions & 0 deletions demo/demo-2-compojure/src/demo/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns demo.core
(:require [ring.adapter.jetty :as jetty]
[demo.page :as page]
[ring.middleware stacktrace params keyword-params])
(:use compojure.core))


(defroutes main-route
(GET "/" [:as req]
(page/hello-world (-> req :params :who))))

(def jetty-handler
(-> #'main-route
;; transate :params {"name" val} into {:name val}
ring.middleware.keyword-params/wrap-keyword-params
;; parse request to :query-params, :form-params and :params
ring.middleware.params/wrap-params
;; handle exceptions and print stack traces
ring.middleware.stacktrace/wrap-stacktrace))

(defn -main [& [PORT]]
(jetty/run-jetty #'jetty-handler
;; join? false will return to command line
{:join? false
:port (if PORT
(Integer/parseInt PORT)
8080)}))
9 changes: 9 additions & 0 deletions demo/demo-2-compojure/src/demo/page.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns demo.page
(:use [hiccup.core]))

(defn hello-world [& [WHO]]
(html [:html
[:head
[:title "Hello World"]]
[:body
[:h1 "Hello, " (or WHO "Prague Clojurians")]]]))
7 changes: 7 additions & 0 deletions demo/demo-2-compojure/test/demo/core_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns demo.core-test
(:use clojure.test
demo.core))

(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))
10 changes: 10 additions & 0 deletions demo/demo-3-chat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/target
/lib
/classes
/checkouts
pom.xml
*.jar
*.class
.lein-deps-sum
.lein-failures
.lein-plugins
25 changes: 25 additions & 0 deletions demo/demo-3-chat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# demo

project.clj
-----------

For the second demo we included two libraries: hiccup and
compojure.

https://github.com/weavejester/hiccup
https://github.com/weavejester/compojure

For easier request handling we also added ring-core and
ring-devel. Both include definition of ring "middleware".

Hiccup
------

Hiccup is a library for HTML rendering from Clojure data
structures

Compojure
---------

Is a DSL (Domain Specific Language) for writing routing
information for Ring.
3 changes: 3 additions & 0 deletions demo/demo-3-chat/doc/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction to demo

TODO: write [great documentation](http://jacobian.org/writing/great-documentation/what-to-write/)
14 changes: 14 additions & 0 deletions demo/demo-3-chat/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(defproject demo "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}

:dependencies [[org.clojure/clojure "1.4.0"]
[ring/ring-jetty-adapter "1.1.5"]
[ring/ring-core "1.1.5"] ;; parsing parameters etc
[ring/ring-devel "1.1.5"] ;; dev middleware like stactrace
[hiccup "1.0.1"]
[compojure "1.1.3"]]

:main "demo.core/-main")
64 changes: 64 additions & 0 deletions demo/demo-3-chat/src/demo/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
(ns demo.core
(:require [clojure.string :as str]
[ring.adapter.jetty :as jetty]
[demo.page :as page]
[demo.message :as msg]
[ring.util.response :as resp]
[ring.middleware session stacktrace params keyword-params])
(:use compojure.core))

(defonce rooms (atom {}))

(defroutes room-route
(GET "/" [room :as req]
(let [logs (@rooms room)]
(page/render-room room logs (-> req :session :author))))
(POST "/" [room author body :as req]
(let [logs (@rooms room)
written-by (or (when-not (empty? author) author)
(-> req :session :author)
"Anonymous")]
(when-not (empty? body)
(swap! rooms update-in [room]
msg/log-add-message author body))
;; redirect back to room
(-> (resp/redirect-after-post (str "/room/" room))
;; and add a author into session as a cookie
(assoc-in [:session :author] author)))))

(defn room-validate
"Validates room parameter - if room already exits"
[{{room :room} :params}]
(when-not (@rooms room)
(resp/not-found (str "No such room: " room))))

(defroutes main-route
(GET "/" []
(page/list-chatrooms @rooms))
(context "/room/:room" []
room-validate
room-route)
(POST "/new-room" [:as r]
(let [new-name (-> r :params :room-id
(str/replace #"[/?%<>&]" "_"))]
(swap! rooms update-in [new-name] #(or % (sorted-map)))
(resp/redirect (str "room/" new-name)))))

(def jetty-handler
(-> #'main-route
;; sessions & cookies
ring.middleware.session/wrap-session
;; transate :params {"name" val} into {:name val}
ring.middleware.keyword-params/wrap-keyword-params
;; parse request to :query-params, :form-params and :params
ring.middleware.params/wrap-params
;; handle exceptions and print stack traces
ring.middleware.stacktrace/wrap-stacktrace))

(defn -main [& [PORT]]
(jetty/run-jetty #'jetty-handler
;; join? false will return to command line
{:join? false
:port (if PORT
(Integer/parseInt PORT)
8080)}))
14 changes: 14 additions & 0 deletions demo/demo-3-chat/src/demo/message.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns demo.message
"A library for dummy storing of messages")

(defn log-add
[LOG MESSAGE]
(if (empty? LOG)
(sorted-map 1 MESSAGE)
(assoc LOG (-> LOG rseq first first inc) MESSAGE)))

(defn log-add-message
[LOG AUTHOR BODY]
(log-add LOG {:body BODY,
:author AUTHOR,
:date (java.util.Date.)}))
Loading

0 comments on commit e649977

Please sign in to comment.