ACF acf www 示例

来自 Alpine Linux
此材料已过时...

请随时帮助我们制作最新版本。(讨论

使用 Web 界面设置主机名

在此示例中,我们将使用上一个示例中的 hostname-model.lua 和 hostname-controller.lua,通过 acf web 界面设置主机名。

在此示例中,我们假设您拥有您正在运行的 Linux 机器的 root 访问权限(最好是 Alpine 机器!)

使用现有代码

我们应该在当前目录下有 'mvc.lua',控制器和模型在 helloworld/app 中。控制器和模型看起来与 mvc.lua 示例的末尾相同,除了我们去掉了 on_load、pre_exec、post_exec 和 on_unload 方法。

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

获取并配置 ACF

1. 从 svn 获取所有 acf 代码和支持库的副本,并复制到 /usr/share/acf 目录中

git clone https://gitlab.alpinelinux.org/acf/acf-core.git /usr/share/acf

2. 将 acf.conf 文件复制到 /etc/acf

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

3. 启动 Web 服务器

您需要安装 haserl、lua 和 mini_httpd 才能运行 acf。如果尚未安装,请安装它们。


启动 mini_httpd(目前手动启动)

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


4. 移动您的主机名模型和控制器

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

5. 创建一个新的视图模板,它是一个 “lua 服务器页面”。创建文件为 /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. 创建一个新的角色文件,它授予所有用户访问您的操作的权限。创建文件为 /usr/share/acf/app/sample/hostname.roles

GUEST=hostname/read,hostname/update

尝试应用

将您的浏览器指向 your host/cgi-bin/acf/sample/hostname/read

现在您应该能够使用 Web 界面和您现有的模型和控制器更新主机名。