improve method name, use ncall for server.listen

This commit is contained in:
Daniel Barlow 2024-09-26 22:14:45 +01:00
parent 9ef1947211
commit 42aa1d8f0a
1 changed files with 4 additions and 8 deletions

View File

@ -86,22 +86,20 @@
(: :sign ca-key))] (: :sign ca-key))]
(crt:toPEM))) (crt:toPEM)))
(fn approved-request? [csr] (fn approve-request? [csr]
(let [attr (csr:getAttributes)] (let [attr (csr:getAttributes)]
(accumulate [found false (accumulate [found false
_ v (ipairs (. attr "challengePassword"))] _ v (ipairs (. attr "challengePassword"))]
(or found (= v options.challenge-password))))) (or found (= v options.challenge-password)))))
(fn handle-sign-csr [out] (fn handle-sign-csr [out]
(let [req (csr.new (out:get_body_as_string))] (let [req (csr.new (out:get_body_as_string))]
(if (approved-request? req) (if (approve-request? req)
(do (do
(out:write_headers (make-headers 200 { :content-type "text/plain" }) false) (out:write_headers (make-headers 200 { :content-type "text/plain" }) false)
(out:write_chunk (new-crt req) true)) (out:write_chunk (new-crt req) true))
(send-error out 400 "missing attributes in CSR")))) (send-error out 400 "missing attributes in CSR"))))
(fn on-stream [sv out] (fn on-stream [sv out]
(let [hdrs (out:get_headers) (let [hdrs (out:get_headers)
method (hdrs:get ":method") method (hdrs:get ":method")
@ -114,14 +112,12 @@
(fn new-server [] (fn new-server []
(let [(addr port) (string.match options.bind-address "(.+):(%d+)$")] (let [(addr port) (string.match options.bind-address "(.+):(%d+)$")]
(case (server.listen (ncall (server.listen
{ {
:host addr :host addr
:port (tonumber port) :port (tonumber port)
:onstream on-stream :onstream on-stream
}) }))))
f (doto f (print))
(nil e) (error e))))
(let [s (new-server)] (let [s (new-server)]
(ncall (s:listen)) (ncall (s:listen))