Browse Source

Fortune tests *almost* passing (utf-8 encoding issue)

Zane Kansil 10 years ago
parent
commit
a31b3ed8bc

+ 1 - 0
frameworks/Clojure/pedestal/benchmark_config

@@ -7,6 +7,7 @@
       "plaintext_url": "/plaintext",
       "plaintext_url": "/plaintext",
       "db_url": "/db",
       "db_url": "/db",
       "query_url": "/queries?queries=",
       "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Micro",
       "classification": "Micro",

+ 2 - 1
frameworks/Clojure/pedestal/project.clj

@@ -13,7 +13,8 @@
                  [org.clojure/data.json "0.2.5"]
                  [org.clojure/data.json "0.2.5"]
                  [org.clojure/java.jdbc "0.3.6"]
                  [org.clojure/java.jdbc "0.3.6"]
                  [korma "0.4.0"]
                  [korma "0.4.0"]
-                 [mysql/mysql-connector-java "5.1.6"]]
+                 [mysql/mysql-connector-java "5.1.6"]
+                 [hiccup "1.0.4"]]
   :min-lein-version "2.0.0"
   :min-lein-version "2.0.0"
   :resource-paths ["config", "resources"]
   :resource-paths ["config", "resources"]
   :profiles {:dev {:aliases {"run-dev" ["trampoline" "run" "-m" "pedestal.server/run-dev"]}
   :profiles {:dev {:aliases {"run-dev" ["trampoline" "run" "-m" "pedestal.server/run-dev"]}

+ 49 - 2
frameworks/Clojure/pedestal/src/pedestal/service.clj

@@ -1,7 +1,10 @@
 (ns pedestal.service
 (ns pedestal.service
   (:import com.mchange.v2.c3p0.ComboPooledDataSource)
   (:import com.mchange.v2.c3p0.ComboPooledDataSource)
   (:use korma.db
   (:use korma.db
-        korma.core)
+        korma.core
+        hiccup.core
+        hiccup.util
+        hiccup.page)
   (:require [io.pedestal.http :as bootstrap]
   (:require [io.pedestal.http :as bootstrap]
             [io.pedestal.http.route :as route]
             [io.pedestal.http.route :as route]
             [io.pedestal.http.body-params :as body-params]
             [io.pedestal.http.body-params :as body-params]
@@ -72,6 +75,49 @@
           (> n 500) 500
           (> n 500) 500
           :else n)))))
           :else n)))))
 
 
+; Set up entity Fortune and the database representation
+(defentity fortune
+  (pk :id)
+  (table :fortune)
+  (entity-fields :id :message)
+  (database mysql-db))
+
+(defn get-all-fortunes []
+  "Query all Fortune records from the database."
+    (select fortune
+            (fields :id :message)))
+
+(defn get-fortunes []
+  "Fetch the full list of Fortunes from the database, sort them by the fortune
+message text, and then return the results."
+    (sort-by #(:message %)
+      (conj
+        (get-all-fortunes)
+        { :id 0 :message "Additional fortune added at request time." })))
+
+(defn fortunes-hiccup [fortunes]
+  "Render the given fortunes to simple HTML using Hiccup."
+  (html5
+   [:head
+    [:title "Fortunes"]]
+   [:body
+    [:table
+     [:tr
+      [:th "id"]
+      [:th "message"]]
+     (for [x fortunes]
+       [:tr
+        [:td (:id x)]
+        [:td (escape-html (:message x))]])
+     ]]))
+
+(defn fortune-test [request]
+  (->
+    (get-fortunes)
+    (fortunes-hiccup)
+    (ring-resp/response)
+    (ring-resp/content-type "text/html")
+    (ring-resp/charset "utf-8")))         ;; Apply charset after content type
 
 
 ;; All of the available routes
 ;; All of the available routes
 (defroutes routes
 (defroutes routes
@@ -79,7 +125,8 @@
   [  "/json"      {:get json-serialization}]
   [  "/json"      {:get json-serialization}]
   [  "/plaintext" {:get plaintext}]
   [  "/plaintext" {:get plaintext}]
   [  "/db"        {:get single-query-test}]
   [  "/db"        {:get single-query-test}]
-  [  "/queries"   {:get multiple-query-test}]]])
+  [  "/queries"   {:get multiple-query-test}]
+  [  "/fortunes"  {:get fortune-test}]]])
 
 
 ;; How the server will look, not the code to start it up
 ;; How the server will look, not the code to start it up
 (def service {:env :prod
 (def service {:env :prod