Permalink
Browse files

some housekeeping, split server into server.handler, add in a gae util.

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
1 parent 2926d11 commit fe065938142a55cc9dc3d02b10b859ec2cb1bb27 @ibdknox ibdknox committed Aug 11, 2011
Showing with 95 additions and 61 deletions.
  1. +1 −8 src/noir/core.clj
  2. +10 −53 src/noir/server.clj
  3. +58 −0 src/noir/server/handler.clj
  4. +26 −0 src/noir/util/gae.clj
View
@@ -22,17 +22,10 @@
(string/replace #":" ">")
(string/replace #"\*" "<")))))
-;; (defpage "/foo/:id" {})
-;; (defpage [:get "/foo/:id"] {})
-;; (defpage foo "/foo/:id" {})
-;; (defpage foo [:post "/foo/:id"])
-
-;; this would be a good candidate for match, once it's ready (https://github.com/swannodette/match)
-
(defn- throwf [msg & args]
(throw (Exception. (apply format msg args))))
-(defn parse-args
+(defn- parse-args
"parses the arguments to defpage. Returns a map containing the keys :name :action :url :destruct :body"
[args]
(let [m (if (symbol? (first args))
View
@@ -1,63 +1,20 @@
(ns noir.server
"A collection of functions to handle Noir's server and add middleware to the stack."
(:use compojure.core
- clojure.java.io
- clojure.tools.namespace
- ring.adapter.jetty
- ring.middleware.file-info
- ring.middleware.reload-modified
- ring.middleware.file)
- (:require [compojure.handler :as handler]
- [compojure.route :as c-route]
- [noir.core :as noir]
- [noir.content.defaults :as defaults]
- [noir.cookies :as cookie]
- [noir.exception :as exception]
- [noir.statuses :as statuses]
- [noir.options :as options]
- [noir.session :as session]
- [noir.validation :as validation]))
-
-(defonce middleware (atom #{}))
-
-(defn- wrap-route-updating [handler]
- (if (options/dev-mode?)
- (wrap-reload-modified handler ["src"])
- handler))
-
-(defn- wrap-custom-middleware [handler]
- (reduce (fn [cur [func args]] (apply func cur args))
- handler
- (seq @middleware)))
-
-(defn- pack-routes []
- (apply routes (concat (vals @noir/pre-routes) (vals @noir/noir-routes) noir/spec-routes)))
-
-(defn- init-routes [opts]
- (binding [options/*options* (options/compile-options opts)]
- (->
- (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)
- (handler/site)
- (session/wrap-noir-session)
- (cookie/wrap-noir-cookies)
- (validation/wrap-noir-validation)
- (statuses/wrap-status-pages)
- (wrap-route-updating)
- (exception/wrap-exceptions)
- (options/wrap-options opts))))
+ [clojure.java.io :only [file]]
+ [clojure.tools.namespace :only [find-namespaces-in-dir find-namespaces-on-classpath]])
+ (:require [compojure.handler :as compojure]
+ [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.
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]]
- (init-routes opts))
+ (-> (handler/base-handler opts)
+ (compojure/site)
+ (handler/wrap-noir-middleware opts)))
(defn load-views
"Require all the namespaces in the given dir so that the pages are loaded
@@ -81,7 +38,7 @@
function, which will be passed the handler. Any extra args to be applied should be
supplied along with the function."
[func & args]
- (swap! middleware conj [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.
@@ -92,7 +49,7 @@
:session-store - an alternate store for session handling"
[port & [opts]]
(println "Starting server...")
- (let [server (run-jetty (init-routes opts) {:port port :join? false})]
+ (let [server (jetty/run-jetty (gen-handler opts) {:port port :join? false})]
(println (str "Server started on port [" port "]."))
(println (str "You can view the site at http://localhost:" port))
server))
@@ -0,0 +1,58 @@
+(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)
+ (:require [compojure.handler :as handler]
+ [noir.core :as noir]
+ [noir.content.defaults :as defaults]
+ [noir.cookies :as cookie]
+ [noir.exception :as exception]
+ [noir.statuses :as statuses]
+ [noir.options :as options]
+ [noir.session :as session]
+ [noir.validation :as validation]))
+
+(defonce middleware (atom #{}))
+
+(defn- wrap-route-updating [handler]
+ (if (options/dev-mode?)
+ (wrap-reload-modified handler ["src"])
+ handler))
+
+(defn- wrap-custom-middleware [handler]
+ (reduce (fn [cur [func args]] (apply func cur args))
+ handler
+ (seq @middleware)))
+
+(defn- pack-routes []
+ (apply routes (concat (vals @noir/pre-routes) (vals @noir/noir-routes) noir/spec-routes)))
+
+(defn- init-routes [opts]
+ (binding [options/*options* (options/compile-options opts)]
+ (->
+ (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
+ "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)
+ (validation/wrap-noir-validation)
+ (statuses/wrap-status-pages)
+ (wrap-route-updating)
+ (exception/wrap-exceptions)
+ (options/wrap-options opts))))
+
+(defn base-handler
+ "Get the most basic Noir request handler, that only adds wrap-custom-middleware."
+ [& [opts]]
+ (init-routes opts))
View
@@ -0,0 +1,26 @@
+(ns noir.util.gae
+ "Functions to help run noir on Google App Engine."
+ (:use compojure.core
+ [ring.middleware params
+ keyword-params
+ nested-params
+ multipart-params
+ cookies
+ session])
+ (:require [noir.server.handler :as handler]
+ [noir.core :as noir]
+ [noir.content.defaults :as defaults]
+ [noir.cookies :as cookie]
+ [noir.exception :as exception]
+ [noir.statuses :as statuses]
+ [noir.options :as options]
+ [noir.session :as session]
+ [noir.validation :as validation]))
+
+
+(defn gae-handler [opts]
+ (-> (handler/base-handler opts)
+ (wrap-keyword-params)
+ (wrap-nested-params)
+ (wrap-params)
+ (handler/wrap-noir-middleware opts)))

0 comments on commit fe06593

Please sign in to comment.