From 2f6f9286a329b9d9733a4733dc151aa34be19fed Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 11 Sep 2017 13:04:51 +0100 Subject: [PATCH] Tidy-up and robustness; no major functional change. --- project.clj | 2 - resources/passwd | 2 +- resources/public/content/stylesheet.css | 8 + src/smeagol/authenticate.clj | 8 +- src/smeagol/configuration.clj | 3 +- src/smeagol/handler.clj | 2 +- src/smeagol/routes/admin.clj | 2 +- src/smeagol/routes/wiki.clj | 11 +- src/smeagol/sanity.clj | 185 +++++++++++++++--------- src/smeagol/util.clj | 22 ++- 10 files changed, 147 insertions(+), 98 deletions(-) diff --git a/project.clj b/project.clj index b3e3f82..4159363 100644 --- a/project.clj +++ b/project.clj @@ -8,9 +8,7 @@ [com.cemerick/url "0.1.1"] [com.fzakaria/slf4j-timbre "0.3.7"] [com.taoensso/encore "2.92.0"] - [com.cemerick/url "0.1.1"] [com.taoensso/timbre "4.10.0"] - [com.fzakaria/slf4j-timbre "0.3.7"] [com.taoensso/tower "3.0.2" :exclusions [com.taoensso/encore]] [crypto-password "0.2.0"] [environ "1.1.0"] diff --git a/resources/passwd b/resources/passwd index 6b5e6ac..fe61aaf 100644 --- a/resources/passwd +++ b/resources/passwd @@ -1 +1 @@ -{:admin {:admin true, :email "info@weft.scot", :password "admin"}, :jenny {:email "jenny@auchencairn.org", :admin false, :password "$s0$f0801$1uniQfftB37G5e5GklJANQ==$kQ0+/YcCuaz2x5iYjwhNlDlnWX/exE/8pSC+R4C0WvQ="}} \ No newline at end of file +{:admin {:admin true, :email "info@weft.scot", :password "admin"}} diff --git a/resources/public/content/stylesheet.css b/resources/public/content/stylesheet.css index 187b346..81a661c 100644 --- a/resources/public/content/stylesheet.css +++ b/resources/public/content/stylesheet.css @@ -265,6 +265,14 @@ th { padding: 0 2em 0 0; } +.sanity-cause .sanity-stacktrace { + display: none; +} + +.sanity-cause:hover .sanity-stacktrace { + display: block; +} + .vega-bindings, .vega-actions { font-size: 66%; } diff --git a/src/smeagol/authenticate.clj b/src/smeagol/authenticate.clj index 8f83703..c65b5ad 100644 --- a/src/smeagol/authenticate.clj +++ b/src/smeagol/authenticate.clj @@ -38,7 +38,7 @@ (def password-file-path (or (env :smeagol-passwd) - (str (clojure.java.io/resource "passwd")))) + (str (io/resource-path) "../passwd"))) (defn- get-users @@ -112,7 +112,7 @@ (timbre/info (str "Successfully changed password for user " username)) true)) (catch Exception any - (timbre/error + (timbre/error any (format "Changing password failed for user %s failed: %s (%s)" username (.getName (.getClass any)) (.getMessage any))) false)))) @@ -162,7 +162,7 @@ (timbre/info "Successfully added user " username) true) (catch Exception any - (timbre/error + (timbre/error any (format "Adding user %s failed: %s (%s)" username (.getName (.getClass any)) (.getMessage any))) false))))) @@ -179,7 +179,7 @@ (timbre/info (str "Successfully deleted user " username)) true) (catch Exception any - (timbre/error + (timbre/error any (format "Deleting user %s failed: %s (%s)" username (.getName (.getClass any)) (.getMessage any))) false)))) diff --git a/src/smeagol/configuration.clj b/src/smeagol/configuration.clj index d630adc..0f9bcd4 100644 --- a/src/smeagol/configuration.clj +++ b/src/smeagol/configuration.clj @@ -37,7 +37,6 @@ ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - (def config-file-path "The relative path to the config file." (or @@ -50,5 +49,5 @@ (try (read-string (slurp config-file-path)) (catch Exception any - (timbre/error "Could not load configuration" any) + (timbre/error any "Could not load configuration") {}))) diff --git a/src/smeagol/handler.clj b/src/smeagol/handler.clj index 2b2c0f9..d44dfc8 100644 --- a/src/smeagol/handler.clj +++ b/src/smeagol/handler.clj @@ -76,7 +76,7 @@ (timbre/info "\n-=[ smeagol started successfully" (when (env :dev) "using the development profile") "]=-") (catch Exception any - (timbre/error "Failure during startup" any) + (timbre/error any "Failure during startup") (destroy)))) ;; timeout sessions after 30 minutes diff --git a/src/smeagol/routes/admin.clj b/src/smeagol/routes/admin.clj index 9491116..106ca35 100644 --- a/src/smeagol/routes/admin.clj +++ b/src/smeagol/routes/admin.clj @@ -98,7 +98,7 @@ :details details :users (auth/list-users)}))) (catch Exception any - (timbre/error (.getMessage any)) + (timbre/error any) (layout/render "edit-user.html" (merge (util/standard-params request) {:title (str (:edit-title-prefix (util/get-messages request)) " " (:target params)) diff --git a/src/smeagol/routes/wiki.clj b/src/smeagol/routes/wiki.clj index 15b3a68..fe80349 100644 --- a/src/smeagol/routes/wiki.clj +++ b/src/smeagol/routes/wiki.clj @@ -45,13 +45,6 @@ ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(defn get-git-repo - "Get the git repository for my content, creating it if necessary" - [] - (hist/load-or-init-repo util/content-dir)) - - (defn process-source "Process `source-text` and save it to the specified `file-path`, committing it to Git and finally redirecting to wiki-page." @@ -61,7 +54,7 @@ file-name (str page suffix) file-path (cjio/file util/content-dir file-name) exists? (.exists (cjio/as-file file-path)) - git-repo (get-git-repo) + git-repo (hist/load-or-init-repo util/content-dir) user (session/get :user) email (auth/get-email user) summary (format "%s: %s" user (or (:summary params) "no summary"))] @@ -157,7 +150,7 @@ [request] (let [params (keywordize-keys (:params request)) data-path (str (io/resource-path) "/content/uploads/") - git-repo (get-git-repo) + git-repo (hist/load-or-init-repo util/content-dir) upload (:upload params) uploaded (if upload (ul/store-upload params data-path)) user (session/get :user) diff --git a/src/smeagol/sanity.clj b/src/smeagol/sanity.clj index 02f7b2d..e7c0503 100644 --- a/src/smeagol/sanity.clj +++ b/src/smeagol/sanity.clj @@ -252,13 +252,28 @@ (as-hiccup [this dictionary] "") clojure.lang.Keyword - (as-hiccup [this dictionary] (str (or (this dictionary)(string/replace (name this) "-" " ")) " ")) + (as-hiccup [this dictionary] + (str + (or + (this dictionary) + (string/replace (name this) "-" " ")) + " ")) clojure.lang.PersistentList - (as-hiccup [this dictionary] (apply vector (cons :div (map #(as-hiccup % dictionary) this)))) + (as-hiccup [this dictionary] + (apply + vector + (cons + :div + (map #(as-hiccup % dictionary) this)))) clojure.lang.PersistentVector - (as-hiccup [this dictionary] (apply vector (cons :div (map #(as-hiccup % dictionary) this)))) + (as-hiccup [this dictionary] + (apply + vector + (cons + :div + (map #(as-hiccup % dictionary) this)))) clojure.lang.PersistentArrayMap (as-hiccup [this dictionary] @@ -296,66 +311,94 @@ vector (cons :div - (cons - {:class "sanity-exception"} - (map - (fn [x] - [:div - {:class "sanity-cause"} - (.getMessage x) - [:div {:class "sanity-stacktrace"} - (apply - vector - (cons - :ol - (map - as-hiccup - (.getStackTrace x) - dictionary)))]]) - (get-causes this)))))) + (cons + {:class "sanity-exception"} + (map + (fn [x] + [:div + {:class "sanity-cause"} + [:h2 (.getMessage x)] + [:div {:class "sanity-stacktrace"} + (apply + vector + (cons + :ol + (map + as-hiccup + (.getStackTrace x) + dictionary)))]]) + (get-causes this)))))) java.lang.Object (as-hiccup [this dictionary] (str this " "))) -(defn sanity-check-report - [problems] +(defn get-locale-messages + "Get messages for the server-side locale." + [] (let [locale (Locale/getDefault) - locale-specifier (str (.getLanguage locale) "-" (.getCountry locale)) - messages (try - (i18n/get-messages locale-specifier "i18n" "en-GB") - (catch Exception any {}))] + locale-specifier (str (.getLanguage locale) "-" (.getCountry locale))] + (try + (i18n/get-messages locale-specifier "i18n" "en-GB") + (catch Exception any {})))) + + +;; Prepackaged hiccup sub-units +(defn as-hiccup-head + [messages] + [:head + [:title (as-hiccup :smeagol-not-initialised messages)] + [:link {:href "/content/stylesheet.css" :rel "stylesheet"}]]) + + +(defn as-hiccup-header + [messages] + [:header + [:div {:id "nav"} " "] + [:h1 (as-hiccup :smeagol-not-initialised messages)] + [:p " "]]) + + +(defn as-hiccup-see-doc + [messages] + [:p (as-hiccup :see-documentation messages) + [:a + {:href + "https://github.com/journeyman-cc/smeagol/wiki/Deploying-Smeagol"} + (as-hiccup :here messages)] "."]) + + +(defn as-hiccup-footer + [messages] + [:footer + [:div {:id "credits"} + [:div + [:img {:height "16" :width "16" :alt "one wiki to rule them all" :src "img/smeagol.png"}] + " One Wiki to rule them all || Smeagol wiki engine || " + [:img + {:height "16" :width "16" + :alt "The Web Engineering Factory & Toolworks" + :src "http://www.weft.scot/images/weft.logo.64.png"}] + " Developed by " + [:a {:href "http://www.weft.scot/"}"WEFT"]]]]) + + +(defn sanity-check-report + "Convert this `problem` report into a nicely formatted HTML page" + [problems] + (let [messages (get-locale-messages)] (html [:html - [:head - [:title (as-hiccup :smeagol-not-initialised messages)] - [:link {:href "/content/stylesheet.css" :rel "stylesheet"}]] + (as-hiccup-head messages) [:body - [:header - [:div {:id "nav"} " "] - [:h1 (as-hiccup :smeagol-not-initialised messages)] - [:p " "]] - [:div {:id "error" :class "error"} - [:div {:class "error"} - (as-hiccup [(count (keys problems)) :problems-found] messages)]] + (as-hiccup-header messages) + [:div {:id "error"} + [:p {:class "error"} + (rest (as-hiccup [(count (keys problems)) :problems-found] messages))]] [:div {:id "main-container" :class "sanity-check-report"} [:p (as-hiccup :smeagol-misconfiguration messages)] (as-hiccup problems messages) - [:p (as-hiccup :see-documentation messages) - [:a - {:href - "https://github.com/journeyman-cc/smeagol/blob/master/resources/public/content/Deploying%20Smeagol.md"} - (as-hiccup :here messages)]]] - [:footer - [:div {:id "credits"} - [:div - [:img {:height "16" :width "16" :alt "one wiki to rule them all" :src "img/smeagol.png"}] - " One Wiki to rule them all || Smeagol wiki engine || " - [:img - {:height "16" :width "16" - :alt "The Web Engineering Factory & Toolworks" - :src "http://www.weft.scot/images/weft.logo.64.png"}] - " Developed by " - [:a {:href "http://www.weft.scot/"}"WEFT"]]]]]]))) + (as-hiccup-see-doc messages)] + (as-hiccup-footer messages)]]))) (defn- raw-sanity-check-installation @@ -383,26 +426,26 @@ If no argument is passed, run the sanity check and if it fails return page contents; if `error` is passed, just return page content describing the error." ([error] - (html - [:html - [:head - [:title "Smeagol is not initialised correctly"] - [:link {:href "/content/stylesheet.css" :rel "stylesheet"}]] - [:body - [:header - [:h1 "Smeagol is not initialised correctly"]] - [:div {:id "error"} - [:p {:class "error"} (.getMessage error)]] - [:p "There was a problem launching Smeagol probably because of misconfiguration:"] - (apply - vector - (cons :ol - (map #(vector :li (.getMessage %)) - (get-causes error)))) - [:p :see-documentation - [:a {:href "https://github.com/journeyman-cc/smeagol/blob/develop/resources/public/content/Deploying%20Smeagol.md"} "here"]]]])) + (let [messages (get-locale-messages)] + (html + [:html + (as-hiccup-head messages) + [:body + (as-hiccup-header messages) + [:div {:id "error"} + [:p {:class "error"} (.getMessage error)]] + [:div {:id "main-container" :class "sanity-check-report"} + [:p (as-hiccup :smeagol-misconfiguration messages)] + (as-hiccup error messages) + (as-hiccup-see-doc messages)] + (as-hiccup-footer messages)]]))) ([] (try (sanity-check-installation) - (catch Exception any (show-sanity-check-error any))))) + (catch Exception any + (timbre/error any "Failure during sanity check") + (show-sanity-check-error any))))) + +(show-sanity-check-error (Exception. "That's insane!")) + diff --git a/src/smeagol/util.clj b/src/smeagol/util.clj index 69cce68..902e377 100644 --- a/src/smeagol/util.clj +++ b/src/smeagol/util.clj @@ -8,7 +8,8 @@ [scot.weft.i18n.core :as i18n] [smeagol.authenticate :as auth] [smeagol.configuration :refer [config]] - [smeagol.formatting :refer [md->html]])) + [smeagol.formatting :refer [md->html]] + [taoensso.timbre :as timbre])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; @@ -55,12 +56,19 @@ "Return the most acceptable messages collection we have given the `Accept-Language` header in this `request`." [request] - (merge - (i18n/get-messages - ((:headers request) "accept-language") - "i18n" - "en-GB") - config)) + (let [specifier ((:headers request) "accept-language") + messages (try + (i18n/get-messages specifier "i18n" "en-GB") + (catch Exception any + (timbre/error + any + (str + "Failed to parse accept-language header " + specifier)) + {}))] + (merge + messages + config))) (def get-messages (memoize raw-get-messages))