Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add in post routes, compojure routes, :resource-root option and tests

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
commit 872dabacd8cb0545f91379f6e3225de25ce2021f 1 parent 673d8f4
@ibdknox ibdknox authored
View
23 src/noir/core.clj
@@ -2,14 +2,12 @@
"Functions to work with partials and pages."
(:use hiccup.core
compojure.core)
- (:require [clojure.string :as string]
- [compojure.route :as c-route]))
+ (:require [clojure.string :as string]))
(defonce noir-routes (atom {}))
(defonce route-funcs (atom {}))
(defonce pre-routes (atom (sorted-map)))
-(defonce spec-routes [(c-route/resources "/")
- (ANY "*" [] {:status 404 :body nil})])
+(defonce post-routes (atom (list)))
(defn- keyword->symbol [namesp kw]
(symbol namesp (string/upper-case (name kw))))
@@ -17,7 +15,7 @@
(defn- route->key [action rte]
(let [action (string/replace (str action) #".*/" "")]
(str action (-> rte
- (string/replace #"\." "!dot!")
+ (string/replace #"\." "!dot!")
(string/replace #"/" "--")
(string/replace #":" ">")
(string/replace #"\*" "<")))))
@@ -147,3 +145,18 @@
[& args]
(let [{:keys [action destruct url body]} (parse-args args)]
`(swap! pre-routes assoc ~url (~action ~url {:as request#} ((fn [~destruct] ~@body) request#)))))
+
+(defmacro post-route
+ "Adds a route to the end of the route table and passes the entire request to
+ be desctructured and used in the body. These routes are guaranteed to be
+ evaluated after those created by defpage and before the generic catch-all and
+ resources routes."
+ [& args]
+ (let [{:keys [action destruct url body]} (parse-args args)]
+ `(swap! post-routes conj (~action ~url {:as request#} ((fn [~destruct] ~@body) request#)))))
+
+(defn compojure-route
+ "Adds a compojure route fn to the end of the route table. These routes are queried after
+ those created by defpage and before the generic catch-all and resources routes."
+ [compojure-func]
+ (swap! post-routes conj compojure-func))
View
21 src/noir/server.clj
@@ -7,8 +7,8 @@
[ring.adapter.jetty :as jetty]
[noir.server.handler :as handler]))
-(defn gen-handler
- "Get a full Noir request handler for use with plugins like lein-ring or lein-beanstalk.
+(defn gen-handler
+ "Get a full Noir request handler for use with plugins like lein-ring or lein-beanstalk.
If used in a definition, this must come after views have been loaded to ensure that the
routes have already been added to the route table."
[& [opts]]
@@ -16,7 +16,7 @@
(compojure/site)
(handler/wrap-noir-middleware opts)))
-(defn load-views
+(defn load-views
"Require all the namespaces in the given dir so that the pages are loaded
by the server."
[& dirs]
@@ -25,7 +25,7 @@
(require n)))
(defn load-views-ns
- "Require all the namespaces prefixed by the namespace symbold given so that the pages
+ "Require all the namespaces prefixed by the namespace symbold given so that the pages
are loaded by the server."
[& ns-syms]
(doseq [ns-sym ns-syms
@@ -33,24 +33,25 @@
(find-namespaces-on-classpath))]
(require n)))
-(defn add-middleware
+(defn add-middleware
"Add a middleware function to the noir server. Func is a standard ring middleware
function, which will be passed the handler. Any extra args to be applied should be
supplied along with the function."
[func & args]
(swap! handler/middleware conj [func args]))
-(defn start
- "Create a noir server bound to the specified port with a map of options and return it.
+(defn start
+ "Create a noir server bound to the specified port with a map of options and return it.
The available options are:
-
+
:mode - either :dev or :prod
:ns - the root namepace of your project
+ :resource-root - an alternative name for the public folder
:session-store - an alternate store for session handling"
[port & [opts]]
(println "Starting server...")
(let [server (jetty/run-jetty (gen-handler opts) {:port port :join? false})]
- (println (str "Server started on port [" port "]."))
+ (println (str "Server started on port [" port "]."))
(println (str "You can view the site at http://localhost:" port))
server))
@@ -64,5 +65,3 @@
[server]
(stop server)
(.start server))
-
-
View
23 src/noir/server/handler.clj
@@ -1,8 +1,9 @@
(ns noir.server.handler
"Handler generation functions used by noir.server and other ring handler libraries."
- (:use [compojure.core :only [routes]]
- ring.middleware.reload-modified)
+ (:use [compojure.core :only [routes ANY]]
+ ring.middleware.reload-modified)
(:require [compojure.handler :as handler]
+ [compojure.route :as c-route]
[noir.core :as noir]
[noir.content.defaults :as defaults]
[noir.cookies :as cookie]
@@ -14,6 +15,10 @@
(defonce middleware (atom #{}))
+(defn- spec-routes []
+ [(c-route/resources "/" {:root (options/get :resource-root "public")})
+ (ANY "*" [] {:status 404 :body nil})])
+
(defn- wrap-route-updating [handler]
(if (options/dev-mode?)
(wrap-reload-modified handler ["src"])
@@ -25,24 +30,24 @@
(seq @middleware)))
(defn- pack-routes []
- (apply routes (concat (vals @noir/pre-routes) (vals @noir/noir-routes) noir/spec-routes)))
+ (apply routes (concat (vals @noir/pre-routes) (vals @noir/noir-routes) @noir/post-routes (spec-routes))))
(defn- init-routes [opts]
(binding [options/*options* (options/compile-options opts)]
- (->
- (if (options/dev-mode?)
- (fn [request]
+ (->
+ (if (options/dev-mode?)
+ (fn [request]
;; by doing this as a function we can ensure that any routes added as the
;; result of a modification are evaluated on the first reload.
((pack-routes) request))
(pack-routes))
(wrap-custom-middleware))))
-(defn wrap-noir-middleware
+(defn wrap-noir-middleware
"Wrap a base handler in all of noir's middleware"
[handler opts]
(binding [options/*options* (options/compile-options opts)]
- (->
+ (->
handler
(session/wrap-noir-session)
(cookie/wrap-noir-cookies)
@@ -52,7 +57,7 @@
(exception/wrap-exceptions)
(options/wrap-options opts))))
-(defn base-handler
+(defn base-handler
"Get the most basic Noir request handler, that only adds wrap-custom-middleware."
[& [opts]]
(init-routes opts))
View
36 test/noir/test/core.clj
@@ -1,5 +1,6 @@
(ns noir.test.core
(:use [noir.core]
+ [compojure.core]
[noir.util.test])
(:use [clojure.test])
(:require [noir.util.crypt :as crypt]
@@ -13,7 +14,7 @@
(deftest hashing
(let [pass (crypt/encrypt "password")]
- (is (crypt/compare "password" pass))))
+ (is (crypt/compare "password" pass))))
(deftest session-get-default
(with-noir
@@ -60,7 +61,6 @@
(defpage "/test" {:keys [nme]}
(str "Hello " nme))
-
(deftest route-test
(-> (send-request "/test" {"nme" "chris"})
(has-status 200)
@@ -76,31 +76,31 @@
(has-body "{\"json\":\"text\"}")))
(deftest parsing-defpage
- (is (= (parse-args '[foo "/" [] "hey"])
+ (is (= (parse-args '[foo "/" [] "hey"])
{:fn-name 'foo
:url "/"
:action 'compojure.core/GET
:destruct []
:body '("hey")}))
- (is (= (parse-args '["/" [] "hey"])
+ (is (= (parse-args '["/" [] "hey"])
{:fn-name 'GET--
:url "/"
:action 'compojure.core/GET
:destruct []
:body '("hey")}))
- (is (= (parse-args '[foo [:post "/"] [] "hey"])
+ (is (= (parse-args '[foo [:post "/"] [] "hey"])
{:fn-name 'foo
:url "/"
:action 'compojure.core/POST
:destruct []
:body '("hey")}))
- (is (= (parse-args '[[:post "/"] [] "hey" "blah"])
+ (is (= (parse-args '[[:post "/"] [] "hey" "blah"])
{:fn-name 'POST--
:url "/"
:action 'compojure.core/POST
:destruct []
:body '("hey" "blah")}))
- (is (= (parse-args '["/test" {} "hey"])
+ (is (= (parse-args '["/test" {} "hey"])
{:fn-name 'GET--test
:url "/test"
:action 'compojure.core/GET
@@ -122,14 +122,35 @@
(resp/status 403
"not allowed"))
+(post-route "/post-route" []
+ (resp/status 403 "not allowed"))
+
+(defpage "/not-post-route" [] "success")
+(post-route "/not-post-route" [] "fail")
+
(defpage "/pre" []
"you should never see this")
+(compojure-route (ANY "/compojure" [] "compojure-route"))
+
(deftest pre-route-test
(-> (send-request "/pre")
(has-status 403)
(has-body "not allowed")))
+(deftest compojure-route-test
+ (-> (send-request "/compojure")
+ (has-status 200)
+ (has-body "compojure-route")))
+
+(deftest post-route-test
+ (-> (send-request "/post-route")
+ (has-status 403)
+ (has-body "not allowed"))
+ (-> (send-request "/not-post-route")
+ (has-status 200)
+ (has-body "success")))
+
(deftest named-route-test
(-> (send-request "/foo")
(has-status 200)
@@ -197,4 +218,3 @@
"test"
"test.@domain.com"
"test@com"))
-
Please sign in to comment.
Something went wrong with that request. Please try again.