Not working in this form

This commit is contained in:
Simon Brooke 2020-01-28 20:06:24 +00:00
parent e4e0d6a95d
commit 77c8b01daf
18 changed files with 1323 additions and 18 deletions

View file

@ -1,13 +1,14 @@
(ns geocsv.handler
(:require
[geocsv.middleware :as middleware]
[geocsv.layout :refer [error-page]]
[geocsv.routes.home :refer [home-routes]]
[reitit.ring :as ring]
[ring.middleware.content-type :refer [wrap-content-type]]
[ring.middleware.webjars :refer [wrap-webjars]]
[geocsv.env :refer [defaults]]
[mount.core :as mount]))
(:require [compojure.core :refer [routes wrap-routes]]
[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]]
[reitit.ring :as ring]
[ring.middleware.content-type :refer [wrap-content-type]]
[ring.middleware.webjars :refer [wrap-webjars]]
[mount.core :as mount]))
(mount/defstate init-app
:start ((or (:init defaults) (fn [])))
@ -17,7 +18,10 @@
:start
(ring/ring-handler
(ring/router
[(home-routes)])
[(home-routes)
(-> #'json/json-routes
(wrap-routes middleware/wrap-csrf)
(wrap-routes middleware/wrap-formats))])
(ring/routes
(ring/create-resource-handler
{:path "/"})

View file

@ -1,10 +1,9 @@
(ns geocsv.routes.home
(:require
[geocsv.layout :as layout]
[clojure.java.io :as io]
[geocsv.middleware :as middleware]
[ring.util.response]
[ring.util.http-response :as response]))
(:require [clojure.java.io :as io]
[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"))

View file

@ -0,0 +1,91 @@
(ns geocsv.routes.json
"JSON routes for geocsv."
(:require [adl-support.core :as ac]
[adl-support.rest-support :as ar]
[clojure.core.memoize :as memo]
[clojure.java.io :as io]
[clojure.string :as s]
[clojure.tools.logging :as log]
[compojure.core :refer [defroutes GET POST]]
[csv2edn.csv2edn :refer :all]
[noir.response :as nresponse]
[noir.util.route :as route]
[ring.util.http-response :as response]
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
;;;; geocsv.routes.json: data service handlers.
;;;;
;;;; 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) 2016 Simon Brooke for Radical Independence Campaign
;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn get-pin-image-names
[request]
(ar/do-or-server-fail
(map
#(s/replace (.getName %) #"-pin\.png$" "")
(let [grammar-matcher (.getPathMatcher
(java.nio.file.FileSystems/getDefault)
"glob:*-pin.png")]
(->> "resources/public/img/map-pins"
clojure.java.io/file
file-seq
(filter #(.isFile %))
(filter #(.matches grammar-matcher (.getFileName (.toPath %)))))))
200))
(defn get-data-uri
"Return JSON formatted data taken from the CSV file at this URI. The file
must exist, be publicly readable, and use commas as separators."
[uri]
(csv->json uri))
(defn get-data-google
"Return JSON formatted data taken from the Google Sheets spreadsheet with
this `docid`. The spreadsheet must exist and must be publicly readable."
[docid]
(get-data-uri
(str
"https://docs.google.com/spreadsheets/d/"
docid
"/export?format=csv")))
(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."
[filename]
(csv->json (io/reader (io/file (str "resources/public/data/" filename)))))
(defn get-data
[request]
(ar/do-or-server-fail
(let [params (ac/massage-params request)]
(cond
(:docid params) (get-data-google (:docid params))
(:uri params) (get-data-uri (:uri params))
(:file params) (get-data-file (:file params))
:else (get-data-file "data.csv")))
200))
(defroutes json-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)))