Example simple.lua
local stardust = require "stardust"
local cjson = require "cjson"
local router = require "stardust.router"
local _M = {}
local function html(res, data)
res.status = 200
res.headers["Content-Type"] = "text/html"
res.body = data
end
local encode = cjson.encode
local function json(res, data)
res.status = 200
res.headers["Content-Type"] = "application/json"
res.body = encode(data)
end
local app = stardust.new()
local r = router.new()
app:use(r)
app:use(stardust.sender)
r:get("%.html?$",
function(req, res)
return html(res, "<html>You came looking for " .. req.path .. "</html>")
end
)
local options = { foo = "bar" }
r:get("^/options",
function(req, res)
local foo = {
options = options,
path = req.path
}
return json(res, foo)
end
)
r:get(r:regex([[/something/(\d+)]]),
function(req, res)
return json(res, { id = req.params[1] })
end
)
r:get(r:regex([[/another/(?<id>\d+)]]),
function(req, res)
return json(res, { id = req.params.id })
end
)
r:get("/id/(%d)",
function(req, res)
return json(res, { id = req.params[1] })
end
)
function _M.run(ngx)
return app:run(ngx)
end
return _M