From 1d3aa73137d8122feaa910b8130154d70f7abb75 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Fri, 8 Apr 2022 22:24:41 +0200 Subject: [PATCH] Made the webroot directory configurable --- config.toml | 13 ++++++++++--- server.js | 6 +++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/config.toml b/config.toml index 289a67a..d07187c 100644 --- a/config.toml +++ b/config.toml @@ -1,5 +1,12 @@ # Sellery configuration file +# Serve files from this directory (Path must be absolute) +# Use $WORKING_DIR to subsitute the current working directory +# So, if you want to server relative paths, use: +# $WORKING_DIR/relative/path +# Paths should NOT end with a '/'! +WEBROOT = '$WORKING_DIR' + # This is the SHA512 digest to check the password against PASSWD_HASH = 'a3c1443b087cf5338d3696f6029fdf791ee4829a27e19c9f257a06ca0d88b5b518ac9868bb13199e807553bda62d3dc15b6354862f34fcab0a7c4c45530349ea' @@ -9,7 +16,7 @@ SESSION_DURATION = 600 # The port the webserver runs on WEBSERVER_PORT = 443 -# SSL key and certificate (you have to generate these yourself) -SSL_KEY_FILE = "key.pem" -SSL_CERT_FILE = "cert.pem" +# SSL key and certificate (you have to generate these yourself!) +SSL_KEY_FILE = 'key.pem' +SSL_CERT_FILE = 'cert.pem' diff --git a/server.js b/server.js index d970d14..7129183 100755 --- a/server.js +++ b/server.js @@ -8,6 +8,10 @@ var toml = require('toml'); // Parse config file const config = toml.parse(fs.readFileSync('config.toml', 'utf-8')); +// Compose webroot directory +const webrootDir = config.WEBROOT + .replace('$WORKING_DIR', __dirname); + // Just a few mime types const contentTypes = { '.html': 'text/html', @@ -181,7 +185,7 @@ function testAuthentication(request, response) { //! This function just serves files as they are... function serverStaticFiles(request, response) { // Fetch requested file - fs.readFile(__dirname + request.url, function (error, data) { + fs.readFile(webrootDir + request.url, function (error, data) { if(!error) { const mimetype = path.extname(request.url); if (!(typeof mimetype === 'undefined')) {