From 53e16e83e7f3c5c3f6e3de1fb2b14ec3f13aa764 Mon Sep 17 00:00:00 2001 From: Jeremy Lee Shields Date: Tue, 5 May 2020 07:40:05 +0200 Subject: [PATCH] Settings moved to settings.json, routes moved to settings.json, mime_type now specifiable for files. --- src/index.lua | 23 +++++++++++++-------- src/{routes.json.template => settings.json} | 12 ++++++++++- 2 files changed, 25 insertions(+), 10 deletions(-) rename src/{routes.json.template => settings.json} (76%) diff --git a/src/index.lua b/src/index.lua index 433044e..0e3f07d 100644 --- a/src/index.lua +++ b/src/index.lua @@ -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) diff --git a/src/routes.json.template b/src/settings.json similarity index 76% rename from src/routes.json.template rename to src/settings.json index 5b57460..db6c247 100644 --- a/src/routes.json.template +++ b/src/settings.json @@ -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", -- 2.45.2