Skip to content

Commit 0805486

Browse files
authored
Merge pull request #682 from ruroru/main
Add fortunes for aleph
2 parents 9e164ab + 06cd70e commit 0805486

4 files changed

Lines changed: 45 additions & 0 deletions

File tree

frameworks/aleph/meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"sync-db",
2323
"static",
2424
"tcp-frag",
25+
"fortunes",
2526
"crud"
2627
]
2728
}

frameworks/aleph/project.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
io.netty/netty-transport-native-unix-common]]
2626
[io.netty/netty-all "4.2.12.Final"]
2727
[org.clojars.jj/tassu "1.0.4"]
28+
[hiccup "2.0.0"]
2829
[org.clojars.jj/boa-sql "1.0.10"]
2930
[org.clojars.jj/async-boa-sql "1.0.10"]
3031
[org.clojars.jj/next-jdbc-adapter "1.0.10"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT id, message FROM fortune

frameworks/aleph/src/aleph_bench/core.clj

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[aleph.netty :as netty]
44
[clojure.core.cache :as cache]
55
[clojure.java.io :as io]
6+
[hiccup2.core :as h]
67
[clojure.string :as str]
78
[jj.sql.async-boa :as async-boa]
89
[jj.sql.boa.query.vertx-pg :as vertx-adapter]
@@ -23,13 +24,16 @@
2324

2425
(def ^:private ^:const ct-json "application/json")
2526
(def ^:private ^:const ct-text "text/plain")
27+
(def ^:private ^:const ct-html "text/html; charset=utf-8")
2628
(def ^:private ^:const ct-octet "application/octet-stream")
2729
(def ^:private ^:const hdr-ct "Content-Type")
2830
(def ^:private ^:const hdr-server "Server")
2931
(def ^:private ^:const server-name "aleph")
3032
(def ^:private ^:const dot ".")
3133
(def ^:private ^:const not-found-body "Not found")
3234
(def ^:private ^:const empty-db-body "{\"items\":[],\"count\":0}")
35+
(def ^:private ^:const fortunes-error-body
36+
"<!DOCTYPE html><html><body>db error</body></html>")
3337
(def ^:private ^:const dataset-path "/data/dataset.json")
3438
(def ^:private ^:const dataset-large-path "/data/dataset-large.json")
3539
(def ^:private ^:const param-min "min")
@@ -45,10 +49,28 @@
4549

4650
(def ^:private json-headers {hdr-ct ct-json hdr-server server-name})
4751
(def ^:private text-headers {hdr-ct ct-text hdr-server server-name})
52+
(def ^:private html-headers {hdr-ct ct-html hdr-server server-name})
4853
(def ^:private crud-hit-headers {hdr-ct ct-json hdr-server server-name "X-Cache" "HIT"})
4954
(def ^:private crud-miss-headers {hdr-ct ct-json hdr-server server-name "X-Cache" "MISS"})
5055
(def ^:private empty-db-response {:status 200 :headers json-headers :body empty-db-body})
5156

57+
(def ^:private runtime-fortune
58+
{:id 0 :message "Additional fortune added at request time."})
59+
60+
(defn render-fortunes
61+
^String [fortunes] (str
62+
(h/html {:mode :html}
63+
(h/raw "<!DOCTYPE html>")
64+
[:html
65+
[:head [:title "Fortunes"]]
66+
[:body
67+
[:table
68+
[:tr [:th "id"] [:th "message"]]
69+
(for [f fortunes]
70+
[:tr
71+
[:td (:id f)]
72+
[:td (:message f)]])]]])))
73+
5274
(def ^:private ^:const extension-map
5375
{".css" "text/css" ".js" "application/javascript" ".html" "text/html"
5476
".woff2" "font/woff2" ".svg" "image/svg+xml" ".webp" "image/webp" ".json" ct-json})
@@ -150,6 +172,7 @@
150172
(def ^:private crud-read-q (async-boa/build-async-query adapter "sql/crud-read"))
151173
(def ^:private crud-create-q (async-boa/build-async-query adapter "sql/crud-create"))
152174
(def ^:private crud-update-q (async-boa/build-async-query adapter "sql/crud-update"))
175+
(def ^:private fortunes-q (async-boa/build-async-query adapter "sql/fortunes"))
153176

154177
(defn- build-ssl-context []
155178
(let [cert-path (or (System/getenv "TLS_CERT") tls-cert-default)
@@ -294,6 +317,24 @@
294317
(fn [_] (d/success! dfd {:status 404 :headers json-headers :body not-found-body})))
295318
dfd))))))
296319

320+
(defn- handle-fortunes [pg-pool _req]
321+
(let [dfd (d/deferred)]
322+
(fortunes-q
323+
pg-pool {}
324+
(fn [rows]
325+
(let [base (mapv (fn [r] {:id (:id r) :message (:message r)}) rows)
326+
all (conj base runtime-fortune)
327+
sorted (sort-by :message all)
328+
body (render-fortunes sorted)]
329+
(d/success! dfd {:status 200
330+
:headers html-headers
331+
:body body})))
332+
(fn [_]
333+
(d/success! dfd {:status 500
334+
:headers html-headers
335+
:body fortunes-error-body})))
336+
dfd))
337+
297338
(defn- handle-static [req]
298339
(let [name (get-in req [:params :filename])
299340
path (str "/data" (:uri req))
@@ -315,6 +356,7 @@
315356
(POST (fn [req] (handle-crud-create pg-pool req)))]
316357
"/crud/items/:id" [(GET (fn [req] (handle-crud-read pg-pool req)))
317358
(PUT (fn [req] (handle-crud-update pg-pool req)))]
359+
"/fortunes" [(GET (fn [req] (handle-fortunes pg-pool req)))]
318360
"/static/:filename" [(GET handle-static)]
319361
"/" [(GET (fn [_] (text-response server-name)))]}))
320362

0 commit comments

Comments
 (0)