|
@ -1,91 +1,48 @@ |
|
|
/** |
|
|
|
|
|
* Main server file for the blog. This file is responsible for |
|
|
|
|
|
* creating the server and listening for clients. The main run |
|
|
|
|
|
* function parses the url and calls a sub module to make the |
|
|
|
|
|
* appropriate pages. |
|
|
|
|
|
* |
|
|
|
|
|
* @author Jeffery Russell 7-21-18 |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
//http server
|
|
|
|
|
|
const http = require('http'); |
|
|
|
|
|
|
|
|
|
|
|
//parsing request url
|
|
|
|
|
|
const url = require('url'); |
|
|
|
|
|
|
|
|
|
|
|
//express app
|
|
|
|
|
|
const express = require("express"); |
|
|
|
|
|
const app = express(); |
|
|
|
|
|
|
|
|
|
|
|
//session data for login
|
|
|
|
|
|
const session = require('express-session'); |
|
|
|
|
|
|
|
|
|
|
|
//sending static content
|
|
|
//sending static content
|
|
|
const includes = require('../includes/includes.js'); |
|
|
const includes = require('../includes/includes.js'); |
|
|
|
|
|
|
|
|
//used for file io
|
|
|
|
|
|
const utils = require('../utils/utils.js'); |
|
|
|
|
|
|
|
|
|
|
|
//cache -- only used for static contents
|
|
|
|
|
|
const cache = require('memory-cache'); |
|
|
|
|
|
|
|
|
|
|
|
//Initializes sessions for login
|
|
|
|
|
|
app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); |
|
|
|
|
|
|
|
|
|
|
|
//port to listen for the admin server on
|
|
|
|
|
|
const port = 8001; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//used to append static content to result
|
|
|
|
|
|
const contentLoader = require('../includes/staticContentServer.js'); |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Parses the request url and calls correct JS files |
|
|
|
|
|
|
|
|
* @author Jeffery Russell 11-3-18 |
|
|
|
|
|
* |
|
|
|
|
|
* @type {{main: (function(*=, *): Promise)}} |
|
|
*/ |
|
|
*/ |
|
|
app.use(function(request, result) |
|
|
|
|
|
{ |
|
|
|
|
|
//prevents people from pointing their dns at my IP:port for my site
|
|
|
|
|
|
if(request.headers.host.includes("localhost:" + port) || |
|
|
|
|
|
request.headers.host.includes("jrtechs.net")) |
|
|
|
|
|
|
|
|
module.exports= |
|
|
{ |
|
|
{ |
|
|
const filename = url.parse(request.url, true).pathname; |
|
|
|
|
|
|
|
|
|
|
|
//handles image requests
|
|
|
|
|
|
if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png")) |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Calls posts and sidebar modules to render blog contents in order |
|
|
|
|
|
* |
|
|
|
|
|
* @param requestURL |
|
|
|
|
|
* @returns {Promise|*} |
|
|
|
|
|
*/ |
|
|
|
|
|
main: function(request, result, filename) |
|
|
{ |
|
|
{ |
|
|
includes.sendJS(result, filename, cache); |
|
|
|
|
|
} |
|
|
|
|
|
else if(filename.includes("/css/") || filename.includes(".woff2")) |
|
|
|
|
|
{ |
|
|
|
|
|
includes.sendCSS(result, filename, cache) |
|
|
|
|
|
} |
|
|
|
|
|
else if(filename.includes("/js/") || filename.includes(".js")) |
|
|
|
|
|
{ |
|
|
|
|
|
includes.sendJS(result, filename, cache); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
result.writeHead(200, {'Content-Type': 'text/html'}); |
|
|
|
|
|
|
|
|
if(contentLoader.serveStaticContent(request, result, filename, "")) |
|
|
|
|
|
{ |
|
|
|
|
|
//do nothing
|
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
result.writeHead(200, {'Content-Type': 'text/html'}); |
|
|
|
|
|
|
|
|
const file = "./admin/admin.js"; |
|
|
|
|
|
|
|
|
const file = "../admin/admin.js"; |
|
|
|
|
|
|
|
|
Promise.all([includes.printAdminHeader(), |
|
|
|
|
|
require(file).main(request), |
|
|
|
|
|
includes.printFooter()]).then(function(content) |
|
|
|
|
|
{ |
|
|
|
|
|
result.write(content.join('')); |
|
|
|
|
|
result.end(); |
|
|
|
|
|
|
|
|
Promise.all([includes.printAdminHeader(), |
|
|
|
|
|
require(file).main(request), |
|
|
|
|
|
includes.printFooter()]).then(function(content) |
|
|
|
|
|
{ |
|
|
|
|
|
result.write(content.join('')); |
|
|
|
|
|
result.end(); |
|
|
|
|
|
|
|
|
}).catch(function(err) |
|
|
|
|
|
{ |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
throw err; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
// utils.printWrongHost(result);
|
|
|
|
|
|
result.writeHead(418, {}); |
|
|
|
|
|
result.end(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
}).catch(function(err) |
|
|
|
|
|
{ |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
throw err; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
http.createServer(app).listen(port); |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
}; |