Created
February 2, 2015 00:50
-
-
Save wolfiestyle/b80fbc4e46220b673331 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env wsapi.cgi | |
local fw = require "framework" | |
local app = fw.app.new() | |
app:setup_session("s3cret key") | |
local page_home = [[ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>${title}</title> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<h1>Example app</h1> | |
<ul> | |
<li><a href="${req:link '/session'}">show session</a></li> | |
<li><a href="${req:link '/routes'}">dump routes</a></li> | |
<li><a href="${req:link '/stuff/42/view'}">more stuff</a></li> | |
</ul> | |
</body> | |
</html> | |
]] | |
-- home page | |
app:path "/" | |
:GET(function(resp, req) | |
resp.stash.title = "framework test" | |
resp:write_template(page_home) | |
end) | |
local function print_header(resp, req) | |
resp:write('<div><a href="', req:link "/", '">back</a></div>\n') | |
end | |
-- session | |
app:path "/session" | |
:GET(print_header) | |
:GET(function(resp, req) | |
if not req.session.id then | |
req.session.id = fw.random_string(16) | |
end | |
resp:write("your session id is: ", fw.sanitize(req.session.id), "<br>\n") | |
resp:write('<form method="POST"><input type="submit" value="Reset"></form>\n') | |
end) | |
:POST(function(resp, req) | |
req:session_end() | |
resp:redirect_app "/session" | |
end) | |
-- dump routes | |
app:path "/routes" | |
:GET(print_header) | |
:GET(function(resp, req) | |
resp:dump(app.routes.index) | |
end) | |
-- more stuff | |
app:path "/stuff/:int/view" | |
:GET(print_header) | |
:GET(function(resp, req, n) | |
resp:write(fw.tag.h1("Interesting stuff " .. n)) | |
end) | |
:GET(function(resp, req, n) | |
resp:write(fw.tag.p{ "derp", "lol", n }) | |
-- a non-false return ends the chain | |
if math.random() > .5 then | |
return true | |
end | |
end) | |
:GET(function(resp, req, n) | |
resp:write(fw.tag.h3 "You won the internet!") | |
end) | |
-- must return the app object | |
return app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment