HTTPD Library

From SolarStrike wiki
Jump to: navigation, search

For a step-by-step guide of setting up a simple website using this library, see this forum post.

Creating a HTTPD instance

object Httpd()

object Httpd(table config)

The HTTPD library is optional and the module must be required before use. Next, the Httpd class must be instantiated:

require('httpd/httpd');

httpd = Httpd();


Optionally, you may provide config parameters:

httpd = Httpd({
   ip            = '0.0.0.0',        -- The IP to bind to; use 0.0.0.0 to accept any connection; default 127.0.0.1
   port          = 80,               -- The port to bind to; default 8080
   controllerDir = './Controllers/',  -- Folder to look for controller objects; default /Controllers/
   viewDir       = './Views/',        -- Folder to look for view files & templates; default /Views/
   staticDir     = './Static/',       -- Folder to look for static files (images, css, javascript, etc.); default /Static/
)};

Supported Methods

httpd:handleEvent()

boolean httpd:handleEvent(string event, ...)

Passes an event off to be handled by the Httpd. Returns true if the event was handled, otherwise false. You should place this in your macro.event() function and check its return result. For example:

function macro.event(e, ...)
	if( not httpd:handleEvent(e, ...) ) then
		-- Wasn't handled by the HTTPD, so we might need to handle it some other way
	end
end


Routes

When initiated, the Httpd expects to be able to load a routes.lua file which is located in the same directory as your calling script(probably main.lua). This routes file should contain calls to the Route object; this object is instantiated for you automatically when you load the HTTPD library. In this file, you will tell the Route object to mount various controllers to URIs via the Route:controller() function.


Supported Methods

Route:controller(string uriSegment, string controllerName)

This function maps the first URI segment of a URL to a specific controller, which must be the exact name of the controller class. For example:

Route:controller('/',    'HomeController');  -- This is a special URI; this is your base page, as if you accessed http://127.0.0.1:8080 (assuming default config)
Route:controller('home', 'HomeController');  -- Just like above, but the home controller is now accessible via / OR /home
Route:controller('test', 'TestController');  -- Just some other controller for an example