From ff6af074f8ac9daa0ce3100171e9e1d04a08b3ba Mon Sep 17 00:00:00 2001 From: Bart Akeley Date: Sun, 29 Oct 2017 11:46:36 -0500 Subject: [PATCH] move db config out of code --- .gitignore | 1 + profiles.clj | 6 +++ project.clj | 30 +++++++------ src/aretherecookies/db.clj | 64 ++++++++++++++++++++++++++ src/aretherecookies/handler.clj | 80 ++++++--------------------------- 5 files changed, 101 insertions(+), 80 deletions(-) create mode 100644 profiles.clj create mode 100644 src/aretherecookies/db.clj diff --git a/.gitignore b/.gitignore index 22d6a48..fe56ed9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ pom.xml.asc *.class /.lein-* /.nrepl-port +profiles.clj \ No newline at end of file diff --git a/profiles.clj b/profiles.clj new file mode 100644 index 0000000..92972df --- /dev/null +++ b/profiles.clj @@ -0,0 +1,6 @@ +{:dev {:env { + :database-url "postgresql://localhost:5432" + :datbase-user "someuser" + :database-password "somepassword" + :database-name "whateveryounamedit" +}}} \ No newline at end of file diff --git a/project.clj b/project.clj index affadaa..e49cd99 100644 --- a/project.clj +++ b/project.clj @@ -1,18 +1,22 @@ (defproject aretherecookies "0.1.0-SNAPSHOT" - :description "FIXME: write description" - :url "http://example.com/FIXME" + :description "where cookies can be found" + :url "http://www.aretherecookies.com" :min-lein-version "2.0.0" - :dependencies [[org.clojure/clojure "1.8.0"] - [compojure "1.5.1"] - [ring/ring-defaults "0.2.1"] - [org.clojure/java.jdbc "0.7.3"] - [org.postgresql/postgresql "42.1.4.jre6"] - [com.mchange/c3p0 "0.9.5.2"] - [ring-middleware-format "0.7.2"] - [org.clojure/data.json "0.2.6"] - [ring/ring-json "0.4.0"]] - :plugins [[lein-ring "0.9.7"]] + :dependencies [ + [org.clojure/clojure "1.8.0"] + [environ "1.1.0"] + [compojure "1.5.1"] + [ring/ring-defaults "0.2.1"] + [org.clojure/java.jdbc "0.7.3"] + [org.postgresql/postgresql "42.1.4.jre6"] + [com.mchange/c3p0 "0.9.5.2"] + [ring-middleware-format "0.7.2"] + [org.clojure/data.json "0.2.6"] + [ring/ring-json "0.4.0"] + ] + :plugins [[lein-ring "0.9.7"] [lein-environ "1.1.0"]] :ring {:handler aretherecookies.handler/app} :profiles {:dev {:dependencies [[javax.servlet/servlet-api "2.5"] - [ring/ring-mock "0.3.0"]]}}) + [ring/ring-mock "0.3.0"]]}} + :uberjar-name "aretherecookies.jar") diff --git a/src/aretherecookies/db.clj b/src/aretherecookies/db.clj new file mode 100644 index 0000000..18c0e1a --- /dev/null +++ b/src/aretherecookies/db.clj @@ -0,0 +1,64 @@ +(ns aretherecookies.db + (:require + [clojure.java.jdbc :as jdbc] + [environ.core :refer [env]] + [clojure.string :as str]) + (:import com.mchange.v2.c3p0.ComboPooledDataSource)) + +(def db-spec {:dbtype "postgresql" + :db (env :database-name) + :url (env :database-url) + :user (env :database-user) + :password (env :database-password)}) + +(defn pool + [spec] + (let [cpds (doto (ComboPooledDataSource.) + (.setDriverClass (:classname spec)) + (.setJdbcUrl (str "jdbc:" (:url db-spec) "/" (:db db-spec))) + (.setUser (:user spec)) + (.setPassword (:password spec)) + ;; expire excess connections after 30 minutes of inactivity: + (.setMaxIdleTimeExcessConnections (* 30 60)) + ;; expire connections after 3 hours of inactivity: + (.setMaxIdleTime (* 3 60 60)))] + {:datasource cpds})) + +(def pooled-db (delay (pool db-spec))) + +(def location-query " +SELECT + id, + name, + place_id, + category, + images, + thumbImage, + ST_AsGeoJSON(loc) as location, + ST_Distance( + loc, + @LOCATION@ + ) / 1609 as distance +FROM food_items +WHERE + ST_DWithin( + loc, + @LOCATION@, + @DISTANCE@ * 1609 + ) +ORDER BY distance ASC;") + +(defn point-text [lng lat] + (-> + "ST_GeogFromText('SRID=4326;POINT(@LONGITUDE@ @LATITUDE@)')" + (str/replace "@LONGITUDE@" (str lng)) + (str/replace "@LATITUDE@" (str lat)))) + +(defn build-fooditems-query [lng lat dist] + (-> + location-query + (str/replace "@LOCATION@" (point-text (str lng) (str lat))) + (str/replace "@DISTANCE@" (str dist)))) + +(defn query-food-items [query] + (jdbc/query @pooled-db [query])) \ No newline at end of file diff --git a/src/aretherecookies/handler.clj b/src/aretherecookies/handler.clj index 7174602..0239b78 100644 --- a/src/aretherecookies/handler.clj +++ b/src/aretherecookies/handler.clj @@ -1,68 +1,12 @@ (ns aretherecookies.handler - (:require [compojure.core :refer :all] + (:require [aretherecookies.db :refer [query-food-items build-fooditems-query]] + [compojure.core :refer :all] [compojure.route :as route] [ring.middleware.defaults :refer [wrap-defaults site-defaults]] - [clojure.java.jdbc :as jdbc] [clojure.data.json :as json] [clojure.string :as str] [ring.middleware.anti-forgery :refer :all] - [ring.middleware.json :refer [wrap-json-body]]) - (:import com.mchange.v2.c3p0.ComboPooledDataSource)) - -(def db-spec {:dbtype "postgresql" - :subprotocol "postgresql" - :subname "//localhost:5432/test1" - :user "bartronx7" - :password "abc123"}) - -(defn pool - [spec] - (let [cpds (doto (ComboPooledDataSource.) - (.setDriverClass (:classname spec)) - (.setJdbcUrl (str "jdbc:" (:subprotocol spec) ":" (:subname spec))) - (.setUser (:user spec)) - (.setPassword (:password spec)) - ;; expire excess connections after 30 minutes of inactivity: - (.setMaxIdleTimeExcessConnections (* 30 60)) - ;; expire connections after 3 hours of inactivity: - (.setMaxIdleTime (* 3 60 60)))] - {:datasource cpds})) - -(def pooled-db (delay (pool db-spec))) - -(def location-query " - SELECT - id, - name, - place_id, - category, - images, - thumbImage, - ST_AsGeoJSON(loc) as location, - ST_Distance( - loc, - @LOCATION@ - ) / 1609 as distance - FROM food_items - WHERE - ST_DWithin( - loc, - @LOCATION@, - @DISTANCE@ * 1609 - ) - ORDER BY distance ASC;") - -(defn point-text [lng lat] - (-> - "ST_GeogFromText('SRID=4326;POINT(@LONGITUDE@ @LATITUDE@)')" - (str/replace "@LONGITUDE@" (str lng)) - (str/replace "@LATITUDE@" (str lat)))) - -(defn build-fooditems-query [lng lat dist] - (-> - location-query - (str/replace "@LOCATION@" (point-text (str lng) (str lat))) - (str/replace "@DISTANCE@" (str dist)))) + [ring.middleware.json :refer [wrap-json-body]])) (defn uuid-to-string [key value] (if (instance? java.util.UUID value) (.toString value) value)) @@ -91,14 +35,16 @@ (defn food-items-to-json [query-result] (map parse-location query-result)) -(defn food-items-handler [{{lat "lat" lng "lng" filter "filter" orderby "orderby"} :body}] - (json/write-str - (hash-map - :orderby orderby - :filter filter - :fooditems (food-items-to-json - (jdbc/query @pooled-db [(build-fooditems-query lng lat (get filter "radius"))]))) - :value-fn uuid-to-string)) +(defn food-items-handler [req] + (println "req---->" (:body req)) + (let [{{lat "lat" lng "lng" filter "filter" orderby "orderby"} :body} req] + (json/write-str + (hash-map + :orderby orderby + :filter filter + :fooditems (food-items-to-json + (query-food-items (build-fooditems-query lng lat (get filter "radius"))))) + :value-fn uuid-to-string))) (defroutes app-routes (GET "/" [] (str {:csrf-token