mirror of
https://gitlab.com/wheres-the-tp/server.git
synced 2026-01-25 04:24:56 -06:00
move db config out of code
This commit is contained in:
parent
f30715d23a
commit
ff6af074f8
5 changed files with 101 additions and 80 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -8,3 +8,4 @@ pom.xml.asc
|
|||
*.class
|
||||
/.lein-*
|
||||
/.nrepl-port
|
||||
profiles.clj
|
||||
6
profiles.clj
Normal file
6
profiles.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{:dev {:env {
|
||||
:database-url "postgresql://localhost:5432"
|
||||
:datbase-user "someuser"
|
||||
:database-password "somepassword"
|
||||
:database-name "whateveryounamedit"
|
||||
}}}
|
||||
30
project.clj
30
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")
|
||||
|
|
|
|||
64
src/aretherecookies/db.clj
Normal file
64
src/aretherecookies/db.clj
Normal file
|
|
@ -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]))
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue