~lastrosade/getcraft

53e16e83e7f3c5c3f6e3de1fb2b14ec3f13aa764 — Jeremy Lee Shields 3 years ago 7149155
Settings moved to settings.json, routes moved to settings.json, mime_type now specifiable for files.
2 files changed, 25 insertions(+), 10 deletions(-)

M src/index.lua
R src/{routes.json.template => settings.json}
M src/index.lua => src/index.lua +14 -9
@@ 3,9 3,7 @@ if (los.type() ~= "linux") then
	print("This software is meant for linux, you need the GNU find utility")
end

local port = "1337"
local host = "127.0.0.1"
local routeFile = "src/routes.json"
local settingsFile = "src/settings.json"

local json = require('json')
local http = require('http')


@@ 65,7 63,7 @@ end
function readAll(file)
	local f = io.open(file, "rb")
	if (f) then
    	local content = f:read("*all")
        local content = f:read("*all")
		f:close()
		return content
	end


@@ 73,8 71,12 @@ function readAll(file)
	return false
end

local routesText = readAll(routeFile)
local routes = json.decode(routesText).routes
local settings = json.decode(readAll(settingsFile))
local routes = settings.routes
local parameters = settings.parameters

local port = parameters.port
local host = parameters.host

http.createServer(function (req, res)
	local routed = false


@@ 102,7 104,11 @@ http.createServer(function (req, res)
					res:setHeader("Content-Length", #err)
					res:finish(err)
				else
					res:setHeader("Content-Type", "text/plain")
					if route.mime == nil then
						res:setHeader("Content-Type", "text/plain")
					else 
						res:setHeader("Content-Type", route.mime)
					end
					res:setHeader("Content-Length", #content)
					res:finish(content)
				end


@@ 134,8 140,7 @@ http.createServer(function (req, res)
				res:finish(content)

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

R src/routes.json.template => src/settings.json +11 -1
@@ 1,4 1,8 @@
{
    "parameters": {
        "port": 1337,
        "host": "127.0.0.1"
    },
    "routes": [

        // Respond with text


@@ 19,7 23,6 @@
            "type": "directory",
            "res": "src/"
        },

        // Get the possible routes and their actions
        {
            "req": "/rg",


@@ 31,6 34,13 @@
            "type": "route refresh"
        },

        // Optionally specify the mime type of the file
        {
            "req": "/index_mime",
            "type": "file",
            "mime": "text/lua",
            "res": "src/index.lua"
        },
        // Optionally give http status code
        {
            "req": "/500",