login

= ludweb

This is a simple web framework for LuaJIT on Linux.

Features:
  * routing with regex parameter
  * templates with branching, looping, % include
  * extras: crypto hash for passwords, sqlite database
  * no dependencies besides LuaJIT

= hello world app

```
local lud = require "ludweb"

lud.app.new_app{
    {"GET", "/?", function (req) return "Hello World!" end},
    {"GET", "/me/(%S+)", function (req, name) return "Hi "..name.."!" end}
}:run(8080)
```

= request object

The first argument passed to the route callbacks is a request object
which looks like this:

```
{
    method="GET",
    -- consider a request to "/foo/bar?key=val#potato":
    path="/foo/bar", query={key="val"}, fragment="potato",
    headers={["user-agent"]="Mozilla/5.0"},
    -- for convenience, headers.cookie is parsed when present:
    cookies={my_key=my_val},
    -- the remaining fields are typically used in POST requests
    payload="some data sent by client",
    -- when a web form is submitted, form data is parsed from payload:
    form={email="hi@there.com"}
}
```

= responses

As can be seen in the hello world example above, most callbacks will return just
one value: the payload string, which typically contains some HTML in web apps or
some JSON in APIs. However, callbacks can return up to four values:

```
local function my_callback(req)
    local payload = "Uh-oh, we can't find the page you're after.\n"
    local status = 404
    local reason = "Not Found"  -- for the first line in HTTP response
    -- cookies can be used e.g. to store session IDs
    local cookie = {key="sid", val="abce6c3...", path="/", age=2*60*60}
    local cookies = {cookie}
    return payload, status, reason, cookies
end
```

A few return values have special meaning:
  * if payload is nil then the server is stopped (i.e. the :run() method returns)
  * if status is nil, the default values of status=200 and reason="OK" are used
  * if status is 303, payload will be used as redirection destination (see below)

The special 303 response is used to redirect the client to another address.
If this status is used, the default value of reason will be "See Other".
The target address must be given in the payload without any other data.
Here's a typical example:

```
local function private_page(req)
    -- check which user is logged in on this session, if any
    local user = get_user(req.cookies)
    -- if there's no login information, redirect to login page
    if user == nil then
        return "/login", 303
    end
    -- otherwise proceed to show private page
    return "some private data only accessible to authenticated users"
end
```

= templates

```
local lud = require "ludweb"

local tpl = [[
  % if $users == nil then
  <p>no users found</p>
  % else
  <ul>
    % for user in $users do
    <li><a href="/user/{{$user.nick}}">{{$user.name}}</a></li>
    % end
  </ul>
  % end
]]

local users = {
    {nick="jsmith", name="John Smith"},
    {nick="mlobato", name="Monteiro Lobato"},
    {nick="mkupona", name="Mwana Kupona"}
}

local str = lud.template.render_str(tpl, {users=users})
print(str)
```

The function `render_file()` is similar to `render_str()`,
except that it takes a file name as first argument.

= crypto

TODO

= sqlite

TODO