1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#!/usr/bin/env wsapi.cgi
local orbit = require "orbit"
-- Orbit applications are usually modules,
-- orbit.new does the necessary initialization
module("hello", package.seeall, orbit.new)
-- These are the controllers, each receives a web object
-- that is the request/response, plus any extra captures from the
-- dispatch pattern. The controller sets any extra headers and/or
-- the status if it's not 200, then return the response. It's
-- good form to delegate the generation of the response to a view
-- function
function index(web)
return render_index()
end
function say(web, name)
return render_say(web, name)
end
-- Builds the application's dispatch table, you can
-- pass multiple patterns, and any captures get passed to
-- the controller
hello:dispatch_get(index, "/", "/index")
hello:dispatch_get(say, "/say/(%a+)")
-- These are the view functions referenced by the controllers.
-- orbit.htmlify does through the functions in the table passed
-- as the first argument and tries to match their name against
-- the provided patterns (with an implicit ^ and $ surrounding
-- the pattern. Each function that matches gets an environment
-- where HTML functions are created on demand. They either take
-- nil (empty tags), a string (text between opening and
-- closing tags), or a table with attributes and a list
-- of strings that will be the text. The indexing the
-- functions adds a class attribute to the tag. Functions
-- are cached.
--
-- This is a convenience function for the common parts of a page
function render_layout(inner_html)
return html{
head{ title"Hello" },
body{ inner_html }
}
end
function render_hello()
return p.hello"Hello World!"
end
function render_index()
return render_layout(render_hello())
end
function render_say(web, name)
return render_layout(render_hello() ..
p.hello((web.input.greeting or "Hello ") .. name .. "!"))
end
orbit.htmlify(hello, "render_.+")
|