66 [jj.sql.boa.query.next-jdbc :refer [->NextJdbcAdapter]]
77 [jj.tassu :refer [GET POST PUT route]]
88 [jsonista.core :as json]
9- [next.jdbc :as jdbc]
109 [ring-http-exchange.core :as server]
1110 [ring-http-exchange.ssl :as ssl])
1211 (:import (com.zaxxer.hikari HikariConfig HikariDataSource)
3231(def ^:private ^:const dot " ." )
3332(def ^:private ^:const not-found-body " Not found" )
3433(def ^:private ^:const dataset-path " /data/dataset.json" )
35- (def ^:private ^:const db-path " /data/benchmark.db" )
3634(def ^:private ^:const static-dir " /data/static" )
3735(def ^:private ^:const param-min " min" )
3836(def ^:private ^:const param-max " max" )
5048(def ^:private json-gzip-headers {hdr-ct ct-json hdr-ce enc-gzip hdr-server server-name})
5149(def ^:private text-headers {hdr-ct ct-text hdr-server server-name})
5250
53- (def ^:private sqlite-query (boa/build-query (->NextJdbcAdapter ) " sql/db-query" ))
5451(def ^:private pg-query (boa/build-query (->NextJdbcAdapter ) " sql/pg-query" ))
5552(def ^:private crud-list-query (boa/build-query (->NextJdbcAdapter ) " sql/crud-list" ))
5653(def ^:private crud-read-query (boa/build-query (->NextJdbcAdapter ) " sql/crud-read" ))
136133 ext (if (>= dot-index 0 ) (subs name dot-index) " " )]
137134 (get extension-map ext ct-octet)))
138135
139- (defn- transform-sqlite-row [row]
140- {:id (:id row)
141- :name (:name row)
142- :category (:category row)
143- :price (:price row)
144- :quantity (:quantity row)
145- :active (== 1 (long (:active row)))
146- :tags (json/read-value (:tags row) json/keyword-keys-object-mapper)
147- :rating {:score (:rating_score row) :count (:rating_count row)}})
148-
149136(defn- transform-pg-row [row]
150137 {:id (:id row)
151138 :name (:name row)
196183 (println (str " Failed to start server on port " port
197184 " : " (.getMessage e))))))))
198185
199- (defn- init-sqlite []
200- (when (.exists (io/file db-path))
201- (jdbc/get-datasource {:dbtype " sqlite" :dbname db-path :read-only true })))
202-
203186(defn- init-postgres []
204187 (when-let [url (System/getenv " DATABASE_URL" )]
205188 (try
242225 (with-open [^InputStream in (:body req)]
243226 (text-response (.transferTo in (OutputStream/nullOutputStream )))))
244227
245- (defn- query-sqlite-items [ds params]
246- (try (mapv transform-sqlite-row (sqlite-query ds params))
247- (catch Exception _ [])))
248-
249228(defn- query-pg-items [ds params]
250229 (try (mapv transform-pg-row (pg-query ds params))
251230 (catch Exception _ [])))
252231
253- (defn- handle-sqlite [ds req]
254- (let [params (parse-qs (:query-string req))
255- min-p (safe-parse-double (get params param-min) 10.0 )
256- max-p (safe-parse-double (get params param-max) 50.0 )
257- limit (safe-parse-long (get params param-limit) 50 )
258- items (query-sqlite-items ds {:min min-p :max max-p :limit limit})]
259- (json-response {:items items :count (clojure.core/count items)})))
260-
261232(defn- handle-pg [ds req]
262233 (let [params (parse-qs (:query-string req))
263234 min-p (safe-parse-double (get params param-min) 10.0 )
353324 :body f}
354325 {:status 404 :body not-found-body})))
355326
356- (defn- build-handler [{:keys [dataset sqlite-ds pg-ds]}]
327+ (defn- build-handler [{:keys [dataset pg-ds]}]
357328 (route
358329 {" /baseline11" [(GET handle-baseline-get)
359330 (POST handle-baseline-post)]
360331 " /json/:count" [(GET (fn [req] (handle-json dataset req)))]
361332 " /upload" [(POST handle-upload)]
362- " /db" [(GET (fn [req] (handle-sqlite sqlite-ds req)))]
363333 " /async-db" [(GET (fn [req] (handle-pg pg-ds req)))]
364334 " /crud/items" [(GET (fn [req] (handle-crud-list pg-ds req)))
365335 (POST (fn [req] (handle-crud-create pg-ds req)))]
371341(defn -main [& _]
372342 (let [dataset (load-json (or (System/getenv " DATASET_PATH" ) dataset-path))
373343 handler (build-handler {:dataset dataset
374- :sqlite-ds (init-sqlite )
375344 :pg-ds (init-postgres )})]
376345 (start-server! handler plain-port)
377346 (start-server! handler tls-port (load-ssl-context ))))
0 commit comments