-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.janet
50 lines (39 loc) · 1.31 KB
/
serve.janet
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
(use osprey)
(import err)
(import path)
(import ./views :as v)
(defn s. [& args] (string ;args))
(def valid-book-ids
(as->
(os/dir "./json_bible") it
(filter |(and
(not= $ "toc.json")
(string/has-suffix? ".json" $)) it)
(map |[(slice $ 0 -6) true] it)
(flatten it)
(table ;it)))
(defn get-book-file [id]
(when (valid-book-ids id)
(path/join "json_bible" (s. id ".json"))))
(GET "/" (ok text/html (v/home)))
(GET "/toc"
@{:status 200
:headers @{"Content-Type" "application/json"}
:body (slurp (path/join "json_bible" "toc.json"))})
(GET "/book/:book-id"
(pp (params :book-id))
(if-let [book-path (-> (params :book-id) get-book-file) ]
@{:status 200
:headers @{"Content-Type" "application/json"}
:body (slurp book-path)}
@{:status 404
:headers @{"Content-Type" "text/plain"}
:body (s. "Book ID " (params :book-id) " is not valid") }))
(var *port* 60808)
(let [port-str (os/getenv "PORT" "60808")]
(set *port* (scan-number (os/getenv "PORT" "60808")))
(when (nil? *port*) (err/str "PORT should be a number, not " port-str ".")))
(enable :static-files)
(when (= [:windows "development"] [(os/which) (os/getenv "DEPLOY_ENV")])
(os/shell "start http://localhost:60808"))
(server *port*)