Split database code into it's own module
This commit is contained in:
parent
e26986770d
commit
7dd14cbd06
57
index.js
57
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();
|
||||
|
||||
|
56
lib/db.js
Normal file
56
lib/db.js
Normal file
@ -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);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user