Building a custom GUI to configure a template

Method Table Handler System

I find this a very convenient way to organize a web application. One defines a hash table of string keys that an application supports to the functions that handle each call.

This is how Iguana itself is organized, just a hash table of strings assigned function pointers implemented in C++.

It’s much easier to implement this structure in Lua than in C++ since Lua supports functions as first class values. This how this example does it. A global Lua table is defined called MethodTable:

MethodTable={
   file=ServeFile,
   channel_list=ChannelList,
   save_settings=SaveSettings,
   load_settings=LoadSettings,
}

This just has string keys mapped to Lua functions. When we get a request we can look for a POST or GET variable called ‘method’. If we have a handler function registered for that method, then we call it with the Request object.

local Request = net.http.parseRequest{data=Data}
local MethodName = Request.params.method

if MethodTable[MethodName] then
   MethodTable[MethodName](Request)
else
   local Body = template.Recall('main.html')
   net.http.respond{body=Body}
end

If no method exists we return the default main.html template.

This makes for a straightforward work flow to add new calls.

Leave A Comment?