2015-06-16 22:52:06 -04:00
|
|
|
var express = require("express");
|
2015-06-25 14:05:20 -04:00
|
|
|
var hbs = require('express-hbs');
|
2015-06-16 22:52:06 -04:00
|
|
|
var fs = require("fs");
|
|
|
|
var bodyParser = require("body-parser");
|
|
|
|
var app = express();
|
2015-06-21 14:04:07 -04:00
|
|
|
var logfile = fs.createWriteStream('./db/log.log', {flags: 'a'});
|
|
|
|
|
|
|
|
|
|
|
|
var file = "./db/test.db";
|
|
|
|
var exists = fs.existsSync(file);
|
|
|
|
|
|
|
|
if(!exists) {
|
|
|
|
console.log("Creating DB file.");
|
|
|
|
fs.openSync(file, "w");
|
|
|
|
}
|
|
|
|
|
|
|
|
var sqlite3 = require("sqlite3").verbose();
|
|
|
|
var db = new sqlite3.Database(file);
|
|
|
|
|
|
|
|
db.serialize(function() {
|
|
|
|
if(!exists) {
|
2015-06-22 12:16:56 -04:00
|
|
|
db.run(
|
|
|
|
"CREATE TABLE Alerts (" +
|
|
|
|
"origJSON TEXT," +
|
|
|
|
"coreId TEXT," +
|
|
|
|
"locationDesc TEXT," +
|
|
|
|
"status TEXT," +
|
|
|
|
"published_at TEXT)"
|
|
|
|
);
|
2015-06-21 14:04:07 -04:00
|
|
|
}
|
|
|
|
});
|
2015-06-16 22:52:06 -04:00
|
|
|
|
|
|
|
app.use(bodyParser.json());
|
2015-06-25 14:05:20 -04:00
|
|
|
// Use `.hbs` for extensions and find partials in `views/partials`.
|
|
|
|
app.engine('hbs', hbs.express4({
|
|
|
|
partialsDir: __dirname + '/views/partials'
|
|
|
|
}));
|
|
|
|
app.set('view engine', 'hbs');
|
|
|
|
app.set('views', __dirname + '/views');
|
2015-06-16 22:52:06 -04:00
|
|
|
|
|
|
|
app.get('/', function(req, res){
|
|
|
|
//res.sendFile("/usr/src/app/index.html");
|
2015-06-25 14:05:20 -04:00
|
|
|
//fs.createReadStream('./log.log').pipe(res);
|
2015-06-16 22:52:06 -04:00
|
|
|
console.log("got a GET request...");
|
2015-06-25 14:05:20 -04:00
|
|
|
db.all("SELECT coreId, published_at FROM Alerts", function(err, rows){
|
|
|
|
console.log(rows);
|
|
|
|
res.render('home', {alerts: rows}, function(err, html) {
|
|
|
|
res.send(html);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
//res.send;
|
2015-06-16 22:52:06 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/', function(req, res){
|
|
|
|
var postEvent = req.body.postEvent;
|
|
|
|
var source = req.body.source;
|
2015-06-22 12:16:56 -04:00
|
|
|
//console.log("Post event = "+postEvent+", source is "+source);
|
2015-06-16 22:52:06 -04:00
|
|
|
console.log(req.body);
|
2015-06-21 14:04:07 -04:00
|
|
|
|
2015-06-22 12:16:56 -04:00
|
|
|
var stmt = db.prepare("INSERT INTO Alerts (OrigJSON, coreid, published_at) VALUES (?, ?, ?)");
|
|
|
|
stmt.run(
|
|
|
|
JSON.stringify(req.body, null, 4),
|
|
|
|
JSON.stringify(req.body.coreid, null, 4),
|
|
|
|
JSON.stringify(req.body.published_at, null, 4)
|
|
|
|
);
|
2015-06-21 14:04:07 -04:00
|
|
|
stmt.finalize();
|
2015-06-16 22:52:06 -04:00
|
|
|
//res.send(JSON.stringify(req.body, null, 4));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(3000, function() {
|
|
|
|
console.log("Started on PORT 3000");
|
|
|
|
})
|