-
Notifications
You must be signed in to change notification settings - Fork 6
/
guestbook.clj
72 lines (61 loc) · 2.67 KB
/
guestbook.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(require '[clojure.edn :as edn]
'[clojure.java.browse :as browse]
'[clojure.java.io :as io]
'[cognitect.transit :as transit]
'[org.httpkit.server :as srv]
'[hiccup.core :as hp])
(import 'java.io.ByteArrayOutputStream)
(def port 8083)
(def filename "messages.txt")
(defn html [cljs-file]
(hp/html
[:html
[:head
[:meta {:charset "UTF-8"}]
[:meta {:name "viewport" :content "width=device-width, initial-scale=1"}]
[:link {:rel "shortcut icon" :href "data:,"}]
[:link {:rel "apple-touch-icon" :href "data:,"}]
[:link {:rel "stylesheet" :href "https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css"}]
[:script {:crossorigin nil :src "https://unpkg.com/react@17/umd/react.production.min.js"}]
[:script {:crossorigin nil :src "https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"}]
[:script {:src "https://cdn.jsdelivr.net/npm/[email protected]/dist/scittle.js" :type "application/javascript"}]
[:script {:src "https://cdn.jsdelivr.net/npm/[email protected]/dist/scittle.reagent.js" :type "application/javascript"}]
[:script {:src "https://cdn.jsdelivr.net/npm/[email protected]/dist/scittle.cljs-ajax.js" :type "application/javascript"}]
[:title "Guestbook"]]
[:body
[:div {:id "content"}]
[:script {:type "application/x-scittle" :src cljs-file}]]]))
(defn home-save-message! [req]
(let [params (transit/read (transit/reader (:body req) :json))
text (prn-str (assoc params :timestamp (java.util.Date.)))]
(spit filename text :append true)
"post success!"))
(defn db-get-messages []
(if (.exists (io/file filename))
(edn/read-string (str "[" (slurp filename) "]"))
[]))
(defn home-message-list [_]
(let [out (ByteArrayOutputStream. 4096)
writer (transit/writer out :json)]
(transit/write writer {:messages (db-get-messages)})
(.toString out)))
(defn home-page [_request cljs-file]
(html cljs-file))
(defn home-routes [{:keys [:request-method :uri] :as req}]
(case [request-method uri]
[:get "/"] {:body (home-page req "guestbook.cljs")
:status 200}
[:get "/messages"] {:headers {"Content-type" "application/transit+json"}
:body (home-message-list req)
:status 200}
[:post "/message"] {:body (home-save-message! req)
:status 200}
[:get "/guestbook.cljs"] {:body (slurp"guestbook.cljs")
:status 200}))
(defn core-http-server []
(srv/run-server home-routes {:port port}))
(let [url (str "http://localhost:" port "/")]
(core-http-server)
(println "serving" url)
(browse/browse-url url)
@(promise))