Skip to content

Commit 25a0015

Browse files
committed
section 8 video 1
1 parent 9e4ca98 commit 25a0015

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

project.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
[korma "0.3.0-RC6"]
1616
[com.novemberain/monger "1.7.0"]
1717
[cheshire "5.1.1"]
18-
[ring "1.2.0"]]
18+
[ring "1.2.0"]
19+
[compojure "1.1.6"]]
1920
:cljsbuild {:builds [{:source-paths ["cljs"]
2021
:compiler {:output-to "resources/public/app.js"}}]})

src/project1/blog.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@
7676
(def blog-handler
7777
(->
7878
(route/routing
79-
(route/with-route-matches :get "/entries" get-handler)
80-
(route/with-route-matches :post "/entries" post-handler)
81-
(route/with-route-matches :get "/entries/:id" get-entry-handler)
82-
(route/with-route-matches :put "/entries/:id" put-handler)
83-
(route/with-route-matches :delete "/entries/:id" delete-handler))
79+
(route/with-route-matches :get "/" get-handler)
80+
(route/with-route-matches :post "/" post-handler)
81+
(route/with-route-matches :get "/:id" get-entry-handler)
82+
(route/with-route-matches :put "/:id" put-handler)
83+
(route/with-route-matches :delete "/:id" delete-handler))
8484
json-error-handler
8585
))
8686

src/project1/core.clj

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[ring.middleware.cookies]
99
[ring.middleware.session]
1010
[ring.middleware.session.memory]
11+
[compojure.core :as compojure]
1112
[project1.html :as html]
1213
[project1.route :as route]
1314
[project1.blog :as blog]
@@ -84,11 +85,11 @@
8485
{:body "Logged out."
8586
:session nil})
8687

87-
(defn form-handler [request]
88+
(defn form-handler [login request]
8889
{:status 200
8990
:headers {"Content-type" "text/html"}
90-
:cookies {:username (:login (:params request))}
91-
:session {:username (:login (:params request))
91+
:cookies {:username login}
92+
:session {:username login
9293
:cnt (inc (or (:cnt (:session request)) 0))}
9394
:body (layout
9495
[:div
@@ -104,18 +105,18 @@
104105
[:b (when-let [f (get-in request [:params :file :tempfile])]
105106
(.getAbsolutePath f))]])})
106107

107-
(def route-handler
108-
(route/routing
109-
blog/blog-handler
110-
(route/with-route-matches :get "/test1" test1-handler)
111-
(route/with-route-matches :get "/test1/:id" test1-handler)
112-
(route/with-route-matches :get "/test2" test2-handler)
113-
(route/with-route-matches :get "/test3" handlers/handler3)
114-
(route/with-route-matches :get "/form" form-handler)
115-
(route/with-route-matches :post "/form" form-handler)
116-
(route/with-route-matches :get "/cookies" cookie-handler)
117-
(route/with-route-matches :get "/session" session-handler)
118-
(route/with-route-matches :get "/logout" logout-handler)))
108+
(compojure/defroutes route-handler
109+
(compojure/context "/entries" []
110+
blog/blog-handler)
111+
(compojure/GET "/test1" [:as request] (test1-handler request))
112+
(compojure/GET "/test1/:id" [id :as request] (test1-handler request))
113+
(compojure/GET "/test2" [id :as request] (test2-handler request))
114+
(compojure/GET "/test3" [id :as request] (handlers/handler3 request))
115+
(compojure/ANY "/form" [login :as request] (form-handler login request))
116+
(compojure/GET "/cookies" [:as request] (cookie-handler request))
117+
(compojure/GET "/session" [:as request] (session-handler request))
118+
(compojure/GET "/logout" [:as request] (logout-handler request))
119+
)
119120

120121
(defn wrapping-handler [request]
121122
(try

src/project1/route.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(defn route-matches [verb path-spec request]
44
(when (= (:request-method request) verb)
55
(let [path-regex (.replaceAll path-spec "/:([^/]+)" "/([^/]+)")
6-
path-matches (re-matches (re-pattern path-regex) (:uri request))]
6+
path-matches (re-matches (re-pattern path-regex) (:path-info request))]
77
(println "Path regex for" path-spec "is" path-regex
88
"match result:" path-matches)
99
(when path-matches

0 commit comments

Comments
 (0)