|
Project Information
Members
Featured
Downloads
Links
|
LWT (Lua Web Tools) allows you to develop web applications in Lua, and run them directly in the Apache HTTP server. The core functionality of LWT is provided by an Apache module, mod_lwt. In addition, LWT provides optional Lua modules for accessing databases and caches. Features
The IS and cache modules are designed to work nicely along mod_lwt, but are optional and independent Lua modules. Design GoalsLWT is built on the following design goals:
ExampleThe following Lua script and HTML template implement a simple number guessing game. Head to the wiki for more formal documentation. The Lua script: require "httpd"
require "cache"
require "cache.memcached"
-- Configure cache
local CACHE = cache.configure({ driver = cache.memcached })
-- Get the request and arguments
request, args = ...
-- Get game ID
game_id = tonumber(args.game_id) or CACHE:inc("game_id", 1, 1)
-- Get or create game state
local game_key = string.format("game-%d", game_id)
game = CACHE:get(game_key) or { }
-- Process request
if args.guess and game.number then
local guess = tonumber(args.guess)
if guess then
if guess < game.number then
outcome = "low"
elseif guess > game.number then
outcome = "high"
else
outcome = "found"
end
table.insert(game.history, { guess = guess, outcome = outcome })
end
elseif args.new or not game.number then
game.number = (os.time() * 65599) % 1000 + 1
game.history = { }
end
-- Store game state
CACHE:set(game_key, game, 3600)
-- Render
httpd.set_content_type("text/html; charset=utf-8");
httpd.write_template(request.filedir .. "guess.html")The HTML template: <!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Guess the Number</title>
</head>
<body onload="document.getElementById('guess').focus();">
<h1>Guess the Number</h1>
<l:if cond="#game.history > 0">
<h2>Guess History</h2>
<ul>
<l:for names="_, entry" in="ipairs(game.history)">
<li>${entry.guess} – ${entry.outcome == "low" and "too low" or entry.outcome == "high" and "too high" or "found"}</li>
</l:for>
</ul>
</l:if>
<form method="post">
<input type="hidden" name="game_id" value="${game_id}" />
<l:if cond="outcome ~= 'found'">
<h2>Make a Guess</h2>
<input id="guess" type="text" name="guess" size="4" /> (1 – 1000) <input type="submit" value="Guess" />
<l:else />
<h2>Congratulations</h2>
<p>You found the number with ${#game.history} ${#game.history ~= 1 and "guesses" or "guess"}.</p>
<input type="submit" name="new" value="New Game" />
</l:if>
</form>
</body>
</html>RequirementsLWT 0.9 is based on Lua 5.1/5.2 and the Apache HTTP Server 2.2. LicenseLWT is licensed under the MIT license which at the time of this writing is the same license as the one of Lua. |