ACF mvc.lua example: Difference between revisions
Line 40: | Line 40: | ||
local f = io.open("/etc/hostname", "w") | local f = io.open("/etc/hostname", "w") | ||
if f then | if f then | ||
f:write(name) | f:write(name .. "\n") | ||
f:close() | f:close() | ||
end | end |
Revision as of 21:10, 25 October 2007
Set the hostname with mvc.lua
In this example we will create a simple hostname-setting command-line application using mvc.lua. Once the controller/model are built, you can use the same code to set the hostname via the web with a web-based application controller.
For this example, we will assume you have root access on the linux box you are running on (preferably an alpine box!)
Get the mvc.lua module
svn "export" the mvc.lua module. Export will grab a copy without all the extra hidden "version control" stuff
svn export svn://svn.alpinelinux.org/acf/core/trunk/www/cgi-bin/mvc.lua
Create a model and controller
Create a file hostname-model.lua, defining the module functions to set and read the hostname. We return a table for each function including the value, error message and type of the value ("String" in the case of the hostname).
hostname-model.lua
-- Model functions for retrieving / setting the hostname module ( ..., package.seeall ) -- All functions return a table with -- A value, the type of the value, and a message if there was an error local hosttype={ type="string" } update= function ( name ) -- Check to make sure the name is valid if (name == nil) then hosttype.msg = "Hostname cannot be nil" elseif (#name > 16) then hosttype.msg = "Hostname must be less than 16 chars" elseif (string.find(name, "[^%w%_%-]")) then hosttype.msg = "Hostname can contain alphanumerics only" end -- If it is, set the hostname if (hosttype.msg == nil ) then local f = io.open("/etc/hostname", "w") if f then f:write(name .. "\n") f:close() end f = io.popen ("/bin/hostname -F /etc/hostname") f:close() return read() -- Otherwise, return the error message else hosttype.value = name return hosttype end end read= function () local f = io.popen ("/bin/hostname") local n = f:read("*a") or "none" f:close()
Create a file hostname-controller.lua, defining the functions that an "end user" could run. We define Create Read Update Delete as standard actions:
hostname-controller.lua
-- hostname controller code module ( ... , package.seeall ) create = function (self ) return self.model.update(self.clientdata.hostname) end read = function (self) return self.model.read() end update = create delete = function (self ) self.clientdata.hostname="" return self.worker":create() end
Optionally test the model code (without mvc.lua)
If you want, you can create a test.lua script to validate the model code works on its own:
test.lua
m=require("hostname-model") print(m.update(arg[1]).msg) print(m.read().value)
You can then test this with:
#lua test.lua "Alpine" nil Alpine
#lua test.lua "Invalid Name" Hostname can contain alphanumerics only Alpine
Make an MVC based application
To make the model and and controller work within the mvc.lua framework, we must do serveral things.
1. Create a configuration file. We'll call the application helloworld, so edit helloworld.conf and add:
appdir=helloworld/app/
2. Move the model and controller to the helloworld app directory:
mkdir -p helloworld/app mv helloworld-*.lua helloworld/app
3. Create an application level controller in the helloworld/app directory, named helloworld/app/app-controller.lua
module ( ..., package.seeall) -- application specific functions will go here
Nothing else needs to go in this controller for now.
4. Create a dispatch wrapper program, named helloworld.lua in the current directory:
-- Simple CLI based mvc application -- this is to get around having to store -- the config file in /etc/helloworld/helloworld.conf ENV={} ENV.HOME="." -- load the module require("mvc") -- create an new "mvc object" MVC=mvc:new() -- load the config file so we can find the appdir MVC:read_config("helloworld") -- create an application container APP=MVC:new("app") -- dispatch the request APP.clientdata.hostname=arg[2] APP:dispatch( "", "hostname", (arg[1] or ""))
This application loads the "mvc.lua" framework, creates an mvc "object" named "MVC", then reads the helloworld.conf file to find out where the app dir is (helloworld/app/). It then loads the app-controller.lua into a new "application level" object named APP. Finally, it sets the clientdata and dispatches the hostname-controller/model pair.
5. Test the application:
# lua helloworld.lua no-such-function foo The following unhandled application error occured: controller: "hostname" does not have a "no-such-function" action.