diff --git a/lib/db.js b/lib/db.js index b67e444..0967c52 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,22 +1,53 @@ var sqlite3 = require('sqlite3').verbose(); 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 * FIXME - need to close the database connection * on exit. */ +function logErrorAndExit(message) { + logger.error(message) + process.exit(1) +} + /** * Initialize our database. */ exports.initializeDatabase = function() { db.serialize(() => { - db.run("create table hosts (hostname TEXT, visitcount Int)", (err, row) => { - if (err !== undefined || err !== null) { - console.log("Error creating database, it likely exists already."); + db.run("create table if not exists hosts (hostname TEXT, visitcount Int)", (err, row) => { + if (err !== null) { + 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") } /**