Added if not exists to init query, added parsing for blocked config
This commit is contained in:
parent
cc2498cddf
commit
b10d007b4d
37
lib/db.js
37
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")
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user