From cfee97fd951ba05fbf9f1699dc4073b2ba35e1bc Mon Sep 17 00:00:00 2001 From: gered Date: Fri, 28 Mar 2014 21:56:40 -0400 Subject: [PATCH] routes are now automatically discovered and added to the ring handler --- src/blarg/handler.clj | 27 ++++++++++++--------------- src/blarg/routes/auth.clj | 3 ++- src/blarg/routes/files.clj | 3 ++- src/blarg/routes/home.clj | 3 ++- src/blarg/routes/posts.clj | 5 +++-- src/blarg/routes/rss.clj | 5 +++-- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/blarg/handler.clj b/src/blarg/handler.clj index 7017fa8..a848899 100644 --- a/src/blarg/handler.clj +++ b/src/blarg/handler.clj @@ -1,23 +1,18 @@ (ns blarg.handler - (:use blarg.routes.home - blarg.routes.posts - blarg.routes.auth - blarg.routes.files - blarg.routes.rss - blarg.routes.accessrules - blarg.config - ring.middleware.head - compojure.core) (:require [noir.util.middleware :refer [app-handler]] [noir.response :as resp] + [compojure.core :refer [defroutes]] [compojure.route :as route] [compojure.response :refer [render]] [taoensso.timbre :as timbre] [taoensso.timbre.appenders.rotor :as rotor] [clj-jtwig.core :as jtwig] [clj-jtwig.web.middleware :refer [wrap-servlet-context-path]] + [blarg.config :refer [config-val]] [blarg.views.layout :as layout] - [blarg.models.db :as db])) + [blarg.models.db :as db] + [blarg.routes.accessrules :refer [auth-required]] + [blarg.route-utils :refer [find-routes]])) (defroutes app-routes (route/resources "/") @@ -60,8 +55,10 @@ :params {:errorInfo e} :status 500))))) -(def app (app-handler - [auth-routes home-routes posts-routes files-routes rss-routes app-routes] - :middleware [wrap-exceptions wrap-servlet-context-path] - :access-rules [{:redirect "/unauthorized" :rule auth-required}] - :formats [:json-kw :edn])) +(defonce routes (find-routes "blarg.routes." app-routes)) + +(defonce app (app-handler + routes + :middleware [wrap-exceptions wrap-servlet-context-path] + :access-rules [{:redirect "/unauthorized" :rule auth-required}] + :formats [:json-kw :edn])) diff --git a/src/blarg/routes/auth.clj b/src/blarg/routes/auth.clj index 3408399..9a1a6c0 100644 --- a/src/blarg/routes/auth.clj +++ b/src/blarg/routes/auth.clj @@ -4,6 +4,7 @@ [noir.util.route]) (:require [blarg.views.layout :as layout] [blarg.models.users :as users] + [blarg.route-utils :refer [register-routes]] [ring.util.response :refer [status]] [ring.middleware.head :refer [wrap-head]] [noir.session :as session] @@ -37,7 +38,7 @@ (defn handle-unauthorized [] (layout/render "unauthorized.html" :status 401)) -(defroutes auth-routes +(register-routes auth-routes (GET "/unauthorized" [] (handle-unauthorized)) (GET "/login" [] (login-page)) (POST "/login" [id pass] (handle-login id pass)) diff --git a/src/blarg/routes/files.clj b/src/blarg/routes/files.clj index 67c8046..4aebf87 100644 --- a/src/blarg/routes/files.clj +++ b/src/blarg/routes/files.clj @@ -6,6 +6,7 @@ (:require [blarg.views.layout :as layout] [blarg.models.files :as files] [blarg.routes.auth :as auth] + [blarg.route-utils :refer [register-routes]] [noir.response :as resp] [noir.session :as session])) @@ -69,7 +70,7 @@ (resp/content-type (:content_type file) (:data file)) (resp/redirect "/notfound"))) -(defroutes files-routes +(register-routes files-routes (GET "/listfiles" [] (restricted (list-files "/"))) (GET "/listfiles/*" [*] (restricted (list-files *))) (POST "/uploadfile" [path file returnpath] (restricted (handle-new-file (ensure-prefix-suffix path "/") file returnpath))) diff --git a/src/blarg/routes/home.clj b/src/blarg/routes/home.clj index 3e8bd79..b846d90 100644 --- a/src/blarg/routes/home.clj +++ b/src/blarg/routes/home.clj @@ -3,6 +3,7 @@ [compojure.core] [blarg.routes.helpers]) (:require [blarg.views.layout :as layout] + [blarg.route-utils :refer [register-routes]] [noir.response :as resp])) (defn about-page [] @@ -15,7 +16,7 @@ "projects.html" :params {:htmlTitle (->html-title ["Projects"])})) -(defroutes home-routes +(register-routes home-routes (GET "/about" [] (about-page)) (GET "/projects" [] (projects-page)) (GET "/toslug" [text] (resp/json {:slug (if text (->slug text))}))) diff --git a/src/blarg/routes/posts.clj b/src/blarg/routes/posts.clj index 85ec3e2..3c03947 100644 --- a/src/blarg/routes/posts.clj +++ b/src/blarg/routes/posts.clj @@ -10,7 +10,8 @@ [noir.session :as session] [blarg.views.layout :as layout] [blarg.models.posts :as posts] - [blarg.routes.auth :as auth])) + [blarg.routes.auth :as auth] + [blarg.route-utils :refer [register-routes]])) (defn string->tags [s] (->> (clojure.string/split s #",") @@ -121,7 +122,7 @@ (resp/redirect "/") (throw (Exception. "Error deleting post.")))) -(defroutes posts-routes +(register-routes posts-routes (GET "/" [page] (list-page (parse-int page 0))) (GET ["/:year/:month/:day/:slug/" :year #"[0-9]{4}" :month #"[0-9]{1,2}" :day #"[0-9]{1,2}" :slug #"(.*)"] diff --git a/src/blarg/routes/rss.clj b/src/blarg/routes/rss.clj index 10cfa3f..a4b1441 100644 --- a/src/blarg/routes/rss.clj +++ b/src/blarg/routes/rss.clj @@ -6,7 +6,8 @@ [clj-rss.core :as rss] [clj-time.core] [clj-time.coerce] - [blarg.models.posts :as posts])) + [blarg.models.posts :as posts] + [blarg.route-utils :refer [register-routes]])) (def rss-title "blarg.ca") (def rss-site-url "http://www.blarg.ca/") @@ -27,5 +28,5 @@ :description (md/md-to-html-string (:body post))})) (doall)))))) -(defroutes rss-routes +(register-routes rss-routes (GET "/rss" [] (handle-rss)))