forked from prague-lambda/clojure-web-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e649977
Showing
29 changed files
with
620 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")]]])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.)})) |
Oops, something went wrong.