~lastrosade/getcraft

674aff1d7707c2da07830d5dae2400b6acdebfae — jeremy.shields 5 years ago bb63c92
dirs
1 files changed, 34 insertions(+), 17 deletions(-)

M src/index.lua
M src/index.lua => src/index.lua +34 -17
@@ 60,10 60,11 @@ function readAll(file)
    return content
end

routesText = readAll(routeFile)
routes = json.decode(routesText).routes

http.createServer(function (req, res)
	
	local routesText = readAll(routeFile)
	local routes = json.decode(routesText).routes
	local routed = false

	for _,route in pairs(routes) do


@@ 81,27 82,43 @@ http.createServer(function (req, res)
				res:setHeader("Content-Type", "text/plain")
				res:setHeader("Content-Length", #content)
				res:finish(content)

			elseif (route.type == "directory") then
				local files = scandir(route.res)
				local array = {}
				for i,f in pairs(files) do
					array[i] = {}
					array[i].filename = f
					array[i].content = readAll(f)
					array[i].size = #array[i].content
				end
				vardump(array)
				content = json.encode(array)
				res:setHeader("Content-Type", "text/plain")
				res:setHeader("Content-Length", #content)
				res:finish(content)

			end

			break
		end
	end

	if (req.url == "/") then
		routed = true

		local files = scandir('src/')
		body = ""
		for i,f in pairs(files) do
			local fi = readAll(f)
			-- print(f .. " - " .. #fi)
			body = body .. fi .. "\n\n"
		end

		res:setHeader("Content-Type", "text/plain")
		res:setHeader("Content-Length", #body)
		res:finish(body)
	end
	-- if (req.url == "/") then
	-- 	routed = true

	-- 	local files = scandir('src/')
	-- 	body = ""
	-- 	for i,f in pairs(files) do
	-- 		local fi = readAll(f)
	-- 		-- print(f .. " - " .. #fi)
	-- 		body = body .. fi .. "\n\n"
	-- 	end

	-- 	res:setHeader("Content-Type", "text/plain")
	-- 	res:setHeader("Content-Length", #body)
	-- 	res:finish(body)
	-- end

	if (routed == false) then
		res:writeHead(404, {})