From 2e9a8db2fe94ddd3986cab65b16339a725db45cb Mon Sep 17 00:00:00 2001 From: jrtechs Date: Fri, 23 Nov 2018 19:04:40 -0500 Subject: [PATCH 1/3] Fixed issue with mathjax cdm again --- utils/renderBlogPost.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/renderBlogPost.js b/utils/renderBlogPost.js index 324fc6e..34d05ea 100644 --- a/utils/renderBlogPost.js +++ b/utils/renderBlogPost.js @@ -111,7 +111,7 @@ module.exports= //this line prevents older versions of pandoc from including invalid cdm scripts result = result.split("").join(""); - + result = result.split("").join(""); if(blocks == -1) resolve(result); From 7e7d168a8b767fc66e9669250318e34683353f69 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 24 Nov 2018 14:59:38 -0500 Subject: [PATCH 2/3] Updated system to read configuration settings from a centralized config file rather than random files or hard coded parameters. --- config.json | 10 ++++++++++ server.js | 12 +++++------- utils/configLoader.js | 33 +++++++++++++++++++++++++++++++++ utils/sql.js | 24 +++++++++++++++++------- utils/utils.js | 11 +++++++++++ 5 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 config.json create mode 100644 utils/configLoader.js diff --git a/config.json b/config.json new file mode 100644 index 0000000..0e37fb7 --- /dev/null +++ b/config.json @@ -0,0 +1,10 @@ +{ + "PORT": 8000, + + "SESSION_SECRET": "random-data-to-seed-session-data", + + "SQL_HOST": "sql-hostname", + "SQL_DATABASE": "sql-database-name", + "SQL_USER": "sql-user", + "SQL_PASSWORD": "sql-password" +} \ No newline at end of file diff --git a/server.js b/server.js index 3c154b4..834a0b8 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,9 @@ * appropriate pages. */ +// Stores the configuration for the server +const config = require('./utils/configLoader').getConfig(); + //http server const http = require('http'); @@ -31,14 +34,11 @@ const map = require('./utils/generateSiteMap.js'); map.main(); -//port for the server to run on -const port = 8000; - //session data for login const session = require('express-session'); //Initializes sessions for login -app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); +app.use(session({ secret: config.SESSION_SECRET, cookie: { maxAge: 6000000 }})); const projects = ["/steam/"]; @@ -99,6 +99,4 @@ app.use(function(request, result) app.use(compression()); -http.createServer(app).listen(port); - - +http.createServer(app).listen(config.PORT); \ No newline at end of file diff --git a/utils/configLoader.js b/utils/configLoader.js new file mode 100644 index 0000000..db2d3ca --- /dev/null +++ b/utils/configLoader.js @@ -0,0 +1,33 @@ +const utils = require('../utils/utils'); + + +/** + * @author Jeffery Russell 11-24-18 + * + * @type {{main: module.exports.main}} + */ +module.exports= + { + + /** + * + * @returns {*|any} + */ + getConfig: function() + { + const configContents = ["PORT", "SESSION_SECRET", + "SQL_HOST", "SQL_DATABASE", "SQL_PASSWORD"]; + + var config = utils.getFileAsJSON("./config.json"); + + for(var i = 0; i < configContents.length; i++) + { + if(!config.hasOwnProperty(configContents[i])) + { + console.log("Missing config property: " + configContents[i]); + process.exit(1); + } + } + return config; + } + } diff --git a/utils/sql.js b/utils/sql.js index c2521c0..f5d61c9 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -1,20 +1,30 @@ +/** + * Boated file which handles all the SQL + * queries ran by the server + * + * @author Jeffery Russell + */ + const mysql = require('mysql'); +/** Sanitizer to clean user inputs and prevent SQL injections */ const sanitizer = require('sanitizer'); -const Promise = require('promise'); - +/** Crypto package used for hashing */ const crypto = require('crypto'); +/** Used to parse post data */ const qs = require('querystring'); -const utils = require('../utils/utils.js'); +/** Used to load the config file from the disk */ +const config = require('../utils/configLoader').getConfig(); +/** SQL connection */ const con = mysql.createConnection({ - host: "localhost", - user: "blog_user", - password: utils.getFileLine('../sql_secret'), - database: "jrtechs_blog" + host: config.SQL_HOST, + user: config.SQL_USER, + password: config.SQL_PASSWORD, + database: config.SQL_DATABASE }); diff --git a/utils/utils.js b/utils/utils.js index 9af829a..2e3383f 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -51,6 +51,17 @@ module.exports= }, + /** + * + * @param fileName + * @returns {any} + */ + getFileAsJSON: function(fileName) + { + return JSON.parse(fs.readFileSync(fileName, 'utf8')); + }, + + /** * Returns all the contents of a file as a single line * with no break lines. From 170ee2d678161e28cc578cbde15425f53d724380 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 24 Nov 2018 17:20:34 -0500 Subject: [PATCH 3/3] Updated a error in CS theory blog post and updated docs on the server.js file. --- .../media/CSTHEORY/PDAConstruction.svg | 2 +- server.js | 27 ++++++++++--------- utils/sql.js | 1 - 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/blogContent/posts/programming/media/CSTHEORY/PDAConstruction.svg b/blogContent/posts/programming/media/CSTHEORY/PDAConstruction.svg index d106f5d..456784f 100644 --- a/blogContent/posts/programming/media/CSTHEORY/PDAConstruction.svg +++ b/blogContent/posts/programming/media/CSTHEORY/PDAConstruction.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/server.js b/server.js index 834a0b8..9bcac45 100644 --- a/server.js +++ b/server.js @@ -5,39 +5,40 @@ * appropriate pages. */ -// Stores the configuration for the server +/** Stores the configuration for the server */ const config = require('./utils/configLoader').getConfig(); -//http server +/** Port for the server to run on */ +const port = config.PORT; + +/** http server */ const http = require('http'); -//used to parse the request URL +/** used to parse the request URL */ const url = require('url'); -//express app +/** express app */ const express = require("express"); -//express app +/** express app */ const app = express(); -//server side logging +/** server side logging */ const sql = require('./utils/sql'); -//Used for gzip compression +/** Used for gzip compression */ const compression = require('compression'); -//used for file io -const utils = require('./utils/utils.js'); -//Updates the site map whenever the server is started +/**Updates the site map whenever the server is started */ const map = require('./utils/generateSiteMap.js'); map.main(); -//session data for login +/**session data for login */ const session = require('express-session'); -//Initializes sessions for login +/**Initializes sessions for login */ app.use(session({ secret: config.SESSION_SECRET, cookie: { maxAge: 6000000 }})); @@ -99,4 +100,4 @@ app.use(function(request, result) app.use(compression()); -http.createServer(app).listen(config.PORT); \ No newline at end of file +http.createServer(app).listen(port); \ No newline at end of file diff --git a/utils/sql.js b/utils/sql.js index f5d61c9..bce6114 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -27,7 +27,6 @@ const con = mysql.createConnection({ database: config.SQL_DATABASE }); - con.connect(function(err) { if (err) console.log(err);