Now successfully remote-loading data from Google sheets

This commit is contained in:
Simon Brooke 2020-01-28 22:32:49 +00:00
parent 08f9c2f201
commit a5881c3b97
10 changed files with 273 additions and 87 deletions

View file

@ -1,10 +1,11 @@
(ns geocsv.handler
(:require [compojure.core :refer [routes wrap-routes]]
[compojure.route :as route]
[geocsv.env :refer [defaults]]
[geocsv.middleware :as middleware]
[geocsv.layout :refer [error-page]]
[geocsv.routes.home :refer [home-routes]]
[geocsv.routes.json :refer [json-routes]]
[geocsv.routes.rest :refer [rest-routes]]
[reitit.ring :as ring]
[ring.middleware.content-type :refer [wrap-content-type]]
[ring.middleware.webjars :refer [wrap-webjars]]
@ -16,24 +17,42 @@
(mount/defstate app-routes
:start
(ring/ring-handler
(ring/router
[(home-routes)
(-> #'json/json-routes
(wrap-routes middleware/wrap-csrf)
(wrap-routes middleware/wrap-formats))])
(ring/routes
(ring/create-resource-handler
{:path "/"})
(wrap-content-type
(wrap-webjars (constantly nil)))
(ring/create-default-handler
{:not-found
(constantly (error-page {:status 404, :title "404 - Page not found"}))
:method-not-allowed
(constantly (error-page {:status 405, :title "405 - Not allowed"}))
:not-acceptable
(constantly (error-page {:status 406, :title "406 - Not acceptable"}))}))))
;; This is an older way of doing routing and Dmitri Sotnikov now does it
;; another way which is almost certainly better but I can't make it work.
(routes
(-> #'home-routes
(wrap-routes middleware/wrap-csrf)
(wrap-routes middleware/wrap-formats))
(-> #'rest-routes
(wrap-routes middleware/wrap-csrf)
(wrap-routes middleware/wrap-formats))
(route/resources "/")
(route/not-found
(:body
(error-page {:status 404
:title "Page not found"
:message "The page you requested has not yet been implemented"})))))
;; (ring/ring-handler
;; (ring/router
;; [(home-routes)
;; ;; (-> rest-routes
;; ;; (wrap-routes middleware/wrap-csrf)
;; ;; (wrap-routes middleware/wrap-formats))
;; ])
;; (ring/routes
;; (ring/create-resource-handler
;; {:path "/"})
;; (wrap-content-type
;; (wrap-webjars (constantly nil)))
;; (ring/create-default-handler
;; {:not-found
;; (constantly (error-page {:status 404, :title "404 - Page not found"}))
;; :method-not-allowed
;; (constantly (error-page {:status 405, :title "405 - Not allowed"}))
;; :not-acceptable
;; (constantly (error-page {:status 406, :title "406 - Not acceptable"}))}))))
(defn app []
(middleware/wrap-base #'app-routes))

View file

@ -1,19 +1,29 @@
(ns geocsv.routes.home
(:require [clojure.java.io :as io]
[compojure.core :refer [defroutes GET POST]]
[geocsv.layout :as layout]
[geocsv.middleware :as middleware]
[ring.util.response]
[ring.util.http-response :as response]))
(defn home-page [request]
(layout/render request "home.html"))
"Serve the home page, in the process merging any parameters passed
in the request into the session."
(assoc
(layout/render request "home.html")
:session
(merge
(:session request)
(:params request))))
(defn home-routes []
[""
{:middleware [middleware/wrap-csrf
middleware/wrap-formats]}
["/" {:get home-page}]
["/docs" {:get (fn [_]
(-> (response/ok (-> "docs/docs.md" io/resource slurp))
(response/header "Content-Type" "text/plain; charset=utf-8")))}]])
(defroutes home-routes
(GET "/" request (home-page request))
(GET "/docs" _ (fn [_]
(->
(response/ok
(->
"docs/docs.md"
io/resource
slurp))
(response/header "Content-Type" "text/plain; charset=utf-8")))))

View file

@ -1,5 +1,5 @@
(ns geocsv.routes.json
"JSON routes for geocsv."
(ns geocsv.routes.rest
"REST routes for geocsv."
(:require [adl-support.core :as ac]
[adl-support.rest-support :as ar]
[clojure.core.memoize :as memo]
@ -44,8 +44,9 @@
(let [grammar-matcher (.getPathMatcher
(java.nio.file.FileSystems/getDefault)
"glob:*-pin.png")]
(->> "resources/public/img/map-pins"
clojure.java.io/file
(->> "public/img/map-pins"
io/resource
io/file
file-seq
(filter #(.isFile %))
(filter #(.matches grammar-matcher (.getFileName (.toPath %)))))))
@ -69,15 +70,20 @@
(defn get-data-file
"Return JSON formatted data taken from the CSV file with the name `filename`
in the directory `resources/public/data`. TODO: There is a safe way to
access the content of the resource directory but I don't recall it just now."
in the directory `resources/public/data`."
[filename]
(csv->json (io/reader (io/file (str "resources/public/data/" filename)))))
(-> (str "public/data/" filename) io/resource io/file io/reader csv->json))
(defn get-data
"Return JSON formatted data from the source implied by this `request`."
[request]
(ar/do-or-server-fail
(let [params (ac/massage-params request)]
;; We're merging the parameters from the request with the key/value
;; pairs already in the session, so that parame put into the session
;; by calls to the home page can be used here.
(let [params (merge
(:session request)
(ac/massage-params request))]
(cond
(:docid params) (get-data-google (:docid params))
(:uri params) (get-data-uri (:uri params))
@ -85,7 +91,8 @@
:else (get-data-file "data.csv")))
200))
(defroutes json-routes
(defroutes rest-routes
(GET "/get-pin-image-names" request (get-pin-image-names request))
(POST "/get-pin-image-names" request (get-pin-image-names request))
(GET "/get-data" request (get-data request)))
(GET "/get-data" request (get-data request))
(POST "/get-data" request (get-data request)))

View file

@ -60,7 +60,7 @@
:fetch-data
(fn [{db :db} _]
(let [uri (assoc source-host
:path "/data/data.json")]
:path "/get-data")]
(js/console.log
(str
"Fetching data: " uri))

View file

@ -91,7 +91,7 @@
(defn map-render
"Render the actual div containing the map."
[]
[:div#map {:style {:height "500px"}}])
[:div#map {:style {:height "1000px"}}])
(defn panel
"A reagent class for the map object."