Browse Source

Updated system to read configuration settings from a centralized config file rather than random files or hard coded parameters.

pull/33/head
Jeffery Russell 6 years ago
parent
commit
7e7d168a8b
5 changed files with 76 additions and 14 deletions
  1. +10
    -0
      config.json
  2. +5
    -7
      server.js
  3. +33
    -0
      utils/configLoader.js
  4. +17
    -7
      utils/sql.js
  5. +11
    -0
      utils/utils.js

+ 10
- 0
config.json View File

@ -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"
}

+ 5
- 7
server.js View File

@ -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);

+ 33
- 0
utils/configLoader.js View File

@ -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;
}
}

+ 17
- 7
utils/sql.js View File

@ -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
});

+ 11
- 0
utils/utils.js View File

@ -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.

Loading…
Cancel
Save