~lastrosade/getcraft

60db0f6440e827d719a4ed1df00bff96656163e5 — jeremy.shields 5 years ago dc953ee
more err and routing options
2 files changed, 57 insertions(+), 27 deletions(-)

M src/index.lua
M src/routes.json.template
M src/index.lua => src/index.lua +29 -22
@@ 73,18 73,22 @@ function readAll(file)
	return false
end

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

http.createServer(function (req, res)
	res:setHeader("Access-Control-Allow-Origin", "*")

	local routesText = readAll(routeFile)
	local routes = json.decode(routesText).routes
	local routed = false
	res:setHeader("Access-Control-Allow-Origin", "*")

	for _,route in pairs(routes) do

		if (req.url == route.req) then

			routed = true
			if (route.status) then
				vardump(route.status)
				res:writeHead(route.status, {})
			end

			if (route.type == "text") then
				res:setHeader("Content-Type", "text/plain")


@@ 107,8 111,8 @@ http.createServer(function (req, res)
			elseif (route.type == "directory") then
				local files = scandir(route.res)
				if (files == false) then
					res:writeHead(404, {})
					err = "404, Directory is empty or missing"
					res:writeHead(404, {})
					res:setHeader("Content-Length", #err)
					res:finish(err)
				else


@@ 117,7 121,6 @@ http.createServer(function (req, res)
						array[i] = {}
						array[i].filename = f:gsub(route.res, "")
						array[i].content = readAll(f)
						array[i].size = #array[i].content
					end
					content = json.encode(array)
					res:setHeader("Content-Type", "text/plain")


@@ 125,28 128,32 @@ http.createServer(function (req, res)
					res:finish(content)
				end

			elseif (route.type == "route get") then
				content = json.encode(routes)
				res:setHeader("Content-Type", "text/plain")
				res:setHeader("Content-Length", #content)
				res:finish(content)

			elseif (route.type == "route refresh") then
				routesText = readAll(routeFile)
				routes = json.decode(routesText).routes
				err = "201, Routes file refreshed"
				res:writeHead(201, {})
				res:setHeader("Content-Length", #err)
				res:finish(err)

			else
				err = "500, Server error. Probably bad routing as route type did not match anything"
				res:writeHead(500, {})
				-- '" .. route.type .. "' 
				res:setHeader("Content-Length", #err)
				res:finish(err)
			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 (routed == false) then
		res:writeHead(404, {})
		res:setHeader("Content-Length", 0)

M src/routes.json.template => src/routes.json.template +28 -5
@@ 1,19 1,42 @@
{
    "routes": [
        {
            "req": "/index",
            "type": "file",
            "res": "src/index.lua"
        },

        // Respond with text
        {
            "req": "/text",
            "type": "text",
            "res": "text"
        },
        // Respond with the content of a file
        {
            "req": "/index",
            "type": "file",
            "res": "src/index.lua"
        },
        // Respond with a json containing file names and content
        {
            "req": "/src",
            "type": "directory",
            "res": "src/"
        },

        // Get the possible routes and their actions
        {
            "req": "/rg",
            "type": "route get"
        },
        // Refresh the routes file
        {
            "req": "/rr",
            "type": "route refresh"
        },

        // Optionally give http status code
        {
            "req": "/500",
            "type": "text",
            "status": 500,
            "res": "oh no"
        }
    ]
}
\ No newline at end of file