routes are now automatically discovered and added to the ring handler

This commit is contained in:
Gered 2014-03-28 21:56:40 -04:00
parent 0112dc515a
commit cfee97fd95
6 changed files with 24 additions and 22 deletions

View file

@ -1,23 +1,18 @@
(ns blarg.handler (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]] (:require [noir.util.middleware :refer [app-handler]]
[noir.response :as resp] [noir.response :as resp]
[compojure.core :refer [defroutes]]
[compojure.route :as route] [compojure.route :as route]
[compojure.response :refer [render]] [compojure.response :refer [render]]
[taoensso.timbre :as timbre] [taoensso.timbre :as timbre]
[taoensso.timbre.appenders.rotor :as rotor] [taoensso.timbre.appenders.rotor :as rotor]
[clj-jtwig.core :as jtwig] [clj-jtwig.core :as jtwig]
[clj-jtwig.web.middleware :refer [wrap-servlet-context-path]] [clj-jtwig.web.middleware :refer [wrap-servlet-context-path]]
[blarg.config :refer [config-val]]
[blarg.views.layout :as layout] [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 (defroutes app-routes
(route/resources "/") (route/resources "/")
@ -60,8 +55,10 @@
:params {:errorInfo e} :params {:errorInfo e}
:status 500))))) :status 500)))))
(def app (app-handler (defonce routes (find-routes "blarg.routes." app-routes))
[auth-routes home-routes posts-routes files-routes rss-routes app-routes]
(defonce app (app-handler
routes
:middleware [wrap-exceptions wrap-servlet-context-path] :middleware [wrap-exceptions wrap-servlet-context-path]
:access-rules [{:redirect "/unauthorized" :rule auth-required}] :access-rules [{:redirect "/unauthorized" :rule auth-required}]
:formats [:json-kw :edn])) :formats [:json-kw :edn]))

View file

@ -4,6 +4,7 @@
[noir.util.route]) [noir.util.route])
(:require [blarg.views.layout :as layout] (:require [blarg.views.layout :as layout]
[blarg.models.users :as users] [blarg.models.users :as users]
[blarg.route-utils :refer [register-routes]]
[ring.util.response :refer [status]] [ring.util.response :refer [status]]
[ring.middleware.head :refer [wrap-head]] [ring.middleware.head :refer [wrap-head]]
[noir.session :as session] [noir.session :as session]
@ -37,7 +38,7 @@
(defn handle-unauthorized [] (defn handle-unauthorized []
(layout/render "unauthorized.html" :status 401)) (layout/render "unauthorized.html" :status 401))
(defroutes auth-routes (register-routes auth-routes
(GET "/unauthorized" [] (handle-unauthorized)) (GET "/unauthorized" [] (handle-unauthorized))
(GET "/login" [] (login-page)) (GET "/login" [] (login-page))
(POST "/login" [id pass] (handle-login id pass)) (POST "/login" [id pass] (handle-login id pass))

View file

@ -6,6 +6,7 @@
(:require [blarg.views.layout :as layout] (:require [blarg.views.layout :as layout]
[blarg.models.files :as files] [blarg.models.files :as files]
[blarg.routes.auth :as auth] [blarg.routes.auth :as auth]
[blarg.route-utils :refer [register-routes]]
[noir.response :as resp] [noir.response :as resp]
[noir.session :as session])) [noir.session :as session]))
@ -69,7 +70,7 @@
(resp/content-type (:content_type file) (:data file)) (resp/content-type (:content_type file) (:data file))
(resp/redirect "/notfound"))) (resp/redirect "/notfound")))
(defroutes files-routes (register-routes files-routes
(GET "/listfiles" [] (restricted (list-files "/"))) (GET "/listfiles" [] (restricted (list-files "/")))
(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))) (POST "/uploadfile" [path file returnpath] (restricted (handle-new-file (ensure-prefix-suffix path "/") file returnpath)))

View file

@ -3,6 +3,7 @@
[compojure.core] [compojure.core]
[blarg.routes.helpers]) [blarg.routes.helpers])
(:require [blarg.views.layout :as layout] (:require [blarg.views.layout :as layout]
[blarg.route-utils :refer [register-routes]]
[noir.response :as resp])) [noir.response :as resp]))
(defn about-page [] (defn about-page []
@ -15,7 +16,7 @@
"projects.html" "projects.html"
:params {:htmlTitle (->html-title ["Projects"])})) :params {:htmlTitle (->html-title ["Projects"])}))
(defroutes home-routes (register-routes home-routes
(GET "/about" [] (about-page)) (GET "/about" [] (about-page))
(GET "/projects" [] (projects-page)) (GET "/projects" [] (projects-page))
(GET "/toslug" [text] (resp/json {:slug (if text (->slug text))}))) (GET "/toslug" [text] (resp/json {:slug (if text (->slug text))})))

View file

@ -10,7 +10,8 @@
[noir.session :as session] [noir.session :as session]
[blarg.views.layout :as layout] [blarg.views.layout :as layout]
[blarg.models.posts :as posts] [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] (defn string->tags [s]
(->> (clojure.string/split s #",") (->> (clojure.string/split s #",")
@ -121,7 +122,7 @@
(resp/redirect "/") (resp/redirect "/")
(throw (Exception. "Error deleting post.")))) (throw (Exception. "Error deleting post."))))
(defroutes posts-routes (register-routes posts-routes
(GET "/" [page] (list-page (parse-int page 0))) (GET "/" [page] (list-page (parse-int page 0)))
(GET (GET
["/:year/:month/:day/:slug/" :year #"[0-9]{4}" :month #"[0-9]{1,2}" :day #"[0-9]{1,2}" :slug #"(.*)"] ["/:year/:month/:day/:slug/" :year #"[0-9]{4}" :month #"[0-9]{1,2}" :day #"[0-9]{1,2}" :slug #"(.*)"]

View file

@ -6,7 +6,8 @@
[clj-rss.core :as rss] [clj-rss.core :as rss]
[clj-time.core] [clj-time.core]
[clj-time.coerce] [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-title "blarg.ca")
(def rss-site-url "http://www.blarg.ca/") (def rss-site-url "http://www.blarg.ca/")
@ -27,5 +28,5 @@
:description (md/md-to-html-string (:body post))})) :description (md/md-to-html-string (:body post))}))
(doall)))))) (doall))))))
(defroutes rss-routes (register-routes rss-routes
(GET "/rss" [] (handle-rss))) (GET "/rss" [] (handle-rss)))