var express = require("express"); var hbs = require('express-hbs'); var nodemailer = require('nodemailer'); var moment = require('moment-timezone'); 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 ', 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){ var d = new Date(); console.log("GET /, " + JSON.stringify(d, 4)); db.all("SELECT coreId, coreName, locationDesc FROM Alerts GROUP BY coreId, coreName, locationDesc;", function(err, rows){ if(err !== null) { console.log(err); } else { //console.log(rows); res.render('index', {cores: rows}, function(err, html) { if(err !== null) { console.log(err); } else { res.send(html); } }); } }); }); app.get('/core/:id', function(req, res){ //res.sendFile("/usr/src/app/index.html"); //fs.createReadStream('./log.log').pipe(res); var d = new Date(); var coreId = req.params.id; console.log("GET /core/" + coreId + ", " + JSON.stringify(d, 4)); db.all("SELECT coreId, published_at, status, coreName FROM Alerts WHERE coreId = '" + coreId + "' ORDER BY published_at DESC LIMIT 30;", function(err, rows){ if(err !== null) { console.log(err); } else { //console.log("SELECT coreId, published_at FROM Alerts WHERE coreId = '" + coreId + "' ORDER BY published_at DESC LIMIT 30;"); //console.log(rows); // Loop over elements in rows array, convert ugly UTC times to pretty local times. rows.forEach(function(row){ var localDtTm = moment.utc((row.published_at || "").replace(/-/g,"/").replace(/[TZ]/g," ")).toDate(); localDtTm = moment(localDtTm).tz('America/New_York').format('MM-DD-YYYY HH:mm:ss'); row.published_at = localDtTm; }); 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"); })