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.