-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoolparty.janet
27 lines (26 loc) · 926 Bytes
/
poolparty.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
(import _poolparty)
(defn serve
[handler &keys {:inf inf :outf outf :health-check health-check}]
(default inf stdin)
# By default we pass in an extra file descriptor
# that janet doesn't know about, we open this manually.
(default outf (_poolparty/out-fdopen 3))
(when (nil? outf)
(error "unable to open output fd, this is fd 3 by default."))
(when (= outf (dyn :out))
(error "server outf should not be the same as :out, hint: (setdyn :out stderr)"))
(default health-check (fn [] nil))
(def buf @"")
(while true
(def req (_poolparty/read-request inf))
(cond
(= req :health-check)
(health-check)
(let [resp (handler req)]
(_poolparty/format-response resp buf)
(file/write outf buf)
(file/flush outf)
# Clear buffer if its a large response
(when (> (length buf) 1000000)
(buffer/clear buf)
(buffer/trim buf))))))