ACF acf www example

From Alpine Linux
Revision as of 21:05, 30 October 2007 by Nangel (talk | contribs) (First whack at webifying the hostname example code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Set the hostname with a web interface

In this example we will use the hostname-model.lua and hostname-controller.lua from the previous example to set the hostname using the acf web interface.

For this example, we will assume you have root access on the linux box you are running on (preferably an alpine box!)

Use the existing code

We should have 'mvc.lua in the current directory, with the controller and model in helloworld/app The Controller and Model look the same as the end of the mvc.lua example, except we've taken out the on_load, pre_exec, post_exec methods.

helloworld/app/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


helloworld/app/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()
        n=string.gsub(n, "\n$", "")
        hosttype.value = n
        return (hosttype) 
end

Get and configure ACF

1. Grab a copy of all the acf code and support libraries from svn and copy into an "/usr/share/acf" directory

svn export svn svn://svn.alpinelinux.org/acf/core/trunk /usr/share/acf

2. Edit the new acf.conf file

Create a new /etc/acf/acf.conf

appdir=/usr/share/acf/app/
libdir=/usr/share/acf/lib/
sessiondir=/usr/share/acf/session.d/

3. Start the web server

You'll need haserl, lua, and mini_httpd installed to run acf. If not already done, please install them.


Start mini_httpd (by hand for now)

 mini_httpd -d /usr/share/acf/www -c 'cgi-bin/**'


4. Move your hostname model and controller

 mkdir /usr/share/acf/app/sample
 mv helloworld/app/hostname-* /usr/share/acf/app/sample/

5. Create a new view template, which is a "lua server page" Create the file as /usr/share/acf/app/sample/hostname-html.lsp

<? local form = ... ?>                                                                          

Hostname

<form action="update" method=post>                                                              

The Hostname is now <input name=hostname value="<?= form.value ?>"

<input type=submit value=Submit>

</form>   

Try the app

point your browser to your host/cgi-bin/acf/sample/hostname/read

You should now be able to update the hostname using the web interface, and your existing model and controller.