From 4daedee4fc5bb30482b130fce0d4b19b3aa090fc Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 10 Aug 2016 20:06:07 +0100 Subject: [PATCH 1/6] Base template updated so that nicely formatted rule errors are displayed correctly. --- resources/templates/base.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/resources/templates/base.html b/resources/templates/base.html index 35c830b..81255cb 100644 --- a/resources/templates/base.html +++ b/resources/templates/base.html @@ -45,7 +45,9 @@ {% endif %} {% if error %}
-

{{error}}

+
+{{error}}
+      
{% endif %} From 4861ee809b613db9c25bebfb8d47430e088e6652 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 21 Aug 2016 14:16:18 +0100 Subject: [PATCH 2/6] Standardised header documentation in line with current best practice. --- resources/templates/base.html | 3 +- src/mw_ui/handler.clj | 32 +++++++++++++++++++-- src/mw_ui/layout.clj | 32 +++++++++++++++++++-- src/mw_ui/middleware.clj | 31 +++++++++++++++++++- src/mw_ui/render_world.clj | 33 +++++++++++++++++++++- src/mw_ui/repl.clj | 31 ++++++++++++++++++-- src/mw_ui/routes/home.clj | 36 +++++++++++++++++++++--- src/mw_ui/routes/load.clj | 28 +++++++++++++++++- src/mw_ui/routes/params.clj | 45 +++++++++++++++++++++++------ src/mw_ui/routes/rules.clj | 53 ++++++++++++++++++++++++++--------- src/mw_ui/routes/save.clj | 29 +++++++++++++++++-- src/mw_ui/util.clj | 33 ++++++++++++++++++++-- 12 files changed, 343 insertions(+), 43 deletions(-) diff --git a/resources/templates/base.html b/resources/templates/base.html index 81255cb..2be5573 100644 --- a/resources/templates/base.html +++ b/resources/templates/base.html @@ -59,7 +59,8 @@ Built with LuminusWeb || λ Powered by Clojure || Engineering and hosting by Journeyman || - World generated using MicroWorld Engine + World generated using MicroWorld Engine || + Version {{version}} diff --git a/src/mw_ui/handler.clj b/src/mw_ui/handler.clj index 7e614d1..66d7fe7 100644 --- a/src/mw_ui/handler.clj +++ b/src/mw_ui/handler.clj @@ -1,4 +1,6 @@ -(ns mw-ui.handler +(ns ^{:doc "Set up and tear down the request handler." + :author "Simon Brooke"} + mw-ui.handler (:require [compojure.core :refer [defroutes]] [mw-ui.routes.home :refer [home-routes]] [mw-ui.middleware :refer [load-middleware]] @@ -10,10 +12,34 @@ [selmer.parser :as parser] [environ.core :refer [env]])) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (defroutes app-routes (route/resources "/") (route/not-found "Not Found")) + (defn init "init will be called once when app is deployed as a servlet on @@ -27,14 +53,15 @@ :async? false ; should be always false for rotor :max-message-per-msecs nil :fn rotor/appender-fn}) - (timbre/set-config! [:shared-appender-config :rotor] {:path "mw_ui.log" :max-size (* 512 1024) :backlog 10}) + (if (env :dev) (parser/cache-off!)) (timbre/info "mw-ui started successfully")) + (defn destroy "destroy will be called when your application shuts down, put any clean up code here" @@ -42,7 +69,6 @@ (timbre/info "mw-ui is shutting down...")) - (def app (app-handler ;; add your application routes here [home-routes app-routes] diff --git a/src/mw_ui/layout.clj b/src/mw_ui/layout.clj index 6a6c224..9861426 100644 --- a/src/mw_ui/layout.clj +++ b/src/mw_ui/layout.clj @@ -1,16 +1,43 @@ -(ns mw-ui.layout +(ns ^{:doc "Layout content as HTML." + :author "Simon Brooke"} + mw-ui.layout (:require [selmer.parser :as parser] [clojure.string :as s] [ring.util.response :refer [content-type response]] [compojure.response :refer [Renderable]])) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + (def template-path "templates/") + (deftype RenderableTemplate [template params] Renderable (render [this request] (content-type - (->> (assoc params + (->> (assoc (merge params {:version (System/getProperty "mw-ui.version")}) (keyword (s/replace template #".html" "-selected")) "active" :servlet-context (if-let [context (:servlet-context request)] @@ -19,6 +46,7 @@ response) "text/html; charset=utf-8"))) + (defn render [template & [params]] (RenderableTemplate. template params)) diff --git a/src/mw_ui/middleware.clj b/src/mw_ui/middleware.clj index 3440b32..99c1a34 100644 --- a/src/mw_ui/middleware.clj +++ b/src/mw_ui/middleware.clj @@ -1,4 +1,6 @@ -(ns mw-ui.middleware +(ns ^{:doc "In truth, boilerplate from Luminus." + :author "Simon Brooke"} + mw-ui.middleware (:require [taoensso.timbre :as timbre] [selmer.parser :as parser] [environ.core :refer [env]] @@ -6,19 +8,46 @@ [noir-exception.core :refer [wrap-internal-error wrap-exceptions]])) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + (defn log-request [handler] (fn [req] (timbre/debug req) (handler req))) + (def development-middleware [log-request wrap-error-page wrap-exceptions]) + (def production-middleware [#(wrap-internal-error % :log (fn [e] (timbre/error e)))]) + (defn load-middleware [] (concat (when (env :dev) development-middleware) production-middleware)) diff --git a/src/mw_ui/render_world.clj b/src/mw_ui/render_world.clj index 9d103d5..2d91180 100644 --- a/src/mw_ui/render_world.clj +++ b/src/mw_ui/render_world.clj @@ -1,4 +1,6 @@ -(ns mw-ui.render-world +(ns ^{:doc "Render the state of the world as an HTML table." + :author "Simon Brooke"} + mw-ui.render-world (:require [clojure.java.io :as jio] [mw-engine.core :as engine] [mw-engine.world :as world] @@ -8,21 +10,47 @@ [noir.io :as io] [noir.session :as session])) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (defn format-css-class [statekey] "Format this statekey, assumed to be a keyword indicating a state in the world, into a CSS class" (subs (str statekey) 1)) + (defn format-image-path "Render this statekey, assumed to be a keyword indicating a state in the world, into a path which should recover the corresponding image file." [statekey] (format "img/tiles/%s.png" (format-css-class statekey))) + (defn format-mouseover [cell] (str cell)) + (defn render-cell "Render this world cell as a Hiccup table cell." [cell] @@ -31,11 +59,13 @@ [:a {:href (format "inspect?x=%d&y=%d" (:x cell) (:y cell))} [:img {:alt (:state cell) :src (format-image-path state)}]]])) + (defn render-world-row "Render this world row as a Hiccup table row." [row] (apply vector (cons :tr (map render-cell row)))) + (defn render-world-table "Render the world implied by the current session as a complete HTML table in a DIV." [] @@ -59,6 +89,7 @@ [:p (str "Generation " generation)]])) + (defn render-inspector "Render in Hiccup format the HTML content of an inspector on this cell." [cell table] diff --git a/src/mw_ui/repl.clj b/src/mw_ui/repl.clj index 3563fac..e034f28 100644 --- a/src/mw_ui/repl.clj +++ b/src/mw_ui/repl.clj @@ -1,9 +1,34 @@ -(ns mw-ui.repl +(ns ^{:doc "In truth, boilerplate from Luminus." + :author "Simon Brooke"} + mw-ui.repl (:use mw-ui.handler ring.server.standalone [ring.middleware file-info file]) - (:gen-class) -) + (:gen-class)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (defonce server (atom nil)) diff --git a/src/mw_ui/routes/home.clj b/src/mw_ui/routes/home.clj index ac779aa..efdda85 100644 --- a/src/mw_ui/routes/home.clj +++ b/src/mw_ui/routes/home.clj @@ -1,4 +1,6 @@ -(ns mw-ui.routes.home +(ns ^{:doc "Routes which serve the main pages of the application." + :author "Simon Brooke"} + mw-ui.routes.home (:use clojure.walk compojure.core) (:require [clojure.pprint :only [pprint]] @@ -15,6 +17,29 @@ [noir.session :as session] [ring.util.response :as response])) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (defn list-states [] (sort @@ -27,19 +52,22 @@ (layout/render "trusted-content.html" {:title "About MicroWorld" :about-selected "active" - :content (util/md->html "/md/about.md")})) + :content (util/md->html "/md/about.md") + :version (System/getProperty "mw-ui.version")})) (defn docs-page [] (layout/render "docs.html" {:title "Documentation" :parser (util/md->html "/md/mw-parser.md" ) :states (util/list-resources "/img/tiles" #"([0-9a-z-_]+).png") :lessons (util/list-resources "/md/lesson-plans" #"([0-9a-z-_]+).md") - :components ["mw-engine" "mw-parser" "mw-ui"]})) + :components ["mw-engine" "mw-parser" "mw-ui"] + :version (System/getProperty "mw-ui.version")})) (defn home-page [] "Render the home page." (layout/render "trusted-content.html" {:title "Welcome to MicroWorld" - :content (util/md->html "/md/mw-ui.md")})) + :content (util/md->html "/md/mw-ui.md") + :version (System/getProperty "mw-ui.version")})) (defn inspect-page [request] "Open an inspector on the cell at the co-ordinates specified in this request" diff --git a/src/mw_ui/routes/load.clj b/src/mw_ui/routes/load.clj index ce33831..0636ccf 100644 --- a/src/mw_ui/routes/load.clj +++ b/src/mw_ui/routes/load.clj @@ -1,4 +1,6 @@ -(ns mw-ui.routes.load +(ns ^{:doc "Route which handles the upload of worlds/rules from the client." + :author "Simon Brooke"} + mw-ui.routes.load (:use clojure.walk compojure.core) (:require [hiccup.core :refer [html]] @@ -8,6 +10,29 @@ [mw-ui.layout :as layout] )) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (defn- upload [file] (io/upload-file "/tmp/" file) @@ -16,6 +41,7 @@ (with-open [eddi (java.io.FileReader. (:tempfile file))] (read))) (str "Successfully loaded your world from " (:filename file)))) + (defn load-page "If no args, show the load form; with args, load a world file from the client. diff --git a/src/mw_ui/routes/params.clj b/src/mw_ui/routes/params.clj index fe05161..f68fcb3 100644 --- a/src/mw_ui/routes/params.clj +++ b/src/mw_ui/routes/params.clj @@ -1,4 +1,6 @@ -(ns mw-ui.routes.params +(ns ^{:doc "Route which serves and handles the parameters page." + :author "Simon Brooke"} + mw-ui.routes.params (:use clojure.walk clojure.java.io compojure.core) @@ -11,6 +13,30 @@ [noir.io :as io] [noir.session :as session])) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + (defn- send-params [] {:title "Choose your world" :heightmaps (util/list-resources "/img/heightmaps" #"([0-9a-z-_]+).png") @@ -18,7 +44,8 @@ :rulesets (util/list-resources "/rulesets" #"([0-9a-z-_]+).txt") }) -(defn params-page + +(defn params-page "Handler for params request. If no `request` passed, show empty params form. If `request` is passed, put parameters from request into session and show the world page." @@ -31,25 +58,25 @@ pause (:pause params) rulefile (:ruleset params) rulepath (str "/rulesets/" rulefile ".txt")] - (if (not= map "") - (session/put! :world - (heightmap/apply-heightmap + (if (not= map "") + (session/put! :world + (heightmap/apply-heightmap (io/get-resource (str "/img/heightmaps/" map ".png"))))) (when (not= rulefile "") (session/put! :rule-text (io/slurp-resource rulepath)) (session/put! :rules (compiler/compile-file (io/get-resource rulepath)))) (if (not= pause "") (session/put! :pause pause)) - (layout/render "params.html" - (merge (send-params) + (layout/render "params.html" + (merge (send-params) {:r rulefile :h map :message "Your parameters are saved, now look at your world"}))) (catch Exception e (let [params (keywordize-keys (:form-params request))] - (layout/render "params.html" + (layout/render "params.html" (merge (send-params) - {:title "Choose your world" + {:title "Choose your world" :r (:ruleset params) :h (:heightmap params) :message "Your paramters are not saved" diff --git a/src/mw_ui/routes/rules.clj b/src/mw_ui/routes/rules.clj index 7512605..b49a4c8 100644 --- a/src/mw_ui/routes/rules.clj +++ b/src/mw_ui/routes/rules.clj @@ -1,4 +1,6 @@ -(ns mw-ui.routes.rules +(ns ^{:doc "Route which serves and handles the rules page." + :author "Simon Brooke"} + mw-ui.routes.rules (:use clojure.walk compojure.core) (:require [hiccup.core :refer [html]] @@ -10,31 +12,56 @@ [noir.session :as session] [ring.util.response :as response])) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + (defn process-rules-request [request] (let [src (:src (keywordize-keys (:form-params request)))] - (try - (cond src + (try + (cond src (let [rules (compiler/compile-string src)] {:rule-text src :rules rules - :message (str "Successfully compiled " - (count rules) + :message (str "Successfully compiled " + (count rules) " rules") }) - true {:rule-text (or - (session/get :rule-text) + true {:rule-text (or + (session/get :rule-text) (io/slurp-resource "/rulesets/basic.txt")) :message "No rules found in request; loading defaults"}) - (catch Exception e + (catch Exception e {:rule-text src :message "An error occurred during compilation" :error (str (.getName (.getClass e)) ": " (.getMessage e))})))) -(defn rules-page + +(defn rules-page "Request handler for the `rules` request. If the `request` contains a value - for `:src`, treat that as rule source and try to compile it. If compilation - succeeds, stash the compiled rules and the rule text on the session, and - provide feedback; if not, provide feedback. + for `:src`, treat that as rule source and try to compile it. If compilation + succeeds, stash the compiled rules and the rule text on the session, and + provide feedback; if not, provide feedback. If `request` doesn't contain a value for `:src`, load basic rule source from the session or from `resources/rulesets/basic.txt` and pass that back." @@ -44,7 +71,7 @@ (session/put! :rules (:rules processed))) (if (:rule-text processed) (session/put! :rule-text (:rule-text processed))) - (layout/render "rules.html" + (layout/render "rules.html" (merge {:title "Edit Rules"} processed)))) ([] (rules-page nil))) diff --git a/src/mw_ui/routes/save.clj b/src/mw_ui/routes/save.clj index 26f3688..d75417a 100644 --- a/src/mw_ui/routes/save.clj +++ b/src/mw_ui/routes/save.clj @@ -1,7 +1,32 @@ -(ns mw-ui.routes.save +(ns ^{:doc "Route which handles the saving of world state the client." + :author "Simon Brooke"} + mw-ui.routes.save (:require [clojure.pprint :as pretty :only [pprint]] [noir.session :as session] - [ring.util.response :as response])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (defn save-page [] "Save the current world to the browser, using our own custom mime-type in diff --git a/src/mw_ui/util.clj b/src/mw_ui/util.clj index 46def67..fa59383 100644 --- a/src/mw_ui/util.clj +++ b/src/mw_ui/util.clj @@ -1,8 +1,34 @@ -(ns mw-ui.util +(ns ^{:doc "Utility functions used by other namespaces in this package." + :author "Simon Brooke"} + mw-ui.util (:require [noir.io :as io] [noir.session :as session] [markdown.core :as md])) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;; +;;;; mw-ui: a servlet user/visualisation interface for MicroWorld. +;;;; +;;;; This program is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU General Public License +;;;; as published by the Free Software Foundation; either version 2 +;;;; of the License, or (at your option) any later version. +;;;; +;;;; This program is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;;; GNU General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU General Public License +;;;; along with this program; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +;;;; USA. +;;;; +;;;; Copyright (C) 2014 Simon Brooke +;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + (defn md->html "reads a markdown file from public/md and returns an HTML string" [filename] @@ -10,12 +36,13 @@ (io/slurp-resource filename) (md/md-to-html-string))) + (defn list-resources [directory pattern] "List resource files matching `pattern` in `directory`." - (let + (let [path (str (io/resource-path) directory)] (session/put! :list-resources-path path) (sort - (remove nil? + (remove nil? (map #(first (rest (re-matches pattern (.getName %)))) (file-seq (clojure.java.io/file path))))))) From c18b74fef6926f717af20575d73934ceb20a0f6a Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 27 Dec 2016 16:02:53 +0000 Subject: [PATCH 3/6] Dockerisation. --- Dockerfile | 3 +++ project.clj | 5 ++++- src/mw_ui/routes/save.clj | 8 ++++---- 3 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..afcd747 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,3 @@ +FROM tomcat:alpine +COPY target/microworld.war $CATALINA_HOME/webapps/ + diff --git a/project.clj b/project.clj index e280238..a3a3dfe 100644 --- a/project.clj +++ b/project.clj @@ -23,7 +23,10 @@ :repl-options {:init-ns mw-ui.repl} :plugins [[lein-ring "0.8.11"] [lein-environ "0.5.0"] - [lein-marginalia "0.7.1"]] + [lein-marginalia "0.7.1"] + [io.sarnowski/lein-docker "1.1.0"]] + :docker {:image-name "simonbrooke/microworld" + :dockerfile "Dockerfile"} :ring {:handler mw-ui.handler/app :init mw-ui.handler/init :destroy mw-ui.handler/destroy diff --git a/src/mw_ui/routes/save.clj b/src/mw_ui/routes/save.clj index d75417a..407495b 100644 --- a/src/mw_ui/routes/save.clj +++ b/src/mw_ui/routes/save.clj @@ -3,6 +3,7 @@ mw-ui.routes.save (:require [clojure.pprint :as pretty :only [pprint]] [noir.session :as session] + [noir.response :as response])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; @@ -33,8 +34,7 @@ an attempt to prevent the browser trying to do anything clever with it. Note that it is saved as a raw Clojure data structure, not as XML or any proprietary format." - (response/header - (response/response - (with-out-str (pretty/pprint (session/get :world)))) - "Content-Type" "application/journeyman-mwm; charset=utf-8")) + (response/content-type + "application/journeyman-mwm; charset=utf-8" + (with-out-str (pretty/pprint (session/get :world))))) From 1e9ce7b9a0cf306df66bc279eea694972d9fdf1f Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Mon, 4 Dec 2017 17:11:30 +0000 Subject: [PATCH 4/6] Small fixes arising out of `lein ancient` updates. --- project.clj | 32 ++++++++++++++++++-------------- resources/templates/params.html | 3 ++- resources/templates/rules.html | 3 ++- src/mw_ui/handler.clj | 21 ++++++++++----------- src/mw_ui/layout.clj | 4 ++++ src/mw_ui/render_world.clj | 3 +-- 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/project.clj b/project.clj index a3a3dfe..2476ddb 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject mw-ui "0.1.5-SNAPSHOT" +(defproject mw-ui "0.1.5" :description "Web-based user interface for MicroWorld" :url "http://www.journeyman.cc/microworld" :manifest { @@ -8,17 +8,21 @@ "build-signature-timestamp" "unset" "Implementation-Version" "unset" } - :dependencies [[org.clojure/clojure "1.6.0"] - [mw-engine "0.1.5-SNAPSHOT"] - [mw-parser "0.1.5-SNAPSHOT"] - [lib-noir "0.8.4"] - [ring-server "0.3.1"] - [selmer "0.6.8"] - [com.taoensso/timbre "3.2.1"] - [com.taoensso/tower "2.0.2"] - [markdown-clj "0.9.44"] - [environ "0.5.0"] - [noir-exception "0.2.2"]] + :dependencies [[org.clojure/clojure "1.8.0"] + [mw-engine "0.1.6-SNAPSHOT"] + [mw-parser "0.1.6-SNAPSHOT"] + [lib-noir "0.9.9"] + [ring-server "0.5.0"] + [selmer "1.11.3" :exclusions [cheshire + com.fasterxml.jackson.core/jackson-core + com.fasterxml.jackson.dataformat/jackson-dataformat-smile + com.fasterxml.jackson.dataformat/jackson-dataformat-cbor]] + [hiccup "1.0.5"] + [com.taoensso/timbre "4.10.0" :exclusions [org.clojure/tools.reader]] + [com.taoensso/tower "3.0.2"] + [markdown-clj "1.0.1"] + [environ "1.1.0"] + [noir-exception "0.2.5"]] :repl-options {:init-ns mw-ui.repl} :plugins [[lein-ring "0.8.11"] @@ -40,8 +44,8 @@ :stacktraces? false :auto-reload? false}} :dev {:dependencies [[ring-mock "0.1.5"] - [ring/ring-devel "1.3.0"] - [pjstadig/humane-test-output "0.6.0"]] + [ring/ring-devel "1.6.3" :exclusions [ring/ring-codec]] + [pjstadig/humane-test-output "0.8.3"]] :injections [(require 'pjstadig.humane-test-output) (pjstadig.humane-test-output/activate!)] :env {:dev true}}} diff --git a/resources/templates/params.html b/resources/templates/params.html index f0e13cb..c872da8 100644 --- a/resources/templates/params.html +++ b/resources/templates/params.html @@ -1,6 +1,7 @@ {% extends "templates/base.html" %} {% block content %}
+ {% csrf-field %}

@@ -11,4 +12,4 @@

-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/src/mw_ui/handler.clj b/src/mw_ui/handler.clj index 66d7fe7..f1d6bd2 100644 --- a/src/mw_ui/handler.clj +++ b/src/mw_ui/handler.clj @@ -8,7 +8,7 @@ [noir.util.middleware :refer [app-handler]] [compojure.route :as route] [taoensso.timbre :as timbre] - [taoensso.timbre.appenders.rotor :as rotor] + [taoensso.timbre.appenders.3rd-party.rotor :as rotor] [selmer.parser :as parser] [environ.core :refer [env]])) @@ -46,16 +46,15 @@ an app server such as Tomcat put any initialization code here" [] - (timbre/set-config! - [:appenders :rotor] - {:min-level :info - :enabled? true - :async? false ; should be always false for rotor - :max-message-per-msecs nil - :fn rotor/appender-fn}) - (timbre/set-config! - [:shared-appender-config :rotor] - {:path "mw_ui.log" :max-size (* 512 1024) :backlog 10}) + (timbre/merge-config! + {:appenders + {:rotor (rotor/rotor-appender + {:path "mw-ui.log" + :max-size (* 512 1024) + :backlog 10})} + :level (or + (if (env :dev) :debug) + :info)}) (if (env :dev) (parser/cache-off!)) diff --git a/src/mw_ui/layout.clj b/src/mw_ui/layout.clj index 9861426..37a4323 100644 --- a/src/mw_ui/layout.clj +++ b/src/mw_ui/layout.clj @@ -3,6 +3,7 @@ mw-ui.layout (:require [selmer.parser :as parser] [clojure.string :as s] + [ring.util.anti-forgery :refer [anti-forgery-field]] [ring.util.response :refer [content-type response]] [compojure.response :refer [Renderable]])) @@ -33,6 +34,9 @@ (def template-path "templates/") +(parser/add-tag! :csrf-field (fn [_ _] (anti-forgery-field))) + + (deftype RenderableTemplate [template params] Renderable (render [this request] diff --git a/src/mw_ui/render_world.clj b/src/mw_ui/render_world.clj index 2d91180..a5635ec 100644 --- a/src/mw_ui/render_world.clj +++ b/src/mw_ui/render_world.clj @@ -78,8 +78,7 @@ (io/get-resource "/rulesets/basic.txt"))) (session/get :rules))) generation (inc (or (session/get :generation) 0)) - w2 (engine/transform-world world rules) - ] + w2 (engine/transform-world world rules)] (session/put! :world w2) (session/put! :generation generation) [:div {:class "world"} From 3567838fa874c02ed46e04ba799b19d50e423df0 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Mon, 1 Jun 2020 20:09:36 +0100 Subject: [PATCH 5/6] Updated .gitignore to ignore build.tmp directories. --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index cf11b31..f0d939b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,9 +8,14 @@ pom.xml /.lein-* /.env *.log +/.calva/ # Links to other places: resources/public/docs/mw-*/uberdoc.html # Artefacts: mw_ui.log pom.xml + +buildall.tmp.*/ + +.nrepl-port From a44014e82f556cf6dab39aa81edbac9c8e7b9c5e Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Thu, 9 Dec 2021 20:36:01 +0000 Subject: [PATCH 6/6] Just enough fix to get this to build Does not resolve problem of listing resources in jar --- project.clj | 6 +++--- src/mw_ui/util.clj | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/project.clj b/project.clj index 2476ddb..871fe1a 100644 --- a/project.clj +++ b/project.clj @@ -9,8 +9,8 @@ "Implementation-Version" "unset" } :dependencies [[org.clojure/clojure "1.8.0"] - [mw-engine "0.1.6-SNAPSHOT"] - [mw-parser "0.1.6-SNAPSHOT"] + [mw-engine "0.1.5"] + [mw-parser "0.1.5"] [lib-noir "0.9.9"] [ring-server "0.5.0"] [selmer "1.11.3" :exclusions [cheshire @@ -23,7 +23,7 @@ [markdown-clj "1.0.1"] [environ "1.1.0"] [noir-exception "0.2.5"]] - + :main mw-ui.repl :repl-options {:init-ns mw-ui.repl} :plugins [[lein-ring "0.8.11"] [lein-environ "0.5.0"] diff --git a/src/mw_ui/util.clj b/src/mw_ui/util.clj index fa59383..b890d6b 100644 --- a/src/mw_ui/util.clj +++ b/src/mw_ui/util.clj @@ -37,8 +37,9 @@ (md/md-to-html-string))) -(defn list-resources [directory pattern] +(defn list-resources "List resource files matching `pattern` in `directory`." + [directory pattern] (let [path (str (io/resource-path) directory)] (session/put! :list-resources-path path)