From 60db0f6440e827d719a4ed1df00bff96656163e5 Mon Sep 17 00:00:00 2001 From: "jeremy.shields" Date: Wed, 7 Nov 2018 09:43:49 +0100 Subject: [PATCH] more err and routing options --- src/index.lua | 51 +++++++++++++++++++++++----------------- src/routes.json.template | 33 ++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 27 deletions(-) diff --git a/src/index.lua b/src/index.lua index d5635e4..1cff0cb 100644 --- a/src/index.lua +++ b/src/index.lua @@ -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) diff --git a/src/routes.json.template b/src/routes.json.template index 0c9f933..5b57460 100644 --- a/src/routes.json.template +++ b/src/routes.json.template @@ -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 -- 2.45.2