Skip to content

Commit

Permalink
actually use config parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Abraham committed Mar 7, 2012
1 parent 4dca6e9 commit 16c64e5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
17 changes: 8 additions & 9 deletions bin/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ require( "../lib/getRequest" )
require( "../lib/answerRequest" )
require( "../lib/readConf" )

local socket = require( "socket" )

local server = assert( socket.bind( "127.0.0.1", "9035" ) )
readConfig()

local socket = require( "socket" )
local server = assert( socket.bind( getIP(), getPort() ) )
local ip, port = server:getsockname()

local selectlist = {}
readConfig()
getConf()

while true do
local client = server:accept()
Expand All @@ -23,11 +20,12 @@ while true do
table.insert( selectlist, client )
end

list, _, err = socket.select( selectlist, nil, 0.1 )
readlist, _, err = socket.select( selectlist, nil, 0.1 )

if err then print( err ) end

for i, cli in ipairs( list ) do
for i, cli in ipairs( readlist ) do
-- request stores the header + body
local request = {}

while true do
Expand Down Expand Up @@ -55,14 +53,15 @@ while true do
client:send( answerRequest( getList[2] ) )
end
end

if request.POST ~= nil then
local getList = getRequest( request.POST, " " )
if getList[1] ~= nil then
client:send( answerRequest( getList[2] ) )
end
end

--Debug Information
-- Debug Information prints complete request
--for k,l in pairs( request ) do
-- print( " key: " .. k .. " value: " .. l )
--end
Expand Down
1 change: 1 addition & 0 deletions config/bad.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
root /home/roa/programming/badmoonrising
port 9035
listen 0.0.0.0
4 changes: 2 additions & 2 deletions lib/answerRequest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
function answerRequest ( request )
local fileStatus = 0;
request = cutTrailingSlash( request )
local fh , err = io.open( "./" .. request, "r" )
local fh , err = io.open( "./content/" .. request, "r" )

if fh == nil then
fileStatus = 1;
fh , err = io.open( "./error.html", "r" )
fh , err = io.open( "./content/error.html", "r" )
return prepareHeader( fileStatus ) .. prepareContent( fh )
else
return prepareHeader( fileStatus ) .. prepareContent( fh )
Expand Down
31 changes: 28 additions & 3 deletions lib/readConf.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/lua

require( "lfs" )

local workDir = lfs.currentdir()

config = {}

function readConfig ()
confFile = "/home/roa/programming/badmoonrising/config/bad.conf"
confFile = workDir .. "/config/bad.conf"
io.input( confFile )

while true do
Expand All @@ -27,10 +31,13 @@ function cutLine ( line )
key = line:sub( 0, e -1 )
value = line:sub( e + 1 )
end

if key == "root" then
validateRoot( key, value )
elseif key == "port" then
validatePort( key, value )
elseif key == "listen" then
validateIP( key, value)
else
return nil
end
Expand All @@ -44,8 +51,26 @@ function validatePort ( key, value )
config[key] = value;
end

function getConf ()
function validateIP ( key, value )
config[key] = value;
end

function getConf ( key )
for k, v in pairs(config) do
print("key: " .. k .. " value: " .. v)
if k == key then
return v
end
end
end

function getRoot ()
return getConf( "root" )
end

function getPort ()
return getConf( "port")
end

function getIP ()
return getConf( "listen" )
end

0 comments on commit 16c64e5

Please sign in to comment.