Added if not exists to init query, added parsing for blocked config

This commit is contained in:
Jacob Windle 2019-02-13 15:54:11 -05:00
parent cc2498cddf
commit b10d007b4d

View File

@ -1,22 +1,53 @@
var sqlite3 = require('sqlite3').verbose(); var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database("./proxy.db"); var db = new sqlite3.Database("./proxy.db");
var winston = require("winston");
var fs = require('fs');
// JSON file to configure hosts we should block.
const blockedFile = "./lib/blocked.json"
const logger = winston.createLogger({
transports: [
new winston.transports.File({filename: '../proxy.log'})
]
});
/** TODO - MAKE THESE FUNCTIONS THEIR OWN MODULE. VVV /** TODO - MAKE THESE FUNCTIONS THEIR OWN MODULE. VVV
* FIXME - need to close the database connection * FIXME - need to close the database connection
* on exit. * on exit.
*/ */
function logErrorAndExit(message) {
logger.error(message)
process.exit(1)
}
/** /**
* Initialize our database. * Initialize our database.
*/ */
exports.initializeDatabase = function() { exports.initializeDatabase = function() {
db.serialize(() => { db.serialize(() => {
db.run("create table hosts (hostname TEXT, visitcount Int)", (err, row) => { db.run("create table if not exists hosts (hostname TEXT, visitcount Int)", (err, row) => {
if (err !== undefined || err !== null) { if (err !== null) {
console.log("Error creating database, it likely exists already."); logErrorAndExit("Error creating table hosts: " + err)
} }
}); });
try {
fs.readFile(blockedFile, (err, data) => {
if (err !== null)
throw err
console.log(JSON.parse(data))
})
} catch(err) {
console.log(err)
logErrorAndExit("Could not find blocked.json, does it exist?")
}
}); });
// fs.accessSync("./blocked.json")
} }
/** /**