diff --git a/src/blarg/handler.clj b/src/blarg/handler.clj index 9f07713..13da2d5 100644 --- a/src/blarg/handler.clj +++ b/src/blarg/handler.clj @@ -8,11 +8,11 @@ blarg.config ring.middleware.head compojure.core) - (:require [noir.util.middleware :as middleware] + (:require [noir.util.middleware :refer [app-handler]] [noir.response :as resp] [compojure.route :as route] [taoensso.timbre :as timbre] - [com.postspectacular.rotor :as rotor] + [taoensso.timbre.appenders.rotor :as rotor] [selmer.parser :as parser] [blarg.views.layout :as layout] [blarg.models.db :as db])) @@ -25,54 +25,43 @@ :headers {"Content-Type" "text/html"} :body (layout/render "notfound.html")}))) -(defn init - "init will be called once when - app is deployed as a servlet on - an app server such as Tomcat - put any initialization code here" - [] +(defn init [] (timbre/set-config! [:appenders :rotor] - {:min-level :info - :enabled? true - :async? false ; should be always false for rotor + {:min-level :info + :enabled? true + :async? false ; should be always false for rotor :max-message-per-msecs nil - :fn rotor/append}) + :fn rotor/appender-fn}) (timbre/set-config! [:shared-appender-config :rotor] - {:path "blarg.log" :max-size 10000 :backlog 10}) + {:path "blarg.log" :max-size (* 512 1024) :backlog 10}) (timbre/info "blarg started successfully") - (if (= "DEV" (config-val :env)) + (when (= "DEV" (config-val :env)) + (timbre/info "Dev environment. Template caching disabled.") (parser/toggle-caching)) (timbre/info "touching database...") (db/touch-databases)) -(defn destroy - "destroy will be called when your application - shuts down, put any clean up code here" - [] +(defn destroy [] (timbre/info "blarg is shutting down...")) -(defn wrap-exceptions [app] +(defn wrap-exceptions [handler] (fn [request] (try - (app request) - (catch Exception e + (handler request) + (catch Throwable e (.printStackTrace e) {:status 500 :headers {"Content-Type" "text/html"} :body (layout/render "error.html" {:error-info e})})))) -;;append your application routes to the all-routes vector -(def all-routes [auth-routes home-routes posts-routes files-routes rss-routes]) - -(def app (middleware/app-handler - (conj all-routes app-routes) +(def app (app-handler + [auth-routes home-routes posts-routes files-routes rss-routes app-routes] :middleware [wrap-exceptions] - :access-rules [[{:redirect "/unauthorized"} auth-required]])) - -(def war-handler (middleware/war-handler app)) + :access-rules [[{:redirect "/unauthorized"} auth-required]] + :formats [:json-kw :edn])) diff --git a/src/blarg/repl.clj b/src/blarg/repl.clj index 13e1280..46eade9 100644 --- a/src/blarg/repl.clj +++ b/src/blarg/repl.clj @@ -6,33 +6,21 @@ (defonce server (atom nil)) (defn get-handler [] - ;; #'app expands to (var app) so that when we reload our code, - ;; the server is forced to re-resolve the symbol in the var - ;; rather than having its own copy. When the root binding - ;; changes, the server picks it up without having to restart. (-> #'app - ; Makes static assets in $PROJECT_DIR/resources/public/ available. - (wrap-file "resources") - ; Content-Type, Content-Length, and Last Modified headers for files in body - (wrap-file-info))) + (wrap-file "resources") + (wrap-file-info))) -(defn start-server - "used for starting the server in development mode from REPL" - [& [port]] - (let [port (if port (Integer/parseInt port) 8080)] +(defn start-server [& [port]] + (let [port (if port (Integer/parseInt port) 3000)] (reset! server (serve (get-handler) - {:port port - :init init + {:port port + :init init :auto-reload? true - :destroy destroy - :join true})) + :destroy destroy + :join? false})) (println (str "You can view the site at http://localhost:" port)))) (defn stop-server [] (.stop @server) (reset! server nil)) - -#_(start-server) - -#_(stop-server) diff --git a/src/blarg/routes/auth.clj b/src/blarg/routes/auth.clj index 3980210..6fde196 100644 --- a/src/blarg/routes/auth.clj +++ b/src/blarg/routes/auth.clj @@ -40,4 +40,4 @@ (GET "/login" [] (login-page)) (POST "/login" [id pass] (handle-login id pass)) (GET "/logout" [] (logout)) - (restricted GET "/private" [] "private!")) + (GET "/private" [] (restricted "private!"))) diff --git a/src/blarg/routes/files.clj b/src/blarg/routes/files.clj index 3d6960f..2e1e7cc 100644 --- a/src/blarg/routes/files.clj +++ b/src/blarg/routes/files.clj @@ -70,11 +70,11 @@ (resp/redirect "/notfound"))) (defroutes files-routes - (restricted GET "/listfiles" [] (list-files "/")) - (restricted GET "/listfiles/*" [*] (list-files *)) - (restricted POST "/uploadfile" [path file returnpath] (handle-new-file (ensure-prefix-suffix path "/") file returnpath)) - (restricted POST "/updatefile" [id file returnpath] (handle-update-file (ensure-prefix id "/") file returnpath)) - (restricted POST "/deletefile" [id returnpath] (handle-delete-file (ensure-prefix id "/") returnpath)) - (restricted GET "/publishfile/*" [* returnpath] (handle-publish-file * true returnpath)) - (restricted GET "/unpublishfile/*" [* returnpath] (handle-publish-file * false returnpath)) + (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 "/updatefile" [id file returnpath] (restricted (handle-update-file (ensure-prefix id "/") file returnpath))) + (POST "/deletefile" [id returnpath] (restricted (handle-delete-file (ensure-prefix id "/") returnpath))) + (GET "/publishfile/*" [* returnpath] (restricted (handle-publish-file * true returnpath))) + (GET "/unpublishfile/*" [* returnpath] (restricted (handle-publish-file * false returnpath))) (GET "/files/*" [*] (get-file *))) diff --git a/src/blarg/routes/posts.clj b/src/blarg/routes/posts.clj index 450934c..37437cc 100644 --- a/src/blarg/routes/posts.clj +++ b/src/blarg/routes/posts.clj @@ -131,10 +131,10 @@ (show-post-page year month day slug)) (GET "/tag/:tag" [tag] (list-by-tag tag)) (GET "/archive" [] (list-archive)) - (restricted GET "/newpost" [] (new-post-page)) - (restricted POST "/newpost" [title tags body] (handle-new-post title tags body)) - (restricted GET "/editpost/:id" [id] (edit-post-page id)) - (restricted POST "/editpost/:id" [id title tags body] (handle-edit-post id title tags body)) - (restricted GET "/publish/:id" [id reset-date] (handle-publish-post id true reset-date)) - (restricted GET "/unpublish/:id" [id] (handle-publish-post id false false)) - (restricted GET "/deletepost/:id" [id] (handle-delete-post id))) + (GET "/newpost" [] (restricted (new-post-page))) + (POST "/newpost" [title tags body] (restricted (handle-new-post title tags body))) + (GET "/editpost/:id" [id] (restricted (edit-post-page id))) + (POST "/editpost/:id" [id title tags body] (restricted (handle-edit-post id title tags body))) + (GET "/publish/:id" [id reset-date] (restricted (handle-publish-post id true reset-date))) + (GET "/unpublish/:id" [id] (restricted (handle-publish-post id false false))) + (GET "/deletepost/:id" [id] (restricted (handle-delete-post id)))) diff --git a/src/blarg/routes/rss.clj b/src/blarg/routes/rss.clj index 9f03369..5df30d0 100644 --- a/src/blarg/routes/rss.clj +++ b/src/blarg/routes/rss.clj @@ -28,4 +28,4 @@ (doall)))))) (defroutes rss-routes - (GET "/rss" [] (handle-rss))) + (GET "/rss" [] (handle-rss))) diff --git a/src/blarg/views/layout.clj b/src/blarg/views/layout.clj index 1315861..8a3a172 100644 --- a/src/blarg/views/layout.clj +++ b/src/blarg/views/layout.clj @@ -1,14 +1,23 @@ (ns blarg.views.layout - (:use noir.request) (:require [selmer.parser :as parser] - [noir.session :as session] - [blarg.views.viewfilters])) + [ring.util.response :refer [content-type response]] + [compojure.response :refer [Renderable]] + [noir.session :as session]) + (:use [blarg.views.viewfilters])) (def template-path "blarg/views/templates/") +(deftype RenderableTemplate [template params] + Renderable + (render [this request] + (content-type + (response + (parser/render-file + (str template-path template) + (assoc params + :context (:context request) + :user-id (session/get :user)))) + "text/html; charset=utf-8"))) + (defn render [template & [params]] - (parser/render-file - (str template-path template) - (assoc params - :context (:context *request*) - :user-id (session/get :user)))) + (RenderableTemplate. template params))