Commit dba72856 authored by Ted Trask's avatar Ted Trask
Browse files

Modified dnscache to use controllerfunctions, modelfunctions, and common lsp files.

git-svn-id: svn://svn.alpinelinux.org/acf/dnscache/trunk@1282 ab2d0c66-481e-0410-8bed-d214d4d58bed
parent 49a7dc03
<? local form, viewlibrary = ...
<? local form, viewlibrary, page_info = ...
require("viewfunctions")
?>
<?
......@@ -15,6 +15,7 @@ end ?>
<H1>Config</H1>
<?
form.action = page_info.script .. page_info.prefix .. page_info.controller .. "/" .. page_info.action
displayform(form)
?>
......
module(..., package.seeall)
-- Load libraries
require("format")
local function handle_form(getFunction, setFunction, clientdata, option, label, descr)
local form = getFunction()
if clientdata[option] then
form.errtxt = nil
for name,value in pairs(form.value) do
value.errtxt = nil
if value.type == "boolean" then
value.value = (clientdata[name] ~= nil)
elseif value.type == "list" then
value.value = {}
if clientdata[name] and clientdata[name] ~= "" then
for ip in string.gmatch(clientdata[name].."\n", "%s*(%S[^\n]*%S)%s*\n") do
table.insert(value.value, ip)
end
end
else
value.value = clientdata[name] or value.value
end
end
form = setFunction(form)
if not form.errtxt then
form.descr = descr
end
end
form.type = "form"
form.option = option
form.label = label
return form
end
require("controllerfunctions")
default_action = "status"
......@@ -43,47 +10,19 @@ function status(self)
end
function startstop(self)
local result
if self.clientdata.action then
result = self.model.startstop_service(self.clientdata.action)
self.sessiondata.dnscachestartstopresult = result
self.redirect_to_referrer(self)
end
local status = self.model.getstatus()
status = status.value.status
if self.sessiondata.dnscachestartstopresult then
result = self.sessiondata.dnscachestartstopresult
self.sessiondata.dnscachestartstopresult = nil
end
return cfe({ type="group", value={status=status, result=result} })
return controllerfunctions.handle_startstop(self, self.model.startstop_service, self.model.getstatus, self.clientdata)
end
function config(self)
return handle_form(self.model.getconfig, self.model.setconfig, self.clientdata, "Save", "Edit Config", "Configuration Set")
return controllerfunctions.handle_form(self, self.model.getconfig, self.model.setconfig, self.clientdata, "Save", "Edit Config", "Configuration Set")
end
function expert(self)
local filedetails
if self.clientdata.Save then
filedetails = self.model.setconfigfile(self.clientdata.filecontent)
if not filedetails.errtxt then
filedetails.descr = "Config file set"
end
else
filedetails = self.model.getconfigfile()
end
filedetails.type = "form"
filedetails.option = "Save"
filedetails.label = "Set config file"
return filedetails
return controllerfunctions.handle_form(self, self.model.getconfigfile, self.model.setconfigfile, self.clientdata, "Save", "Edit Config File", "Configuration File Set")
end
function editips(self)
return handle_form(self.model.getIPs, self.model.setIPs, self.clientdata, "Save", "Edit IP List", "IP List Set")
return controllerfunctions.handle_form(self, self.model.getIPs, self.model.setIPs, self.clientdata, "Save", "Edit IP List", "IP List Set")
end
function listdomains(self)
......@@ -91,20 +30,14 @@ function listdomains(self)
end
function createdomain(self)
local cmdresult = handle_form(self.model.getNewDomain, self.model.setNewDomain, self.clientdata, "Create", "Create new domain", "New domain created")
if cmdresult.descr then
redirect_to_referrer(self)
end
return cmdresult
return controllerfunctions.handle_form(self, self.model.getNewDomain, self.model.setNewDomain, self.clientdata, "Create", "Create New Domain", "New Domain Created")
end
function editdomain(self)
return handle_form(function(form) return self.model.getDomain(self.clientdata.domain) end,
self.model.setDomain, self.clientdata, "Save", "Edit domain entry", "Domain saved")
return controllerfunctions.handle_form(self, function(form) return self.model.getDomain(self.clientdata.domain) end,
self.model.setDomain, self.clientdata, "Save", "Edit Domain Entry", "Domain Saved")
end
function deletedomain(self)
local cmdresult = self.model.deleteDomain(self.clientdata.domain)
redirect_to_referrer(self)
return cmdresult
return self:redirect_to_referrer(self.model.deleteDomain(self.clientdata.domain))
end
<? local form, viewlibrary = ...
require("viewfunctions")
?>
<?
--[[ DEBUG INFORMATION
io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>")
io.write(html.cfe_unpack(form))
io.write("</span>")
--]]
?>
<H1><?= form.label ?></H1>
<?
form.action = "createdomain"
local order = { }
displayform(form, order)
?>
../form-html.lsp
\ No newline at end of file
<? local form, viewlibrary = ...
<? local form, viewlibrary, page_info = ...
require("viewfunctions")
?>
<?
......@@ -11,6 +11,7 @@ io.write("</span>")
<H1><?= form.label ?></H1>
<?
form.action = page_info.script .. page_info.prefix .. page_info.controller .. "/" .. page_info.action
form.value.domain.contenteditable = false
local order = { "domain", "iplist" }
displayform(form, order)
......
<? local form, viewlibrary = ...
require("viewfunctions")
?>
<?
--[[ DEBUG INFORMATION
io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>")
io.write(html.cfe_unpack(form))
io.write("</span>")
--]]
?>
<H1><?= form.label ?></H1>
<?
displayform(form)
?>
../form-html.lsp
\ No newline at end of file
<? local form, viewlibrary = ... ?>
<? require("viewfunctions") ?>
<?
--[[ DEBUG INFORMATION
io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>")
io.write(html.cfe_unpack(form))
io.write("</span>")
--]]
?>
<? ---[[ ?>
<? if viewlibrary and viewlibrary.dispatch_component then
viewlibrary.dispatch_component("status")
end ?>
<? --]] ?>
<H1>Configuration</H1>
<H2>Expert Configuration</H2>
<H3>File Details</H3>
<DL>
<?
displayitem(form.value.filename)
displayitem(form.value.filesize)
displayitem(form.value.mtime)
?>
</DL>
<H3>File Content</H3>
<? if form.descr then ?><P CLASS='descr'><?= string.gsub(form.descr, "\n", "<BR>") ?></P><? end ?>
<? if form.errtxt then ?><P CLASS='error'><?= string.gsub(form.errtxt, "\n", "<BR>") ?></P><? end ?>
<form action="" method="POST">
<textarea name="filecontent">
<?= form.value.filecontent.value ?>
</textarea>
<? if form.value.filecontent.errtxt then ?><P CLASS='error'><?= string.gsub(form.value.filecontent.errtxt, "\n", "<BR>") ?></P><? end ?>
<DL><DT></DT><DD><input class="submit" type="submit" name="<?= form.option ?>" value="Save"></DD></DL>
</form>
<? if viewlibrary and viewlibrary.dispatch_component then
viewlibrary.dispatch_component("startstop")
end ?>
<?
--[[ DEBUG INFORMATION
io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>")
io.write(html.cfe_unpack(form))
io.write("</span>")
--]]
?>
../expert-html.lsp
\ No newline at end of file
<? local data, viewlibrary = ...
<? local data, viewlibrary, page_info, session = ...
require("viewfunctions")
?>
<?
......@@ -9,6 +9,8 @@ io.write("</span>")
--]]
?>
<? displaycommandresults({"deletedomain"}, session) ?>
<h1>Configuration</h1>
<h2>Edit/View DNS server entries</h2>
<TABLE>
......
module(..., package.seeall)
-- Load libraries
require("modelfunctions")
require("fs")
require("procps")
require("getopts")
require("format")
require("posix")
require("daemoncontrol")
require("validator")
require("processinfo")
-- Set variables
local configfile = "/etc/conf.d/dnscache"
......@@ -21,15 +19,6 @@ local descr = {
-- ################################################################################
-- LOCAL FUNCTIONS
local function process_status_text(procname)
local t = procps.pidof(procname)
if (t) and (#t > 0) then
return "Enabled"
else
return "Disabled"
end
end
local function validateconfig(config)
local success = true
if config.value.IPSEND and not validator.is_ipv4(config.value.IPSEND.value) then
......@@ -47,22 +36,6 @@ local function validateconfig(config)
return success, config
end
local function getfiledetails(file)
local filename = cfe({ value=file, label="File name" })
local filecontent = cfe({ type="longtext", label="File content" })
local filesize = cfe({ value="0", label="File size" })
local mtime = cfe({ value="---", label="File date" })
if fs.is_file(file) then
local filedetails = fs.stat(file)
filecontent.value = fs.read_file(file)
filesize.value = filedetails.size
mtime.value = filedetails.mtime
else
filename.errtxt = "File not found"
end
return cfe({ type="group", value={filename=filename, filecontent=filecontent, filesize=filesize, mtime=mtime}, label="Config file details" })
end
local function validatedomain(domain)
local success = false
local domains = getDomains()
......@@ -88,34 +61,11 @@ end
-- PUBLIC FUNCTIONS
function startstop_service(action)
-- action is validated in daemoncontrol
local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol(processname, action)
return cfe({ type="boolean", value=cmdresult, descr=cmdmessage, errtxt=cmderror, label="Start/Stop result" })
return modelfunctions.startstop_service(processname, action)
end
function getstatus()
local status = {}
local value, errtxt = processinfo.package_version(packagename)
status.version = cfe({
label="Program version",
value=value,
errtxt=errtxt,
})
status.status = cfe({
label="Program status",
value=process_status_text(processname),
})
local autostart_sequense, autostart_errtxt = processinfo.process_botsequence(processname)
status.autostart = cfe({
label="Autostart sequence",
value=autostart_sequense,
errtxt=autostart_errtxt,
})
return cfe({ type="group", value=status, label="DNS Cache Status" })
return modelfunctions.getstatus(processname, packagename, "DNS Cache Status")
end
function getconfig()
......@@ -152,18 +102,22 @@ function setconfig(config)
end
function getconfigfile()
local config = getfiledetails(configfile)
local config = modelfunctions.getfiledetails(configfile)
-- Validate
return config
end
function setconfigfile(modifications)
local configcontent = string.gsub(format.dostounix(modifications), "\n*$", "")
local config
function setconfigfile(config)
local configcontent = string.gsub(format.dostounix(config.value.filecontent.value), "\n*$", "")
-- Validate
if fs.is_file(configfile) then
fs.write_file(configfile, configcontent)
config = getfile(configfile)
config = modelfunctions.getfiledetails(configfile)
else
config = getfile(configfile)
config.value.filecontent.value = configcontent
config.errtxt = "Failed to set config"
end
......
<? local data = ... ?>
<? --[[
io.write(html.cfe_unpack(data))
--]] ?>
<H1>Management</H1>
<DL>
<form action="startstop" method="POST">
<DT>Program control-panel</DT>
<DD>
<input class="submit" type="submit" name="action" value="Start" <? if data.value.status.value== "Enabled" then io.write("disabled") end ?>>
<input class="submit" type="submit" name="action" value="Stop" <? if data.value.status.value== "Disabled" then io.write("disabled") end ?>>
<input class="submit" type="submit" name="action" value="Restart" <? if data.value.status.value== "Disabled" then io.write("disabled") end ?>>
</DD>
</form>
<? if data.value.result then ?>
<DT>Previous action result</DT>
<DD>
<? if data.value.result.descr then ?>
<P CLASS='descr'><?= string.gsub(data.value.result.descr, "\n", "<BR>") ?></P>
<? end if data.value.result.errtxt then ?>
<P CLASS='error'><?= string.gsub(data.value.result.errtxt, "\n", "<BR>") ?></P>
<? end end ?>
</DD>
</DL>
../startstop-html.lsp
\ No newline at end of file
<? local data = ...
require("viewfunctions")
?>
<?
--[[ DEBUG INFORMATION
io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>")
io.write(html.cfe_unpack(data))
io.write("</span>")
--]]
?>
<H1>System Info</H1>
<DL>
<?
displayitem(data.value.status)
displayitem(data.value.version)
displayitem(data.value.autostart)
?>
</DL>
../status-html.lsp
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment