|
@@ -11,8 +11,7 @@
|
|
(:require [compojure.handler :as handler]
|
|
(:require [compojure.handler :as handler]
|
|
[compojure.route :as route]
|
|
[compojure.route :as route]
|
|
[clojure.java.jdbc :as jdbc]
|
|
[clojure.java.jdbc :as jdbc]
|
|
- [clojure.java.jdbc.sql :as sql]
|
|
|
|
- [net.cgrand.enlive-html :as html]))
|
|
|
|
|
|
+ [clojure.java.jdbc.sql :as sql]))
|
|
|
|
|
|
; Database connection
|
|
; Database connection
|
|
(defdb db (mysql {:subname "//localhost:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true"
|
|
(defdb db (mysql {:subname "//localhost:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true"
|
|
@@ -39,11 +38,10 @@
|
|
|
|
|
|
; Run the specified number of queries, return the results
|
|
; Run the specified number of queries, return the results
|
|
(defn run-queries [queries]
|
|
(defn run-queries [queries]
|
|
- (vec ; Return as a vector
|
|
|
|
(flatten ; Make it a list of maps
|
|
(flatten ; Make it a list of maps
|
|
(take
|
|
(take
|
|
queries ; Number of queries to run
|
|
queries ; Number of queries to run
|
|
- (repeatedly get-world)))))
|
|
|
|
|
|
+ (repeatedly get-world))))
|
|
|
|
|
|
; Database connection for java.jdbc "raw"
|
|
; Database connection for java.jdbc "raw"
|
|
; https://github.com/clojure/java.jdbc/blob/master/doc/clojure/java/jdbc/ConnectionPooling.md
|
|
; https://github.com/clojure/java.jdbc/blob/master/doc/clojure/java/jdbc/ConnectionPooling.md
|
|
@@ -80,11 +78,10 @@
|
|
|
|
|
|
; Run the specified number of queries, return the results
|
|
; Run the specified number of queries, return the results
|
|
(defn run-queries-raw [queries]
|
|
(defn run-queries-raw [queries]
|
|
- (vec ; Return as a vector
|
|
|
|
(flatten ; Make it a list of maps
|
|
(flatten ; Make it a list of maps
|
|
(take
|
|
(take
|
|
queries ; Number of queries to run
|
|
queries ; Number of queries to run
|
|
- (repeatedly get-world-raw)))))
|
|
|
|
|
|
+ (repeatedly get-world-raw))))
|
|
|
|
|
|
(defn get-query-count [queries]
|
|
(defn get-query-count [queries]
|
|
"Parse provided string value of query count, clamping values to between 1 and 500."
|
|
"Parse provided string value of query count, clamping values to between 1 and 500."
|
|
@@ -115,31 +112,21 @@ message text, and then return the results."
|
|
(let [fortunes (conj (get-all-fortunes) {:id 0 :message "Additional fortune added at request time."} )]
|
|
(let [fortunes (conj (get-all-fortunes) {:id 0 :message "Additional fortune added at request time."} )]
|
|
(sort-by #(:message %) fortunes)))
|
|
(sort-by #(:message %) fortunes)))
|
|
|
|
|
|
-(defn escape-html-local
|
|
|
|
- "Change special characters into HTML character entities."
|
|
|
|
- [text]
|
|
|
|
- (.. ^String (as-str text)
|
|
|
|
- (replace "&" "&")
|
|
|
|
- (replace "<" "<")
|
|
|
|
- (replace ">" ">")
|
|
|
|
- (replace "\"" """)
|
|
|
|
- (replace "'" "'")))
|
|
|
|
-
|
|
|
|
(defn fortunes-hiccup [fortunes]
|
|
(defn fortunes-hiccup [fortunes]
|
|
"Render the given fortunes to simple HTML using Hiccup."
|
|
"Render the given fortunes to simple HTML using Hiccup."
|
|
(html5
|
|
(html5
|
|
- [:head
|
|
|
|
- [:title "Fortunes"]]
|
|
|
|
- [:body
|
|
|
|
- [:table
|
|
|
|
- [:tr
|
|
|
|
- [:th "id"]
|
|
|
|
- [:th "message"]]
|
|
|
|
- (for [x fortunes]
|
|
|
|
- [:tr
|
|
|
|
- [:td (:id x)]
|
|
|
|
- [:td (escape-html-local (:message x))]])
|
|
|
|
- ]]))
|
|
|
|
|
|
+ [:head
|
|
|
|
+ [:title "Fortunes"]]
|
|
|
|
+ [:body
|
|
|
|
+ [:table
|
|
|
|
+ [:tr
|
|
|
|
+ [:th "id"]
|
|
|
|
+ [:th "message"]]
|
|
|
|
+ (for [x fortunes]
|
|
|
|
+ [:tr
|
|
|
|
+ [:td (:id x)]
|
|
|
|
+ [:td (escape-html (:message x))]])
|
|
|
|
+ ]]))
|
|
|
|
|
|
(defn fortunes-enlive [fortunes]
|
|
(defn fortunes-enlive [fortunes]
|
|
"Render the given fortunes to simple HTML using Enlive."
|
|
"Render the given fortunes to simple HTML using Enlive."
|
|
@@ -153,7 +140,9 @@ message text, and then return the results."
|
|
:headers {"Content-Type" "text/plain; charset=utf-8"}
|
|
:headers {"Content-Type" "text/plain; charset=utf-8"}
|
|
:body "Hello, World!"})
|
|
:body "Hello, World!"})
|
|
(GET "/json" [] (response {:message "Hello, World!"}))
|
|
(GET "/json" [] (response {:message "Hello, World!"}))
|
|
|
|
+ (GET "/db" [] (response (first (run-queries (get-query-count 1)))))
|
|
(GET "/db/:queries" [queries] (response (run-queries (get-query-count queries))))
|
|
(GET "/db/:queries" [queries] (response (run-queries (get-query-count queries))))
|
|
|
|
+ (GET "/dbraw" [] (response (first (run-queries-raw (get-query-count 1)))))
|
|
(GET "/dbraw/:queries" [queries] (response (run-queries-raw (get-query-count queries))))
|
|
(GET "/dbraw/:queries" [queries] (response (run-queries-raw (get-query-count queries))))
|
|
(GET "/fortune" [] (response (get-fortunes)))
|
|
(GET "/fortune" [] (response (get-fortunes)))
|
|
(GET "/fortune-hiccup" [] (fortunes-hiccup (get-fortunes)))
|
|
(GET "/fortune-hiccup" [] (fortunes-hiccup (get-fortunes)))
|