|
@@ -19,7 +19,9 @@
|
|
|
;; Query a random World record from the database
|
|
|
(defn get-world []
|
|
|
(let [id (inc (rand-int 9999))] ; Num between 1 and 10,000
|
|
|
- (db/query "select id randomNumber from world where id = ?" id)))
|
|
|
+ ; Set a naming strategy to preserve column name case
|
|
|
+ (clojure.java.jdbc/with-naming-strategy {:keyword identity}
|
|
|
+ (db/query "select * from world where id = ?" id))))
|
|
|
|
|
|
;; Run the specified number of queries, return the results
|
|
|
(defn run-queries [queries]
|
|
@@ -28,6 +30,16 @@
|
|
|
queries ; Number of queries to run
|
|
|
(repeatedly get-world))))
|
|
|
|
|
|
+(defn get-query-count [queries]
|
|
|
+ "Parse provided string value of query count, clamping values to between 1 and 500."
|
|
|
+ (let [q (try (Integer/parseInt queries)
|
|
|
+ (catch Exception e 1))] ; default to 1 on parse failure
|
|
|
+ (if (> q 500)
|
|
|
+ 500 ; clamp to 500 max
|
|
|
+ (if (< q 1)
|
|
|
+ 1 ; clamp to 1 min
|
|
|
+ q)))) ; otherwise use provided value
|
|
|
+
|
|
|
;; Define route handlers
|
|
|
(defroutes app-routes
|
|
|
(GET "/http-kit/" [] "Hello, World!")
|
|
@@ -35,7 +47,7 @@
|
|
|
(GET "/http-kit/db" []
|
|
|
(response (first (run-queries 1))))
|
|
|
(GET "/http-kit/db/:queries" [queries]
|
|
|
- (response (run-queries (Integer/parseInt queries))))
|
|
|
+ (response (run-queries (get-query-count queries))))
|
|
|
(route/not-found "Not Found"))
|
|
|
|
|
|
|