diff --git a/index.js b/index.js index 63ee0c9..82dab04 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,7 @@ var http = require('http'); var winston = require("winston"); var fs = require("fs"); -var sqlite3 = require('sqlite3').verbose(); -var db = new sqlite3.Database("./proxy.db"); +var myDb = require("./lib/db.js"); const url = require("url"); // Rudimentary logger. @@ -38,7 +37,7 @@ const processRequest = (request, response) => { logger.info({message: "Requesting: " + req.url}); const hostUrl = new url.URL(req.url); - visitHost(hostUrl.hostname); + myDb.visitHost(hostUrl.hostname); // Complicated line of JS to forward request, and pipe it to the // response object. req.pipe(http.request(req.url, (resp) => {resp.pipe(res)})); @@ -57,54 +56,6 @@ function startServer() { return server; } -/** TODO - MAKE THESE FUNCTIONS THEIR OWN MODULE. VVV - * FIXME - need to close the database connection - * on exit. - */ - -/** - * Initialize our database. - */ -function initializeDatabase() { - 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.close(); -} - -/** - * Update the visit count for a host logged by the proxy - * @param {string} hostname Host logged by proxy - */ -function updateVisitCount(hostname) { - db.serialize(() => { - db.run("update hosts set visitcount = visitcount + 1 where hostname = ?", [hostname]); - }); - // db.close(); -} - -function addHostToTable(hostname) { - db.serialize(() => { - db.run("insert into hosts (hostname, visitcount) values (?, 0)", [hostname]) - }) - // db.close(); -} - -function visitHost(hostname) { - return db.serialize(() => { - db.get("select * from hosts where hostname = ?", [hostname], (err, row) => { - if (row === undefined) { - addHostToTable(hostname); - } else { - updateVisitCount(hostname); - } - }) - }); -} - -initializeDatabase(); +myDb.initializeDatabase(); startServer(); + diff --git a/lib/db.js b/lib/db.js new file mode 100644 index 0000000..e6babf8 --- /dev/null +++ b/lib/db.js @@ -0,0 +1,56 @@ +var sqlite3 = require('sqlite3').verbose(); +var db = new sqlite3.Database("./proxy.db") + +/** TODO - MAKE THESE FUNCTIONS THEIR OWN MODULE. VVV + * FIXME - need to close the database connection + * on exit. + */ + +/** + * 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."); + } + }); + }); +} + +/** + * Update the visit count for a host logged by the proxy + * @param {string} hostname Host logged by proxy + */ +function updateVisitCount(hostname) { + db.serialize(() => { + db.run("update hosts set visitcount = visitcount + 1 where hostname = ?", [hostname]); + }); +} + +/** + * Add a row for a host to the hosts table. + * @param {string} hostname Host that was freshly visited. + */ +function addHostToTable(hostname) { + db.serialize(() => { + db.run("insert into hosts (hostname, visitcount) values (?, 0)", [hostname]) + }) +} + +/** + * Either initialize a row in the database, or update the visit count for a row. + * @param {string} hostname Host that was visited + */ +exports.visitHost = function (hostname) { + return db.serialize(() => { + db.get("select * from hosts where hostname = ?", [hostname], (err, row) => { + if (row === undefined) { + addHostToTable(hostname); + } else { + updateVisitCount(hostname); + } + }) + }); +}