Murga-Projects Forums

Full Version: lighttpd interface
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is a (Linux/Unix/BSD) gui interface to the lighttpd web server.
I've been tweaking it a little here and there since it was still a Lua-FLTK script.
At this point I think it will work for all versions of MurgaLua from the start to 0.5.5 since it doesn't use anything other than Lua and FLTK.

You might want to check/edit the paths under "find conf file" (or just change the lightyvar variable) to reflect where the lighttpd config file is located. This script was designed to work with a lighttpd package for Damn Small Linux that installed to /opt/lighttpd. It should be tweaked some more for general (non-DSL) use, but I was working on it a little today and just thought I'd post it.

It should be run as root, or edit the commands to run as root.

Code:
#!/bin/murgaLua

--[[

  gui controller for lighttpd (c)2006-2007 mikshaw
  should work with all versions of murgaLua, including 0.5.5,
  which was the current version at the time of this release

]]

Fl:get_system_colors()
Fl:scheme(NULL)

-- test if given file is readable
function find_conf(file)
local filetest=io.open(file)
if filetest then
  filetest:close()
  return true
end
end

-- find conf file
lightyvar=os.getenv("LIGHTTPD_CONF")
if lightyvar and find_conf(lightyvar) then lighttpd_conf=lightyvar
elseif find_conf("/opt/lighttpd.conf") then lighttpd_conf="/opt/lighttpd.conf"
else lighttpd_conf="/opt/lighttpd/test/lighttpd.conf" end

tempfile="/tmp/lighttpd.lua.tmp" -- for command output

function checkstatus() -- see if server is running
state:hide() -- prevents label overlap
if os.execute("pidof lighttpd &>/dev/null") == 0 then
state:label("server is running") else state:label("server is not running")
end
state:show()
end

-- this might need some improvement one day
function lcontrol(self)
if self:label() == "test config" then
os.execute("/opt/lighttpd/lighty test &>"..tempfile)
else
os.execute("/opt/lighttpd/lighty "..self:label().." &>"..tempfile.." && sleep 1")
checkstatus()
end
-- show the command output
for line in io.lines(tempfile) do outpoot:add(line) end
outpoot:bottomline(outpoot:size())
os.remove(tempfile)
end

-- start interface
bw=80;bh=25;ww=bw*4;wh=bh*7
w=fltk:Fl_Window(ww,wh,"LIGHTTPD Control")
cfg=fltk:Fl_Box(0,bh,ww,bh)
cfg:label("config file: "..lighttpd_conf)
state=fltk:Fl_Box(0,bh*2,ww,bh)
bup=fltk:Fl_Button(0,0,bw,bh,"start");bup:callback(lcontrol)
bdn=fltk:Fl_Button(bw,0,bw,bh,"stop");bdn:callback(lcontrol)
bre=fltk:Fl_Button(bw*2,0,bw,bh,"restart");bre:callback(lcontrol)
bt=fltk:Fl_Button(bw*3,0,bw,bh,"test config");bt:callback(lcontrol)

outpoot=fltk:Fl_Browser(0,bh*3,ww,bh*4)
w:resizable(outpoot)

checkstatus()
w:show()
Fl:run()


Oh...I forgot it makes use of a bash script too:

Code:
#! /bin/sh

LIGHTTPD=/opt/lighttpd/sbin/lighttpd

# Find a configuration file; checks for existing LIGHTTPD_CONF variable first
[ -r "${LIGHTTPD_CONF}" ] ||  LIGHTTPD_CONF=/opt/lighttpd.conf
[ -r "${LIGHTTPD_CONF}" ] ||  LIGHTTPD_CONF=/opt/lighttpd/test/lighttpd.conf
#PIDFILE=/var/run/lighttpd.pid

testlightycfg() {
        echo "lighttpd: Using configuration file ${LIGHTTPD_CONF}..."
        ${LIGHTTPD} ${1} -t -f ${LIGHTTPD_CONF} || exit 1
}

case $1 in
    start)
        testlightycfg
        echo "Starting lighttpd"
        ${LIGHTTPD} -f ${LIGHTTPD_CONF}
        ;;
    stop)
        echo "Shutting down lighttpd"
        killall ${LIGHTTPD}
        rm -f /var/run/lighttpd.pid
        ;;
    restart)
        $0 stop
        sleep 2
        $0 start
        ;;
    test*)
        testlightycfg -p
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|test}"
        exit 1
        ;;
