lua hello-world HTTP server

This commit is contained in:
Daniel Barlow 2022-09-13 20:55:15 +01:00
commit f75b8bd59a
3 changed files with 56 additions and 0 deletions

13
default.nix Normal file
View File

@ -0,0 +1,13 @@
with import <nixpkgs> {};
let lua = lua5_3.withPackages (ps: [
ps.dkjson
ps.lpeg
ps.http
ps.luaposix
]);
in stdenv.mkDerivation {
pname = "grafana-sms-alert";
version = "0.1";
src = ./.;
buildInputs = [lua (fennel.override { inherit lua; }) ];
}

13
main.fnl Normal file
View File

@ -0,0 +1,13 @@
(local server (require :server))
(local { : view } (require :fennel))
(fn handler [payload]
(print (view payload))
"OK")
(doto (server.new handler)
(: :listen)
(: :loop))
(io.stdin:read)

30
server.fnl Normal file
View File

@ -0,0 +1,30 @@
(local server (require :http.server))
(local headers (require :http.headers))
(local json (require :dkjson))
(fn on-stream [handler sv out]
(let [hdrs (out:get_headers)
method (hdrs:get ":method")
path (or (hdrs:get ":path") "/")
rh (headers.new)]
(rh:append ":status" :200)
(rh:append :content-type :text/plain)
(out:write_headers rh false)
(print "got oune" handler)
(let [(obj pos err) (json.decode (out:get_body_as_string) 1 nil)
ret (handler obj)]
(out:write_chunk ret true))))
(fn new-server [handler]
(server.listen
{
:host :localhost
:port 8201
:onstream (partial on-stream handler)
}))
{ :new new-server }
;(srv:listen)
;(srv:loop)