Difference between revisions of "HTTPD Library"
(Created page with "= Creating a cache instance = '''object Httpd()''' '''object Httpd(table config)''' The HTTPD library is optional and the module must be required before use. Next, the Httpd...") |
|||
Line 1: | Line 1: | ||
− | = Creating a | + | For a step-by-step guide of setting up a simple website using this library, see [https://solarstrike.net/phpBB3/viewtopic.php?f=6&t=6367 this forum post]. |
+ | |||
+ | = Creating a HTTPD instance = | ||
'''object Httpd()''' | '''object Httpd()''' | ||
Line 10: | Line 12: | ||
httpd = Httpd(); | httpd = Httpd(); | ||
</source> | </source> | ||
+ | |||
Optionally, you may provide config parameters: | Optionally, you may provide config parameters: | ||
Line 20: | Line 23: | ||
staticDir = '/Static/', -- Folder to look for static files (images, css, javascript, etc.); default /Static/ | staticDir = '/Static/', -- Folder to look for static files (images, css, javascript, etc.); default /Static/ | ||
)}; | )}; | ||
+ | </source> | ||
+ | |||
+ | |||
+ | =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: | ||
+ | <source lang="lua"> | ||
+ | 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 | ||
+ | </source> | ||
+ | |||
+ | |||
+ | =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. | ||
+ | |||
+ | '''Route:controller(uriSegment, 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: | ||
+ | |||
+ | <source lang="lua"> | ||
+ | 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 | ||
</source> | </source> |
Revision as of 04:32, 9 September 2016
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.
Route:controller(uriSegment, 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