ACF acf www example

From Alpine Linux
Revision as of 12:30, 7 March 2016 by Fab (talk | contribs) (Reverted edits by RuthHughes (talk) to last revision by Ttrask)
This material is obsolete ...

Please feel free to help us make an up-to-date version. (Discuss)

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, and on_unload 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.alpinelinux.org/acf/core/trunk /usr/share/acf

2. Copy the acf.conf file to /etc/acf

cp /usr/share/acf/acf.conf /etc/acf/acf.conf

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/**' start


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 = ... ?>                                                                          
<h1>Hostname</h1>

<form action="update" method=post>                                                          
<p>The Hostname is now <input name=hostname value="<?= form.value ?>"</p>
                                                                                            
<p>input type=submit value=Submit></p>
</form>

6. Create a new roles file, which gives all users permission to access your actions. Create the file as /usr/share/acf/app/sample/hostname.roles

GUEST=hostname/read,hostname/update

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.