111 lines
3.1 KiB
JavaScript
111 lines
3.1 KiB
JavaScript
var express = require("express");
|
|
var hbs = require('express-hbs');
|
|
var nodemailer = require('nodemailer');
|
|
var fs = require("fs");
|
|
var bodyParser = require("body-parser");
|
|
var app = express();
|
|
var logfile = fs.createWriteStream('./db/log.log', {flags: 'a'});
|
|
|
|
// Setup email
|
|
var transporter = nodemailer.createTransport({
|
|
service: 'Gmail',
|
|
auth: {
|
|
user: 'jkU8K5lrog9w@gmail.com',
|
|
pass: 'NSLQZ~d6Zb~LLv8#t'
|
|
}
|
|
});
|
|
var mailOptions = {
|
|
from: 'Alert Monitor <jkU8K5lrog9w@gmail.com>',
|
|
to: 'jody@kaplon.us',
|
|
subject: 'Alert received',
|
|
text: 'test alert'
|
|
};
|
|
|
|
|
|
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) {
|
|
db.run(
|
|
"CREATE TABLE Alerts (" +
|
|
"origJSON TEXT," +
|
|
"coreId TEXT," +
|
|
"coreName TEXT," +
|
|
"locationDesc TEXT," +
|
|
"status TEXT," +
|
|
"published_at TEXT)"
|
|
);
|
|
}
|
|
});
|
|
|
|
app.use(bodyParser.json());
|
|
// 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');
|
|
|
|
app.get('/', function(req, res){
|
|
db.all("SELECT coreId, coreName, published_at FROM Alerts GROUP BY coreId, coreName, published_at;", function(err, rows){
|
|
//console.log(rows);
|
|
res.render('index', {cores: rows}, function(err, html) {
|
|
res.send(html);
|
|
});
|
|
});
|
|
|
|
// not sure if this ':id' notaction is correct
|
|
app.get('/core/:id', function(req, res){
|
|
//res.sendFile("/usr/src/app/index.html");
|
|
//fs.createReadStream('./log.log').pipe(res);
|
|
var d = new Date();
|
|
console.log("GET request at, " + JSON.stringify(d, 4));
|
|
// add WHERE clause w/core ID value from URL
|
|
db.all("SELECT coreId, published_at FROM Alerts ORDER BY published_at DESC LIMIT 30;", function(err, rows){
|
|
//console.log(rows);
|
|
res.render('core', {alerts: rows}, function(err, html) {
|
|
res.send(html);
|
|
});
|
|
});
|
|
//res.send;
|
|
});
|
|
|
|
app.post('/', function(req, res){
|
|
var postEvent = req.body.postEvent;
|
|
var source = req.body.source;
|
|
//console.log("Post event = "+postEvent+", source is "+source);
|
|
console.log(req.body);
|
|
|
|
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)
|
|
);
|
|
stmt.finalize();
|
|
|
|
// Emails were working...don't need to send on every POST anymore.
|
|
//transporter.sendMail(mailOptions, function(error, info){
|
|
//if(error){
|
|
//console.log(error);
|
|
//}else{
|
|
//console.log('Message sent: ' + info.response);
|
|
//}
|
|
//});
|
|
|
|
//res.send(JSON.stringify(req.body, null, 4));
|
|
});
|
|
|
|
app.listen(3000, function() {
|
|
console.log("Started on PORT 3000");
|
|
})
|