|
7 | 7 | [jj.sql.async-boa :as boa] |
8 | 8 | [jj.sql.boa.query.vertx-pg :as vertx-adapter] |
9 | 9 | [jj.tassu :refer [GET POST PUT async-route]] |
10 | | - [jsonista.core :as json] |
| 10 | + [clojure.data.json :as json] |
11 | 11 | [ring-http-exchange.core :as server] |
12 | 12 | [ring-http-exchange.ssl :as ssl]) |
13 | 13 | (:import (io.vertx.core Vertx) |
|
84 | 84 |
|
85 | 85 | (defn- load-json [path] |
86 | 86 | (when (.exists (io/file path)) |
87 | | - (json/read-value (slurp path) json/keyword-keys-object-mapper))) |
| 87 | + (json/read-str (slurp path) :key-fn keyword))) |
88 | 88 |
|
89 | 89 | (defn- process-item [item ^long m] |
90 | 90 | (assoc item :total (* (:price item) (:quantity item) m))) |
|
124 | 124 | (.toByteArray baos))) |
125 | 125 |
|
126 | 126 | (defn- json-response [data] |
127 | | - {:status 200 :headers json-headers :body (json/write-value-as-string data)}) |
| 127 | + {:status 200 :headers json-headers :body (json/write-str data)}) |
128 | 128 |
|
129 | 129 | (defn- text-response [s] |
130 | 130 | {:status 200 :headers text-headers :body (str s)}) |
|
148 | 148 | :price (:price row) |
149 | 149 | :quantity (:quantity row) |
150 | 150 | :active (:active row) |
151 | | - :tags (json/read-value (str (:tags row))) |
| 151 | + :tags (json/read-str (str (:tags row))) |
152 | 152 | :rating {:score (:rating_score row) :count (:rating_count row)}}) |
153 | 153 |
|
154 | 154 | (defn- pem->keystore [^String cert-path ^String key-path] |
|
233 | 233 | params (parse-qs (:query-string req)) |
234 | 234 | m (safe-parse-long (get params param-m) 1) |
235 | 235 | items (map #(process-item % m) (subvec dataset 0 n)) |
236 | | - body-bytes (json/write-value-as-bytes |
237 | | - {:items items :count (clojure.core/count items)})] |
| 236 | + body-bytes (.getBytes (json/write-str {:items items :count (clojure.core/count items)}) "UTF-8")] |
238 | 237 | (respond |
239 | 238 | (if (accepts-gzip? (:headers req)) |
240 | 239 | {:status 200 :headers json-gzip-headers :body (gzip-bytes body-bytes)} |
|
245 | 244 | (respond (text-response (.transferTo in (OutputStream/nullOutputStream)))))) |
246 | 245 |
|
247 | 246 | (defn- handle-pg [pg-pool req respond _raise] |
248 | | - (let [params (parse-qs (:query-string req)) |
249 | | - min-p (safe-parse-double (get params param-min) 10.0) |
250 | | - max-p (safe-parse-double (get params param-max) 50.0) |
251 | | - limit (safe-parse-long (get params param-limit) 50)] |
252 | | - (pg-query pg-pool {:min min-p :max max-p :limit limit} |
253 | | - (fn [rows] |
254 | | - (let [items (map transform-pg-row rows)] |
255 | | - (respond (json-response {:items items :count (count items)})))) |
256 | | - (fn [_] |
257 | | - (respond (json-response {:items [] :count 0})))))) |
| 247 | + (if (nil? pg-pool) |
| 248 | + (respond (json-response {:items [] :count 0})) |
| 249 | + (let [params (parse-qs (:query-string req)) |
| 250 | + min-p (safe-parse-long (get params param-min) 10) |
| 251 | + max-p (safe-parse-long (get params param-max) 50) |
| 252 | + limit (safe-parse-long (get params param-limit) 50)] |
| 253 | + (try |
| 254 | + (pg-query pg-pool {:min min-p :max max-p :limit limit} |
| 255 | + (fn [rows] |
| 256 | + (try |
| 257 | + (let [items (mapv transform-pg-row rows)] |
| 258 | + (respond (json-response {:items items :count (count items)}))) |
| 259 | + (catch Throwable _ |
| 260 | + (respond (json-response {:items [] :count 0}))))) |
| 261 | + (fn [_] |
| 262 | + (respond (json-response {:items [] :count 0})))) |
| 263 | + (catch Throwable _ |
| 264 | + (respond (json-response {:items [] :count 0}))))))) |
258 | 265 |
|
259 | 266 | (defn- handle-fortunes [pg-pool respond raise] |
260 | 267 | (fortunes-query |
|
289 | 296 | :price (long (:price row)) |
290 | 297 | :quantity (long (:quantity row)) |
291 | 298 | :active (:active row) |
292 | | - :tags (json/read-value (str (:tags row))) |
| 299 | + :tags (json/read-str (str (:tags row))) |
293 | 300 | :rating {:score (long (:rating_score row)) :count (long (:rating_count row))}}) |
294 | 301 |
|
295 | 302 | (defn- handle-crud-list [pg-pool req respond raise] |
|
316 | 323 | (crud-read-query pg-pool {:id id} |
317 | 324 | (fn [rows] |
318 | 325 | (if-let [row (first rows)] |
319 | | - (let [json-str (json/write-value-as-string (transform-crud-row row))] |
| 326 | + (let [json-str (json/write-str (transform-crud-row row))] |
320 | 327 | (crud-cache-set id json-str) |
321 | 328 | (respond {:status 200 :headers crud-miss-headers :body json-str})) |
322 | 329 | (respond {:status 404 :headers json-headers :body not-found-body}))) |
323 | 330 | raise))))) |
324 | 331 |
|
325 | 332 | (defn- handle-crud-create [pg-pool req respond raise] |
326 | | - (let [body (json/read-value (:body req) json/keyword-keys-object-mapper) |
| 333 | + (let [body (json/read-str (slurp (:body req)) :key-fn keyword) |
327 | 334 | id (:id body) |
328 | 335 | nm (or (:name body) "New Product") |
329 | 336 | category (or (:category body) "test") |
|
333 | 340 | (fn [rows] |
334 | 341 | (respond {:status 201 |
335 | 342 | :headers json-headers |
336 | | - :body (json/write-value-as-string |
| 343 | + :body (json/write-str |
337 | 344 | {:id (:id (first rows)) |
338 | 345 | :name nm |
339 | 346 | :category category |
|
345 | 352 | (let [id (safe-parse-long (get-in req [:params :id]) nil)] |
346 | 353 | (if (nil? id) |
347 | 354 | (respond {:status 404 :headers json-headers :body not-found-body}) |
348 | | - (let [body (json/read-value (:body req) json/keyword-keys-object-mapper) |
| 355 | + (let [body (json/read-str (slurp (:body req)) :key-fn keyword) |
349 | 356 | nm (or (:name body) "Updated") |
350 | 357 | price (or (:price body) 0) |
351 | 358 | quantity (or (:quantity body) 0)] |
|
356 | 363 | (crud-cache-evict id) |
357 | 364 | (respond {:status 200 |
358 | 365 | :headers json-headers |
359 | | - :body (json/write-value-as-string |
| 366 | + :body (json/write-str |
360 | 367 | {:id id |
361 | 368 | :name nm |
362 | 369 | :price price |
|
0 commit comments