esac


If I had remembered that, i'd have rewritten it to work completely from murgaLua...my bad.
I'll be back later to fix that.

It also appears that the code tags don't preserve text as well as I'd thought. The bash script is riddled with "\{{mybb-code}}" where there should be variables.

Made some changes to it so it doesn't require the bash script.
The os.execute("sleep 2") should probably bechanged to an internal pause, but I didn't want to bother scripting a pause today, and wanted it to work on older versions of murgaLua (prior to murgaLua.createFltkTimer).

It _should_ work on just about any Linux system, and I assume OSX as well. Windows, though, is not supported. Maybe it will be in the future, but that is not a priority for me.

Code:
#!/bin/murgaLua

--[[

  gui controller for lighttpd (c)2006-2007 mikshaw
  should work with all versions of murgaLua
  
  external commands used: lighttpd, killall, sleep
  
]]

-- lighttpd daemon
lighttpd="/opt/lighttpd/sbin/lighttpd"
-- process ID file
pid="/var/run/lighttpd.pid"
-- config file (can be overidden by LIGHTTPD_CONF variable)
lighttpd_conf="/opt/lighttpd.conf"
-- for command output
tempfile="/tmp/lighttpd.lua.tmp"

Fl:get_system_colors()
Fl:scheme(NULL)

-- test for readable file
function isFile(file)
  whichfile=io.open(file,"r")
  if whichfile then
    whichfile:close()
    return true
  end
end

-- find conf file
lightyvar=os.getenv("LIGHTTPD_CONF")
if lightyvar then
  if isFile(lightyvar) then
    lighttpd_conf=lightyvar
  end
end

-- see if server is running
function checkstatus()
  state:hide() -- prevents label overlap
  if isFile(pid) then
    state:label("server is running")
  else
    state:label("server is not running")
  end
  state:show()
end

function testconfig(prnt)
  outpoot:add("Using configuration file "..lighttpd_conf)
  if os.execute(lighttpd..prnt.." -t -f "..lighttpd_conf.." &>"..tempfile) == 0 then
    return true
  end
end

function start_lighty()
  if testconfig("") then
    outpoot:add("Starting lighttpd...")
    Fl:check()
    if os.execute(lighttpd.." -f "..lighttpd_conf.." &>"..tempfile) == 0 then
      outpoot:add("lighttpd started")
    end
  end
end

function stop_lighty()
  outpoot:add("Shutting down lighttpd...")
  Fl:check()
  if os.execute("killall "..lighttpd.." &>"..tempfile) == 0 then
    outpoot:add("lighttpd stopped")
    os.remove(pid)
  end
end

function lcontrol(self)
  if self:label() == "test config" then
    testconfig(" -p")
  elseif self:label() == "start" then
    start_lighty()
    checkstatus()
  elseif self:label() == "stop" then
    stop_lighty()
    checkstatus()
  elseif self:label() == "restart" then
    stop_lighty()
    Fl:check()
    os.execute("sleep 2")
    start_lighty()
    checkstatus()
  end
  -- show the command output
  filename=io.open(tempfile)
  if filename then
    for line in filename:lines() do outpoot:add(line) end
    outpoot:bottomline(outpoot:size())
    filename:close()
    os.remove(tempfile)
  end
end

-- start interface
bw=80;bh=25;ww=bw*4;wh=bh*7
w=fltk:Fl_Window(ww,wh,"LIGHTTPD Control")
cfg=fltk:Fl_Box(0,bh,ww,bh)
cfg:label("config file: "..lighttpd_conf)
state=fltk:Fl_Box(0,bh*2,ww,bh)
bup=fltk:Fl_Button(0,0,bw,bh,"start");bup:callback(lcontrol)
bdn=fltk:Fl_Button(bw,0,bw,bh,"stop");bdn:callback(lcontrol)
bre=fltk:Fl_Button(bw*2,0,bw,bh,"restart");bre:callback(lcontrol)
bt=fltk:Fl_Button(bw*3,0,bw,bh,"test config");bt:callback(lcontrol)

outpoot=fltk:Fl_Browser(0,bh*3,ww,bh*4)
w:resizable(outpoot)

checkstatus()
w:show()
Fl:run()

Reference URL's