-- redbean lua server page demo
local function main()
-- This is the best way to print data to the console or log file.
Log(kLogWarn, "hello from \e[1mlua\e[0m!")
-- This check is pedantic but might be good to have.
if GetMethod() ~= 'GET' and GetMethod() ~= 'HEAD' then
ServeError(405)
SetHeader('Allow', 'GET, HEAD')
return
end
-- These two lines are optional.
-- The default behavior is to do this if you don't.
SetStatus(200) -- Shorthand for SetStatus(200, "OK")
SetHeader('Content-Type', 'text/html; charset=utf-8')
-- Response data is buffered until the script finishes running.
-- Compression is applied automatically, based on your headers.
Write('\n')
Write('
redbean\n')
Write('
redbean lua server page demo
\n')
-- Prevent caching.
-- We need this because we're doing things like putting the client's
-- IP address in the response so we naturally don't want that cached
SetHeader('Expires', FormatHttpDateTime(GetDate()))
SetHeader('Cache-Control', 'no-cache, must-revalidate, max-age=0')
-- GetParams() returns an ordered list of Request-URI query params.
Write('
request uri parameters
\n')
params = GetParams()
if #params > 0 then
Write('
\n')
for i = 1,#params do
Write('
')
Write(EscapeHtml(params[i][1]))
Write('\n')
if params[i][2] then
Write('
')
Write(EscapeHtml(params[i][2]))
Write('\n')
end
end
Write('
\n')
else
Write('
\n')
Write('none \n')
Write('ProTip: Try clicking here!\n')
end
-- Access redbean command line arguments.
-- These are the ones that come *after* the redbean server arguments.
Write('
command line arguments
\n')
if #argv > 0 then
Write('
\n')
for i = 1,#argv do
Write('
')
Write(EscapeHtml(argv[i]))
Write('\n')
end
Write('
\n')
else
Write('
none\n')
end
Write([[
post request html form demo
]])
Write([[
xmlhttprequest request demo
]])
Write('
extra information
\n')
Write('
GetClientAddr()\n')
Write('
')
Write(GetClientAddr())
Write('\n')
Write('
GetServerAddr()\n')
Write('
')
Write(GetServerAddr())
Write('\n')
Write('\n')
end
main()