2016-02-16 17:52:38 -05:00
|
|
|
var express = require("express");
|
|
|
|
var hbs = require('express-hbs');
|
|
|
|
require('handlebars-form-helpers').register(hbs.handlebars);
|
|
|
|
var bodyParser = require("body-parser");
|
|
|
|
var app = express();
|
|
|
|
var winston = require('winston');
|
|
|
|
winston.add(winston.transports.File, { filename: './logs/notes.kaplon.us.log', maxsize: 5000000 }); // 5MB
|
|
|
|
var fileSystem = require('fs');
|
|
|
|
|
2016-02-18 16:51:51 -05:00
|
|
|
app.use(bodyParser.text()); // Use defaults for now, size limit is 100kb.
|
2016-02-16 17:52:38 -05: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');
|
2016-02-18 17:34:25 -05:00
|
|
|
app.use(express.static('codemirror'));
|
2016-02-16 17:52:38 -05:00
|
|
|
|
2016-02-18 16:51:51 -05:00
|
|
|
var notePath = __dirname + '/note-data/allNotes.txt';
|
2016-02-16 17:52:38 -05:00
|
|
|
|
|
|
|
app.get('/', function(req, res){
|
|
|
|
winston.info("GET /");
|
2016-02-18 17:38:31 -05:00
|
|
|
// Get curent text from allNotes.txt and pass that data to handlebars template.
|
2016-02-18 16:51:51 -05:00
|
|
|
fileSystem.readFile(notePath, {encoding: 'utf-8'}, function(err,data){
|
|
|
|
if (!err){
|
|
|
|
winston.info('successful file read');
|
|
|
|
res.render('index', {notetxt: data}, function(err, html) {
|
|
|
|
if(err !== null) {
|
|
|
|
winston.error(err);
|
|
|
|
} else {
|
|
|
|
res.send(html);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}else{
|
2016-02-16 17:52:38 -05:00
|
|
|
winston.error(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/', function(req, res){
|
2016-02-18 16:51:51 -05:00
|
|
|
winston.info('POST received');
|
2016-02-21 09:42:29 -05:00
|
|
|
var now = Date.now();
|
2016-02-18 16:51:51 -05:00
|
|
|
// quick/dirty poc is to do a fileSystem.write to create a new file w/contents of req.body!
|
2016-02-18 18:03:22 -05:00
|
|
|
//fileSystem.writeFile(__dirname + '/note-data/test-' + now + '.txt', req.body, function(err){
|
|
|
|
//if (err) { winston.error(err); }
|
|
|
|
//winston.info('new test file written');
|
|
|
|
//});
|
|
|
|
// Overwrite allNotes.txt with new contents from client.
|
|
|
|
fileSystem.readFile(notePath, 'utf-8', function(err, data){
|
2016-02-21 09:42:29 -05:00
|
|
|
if (err) {
|
|
|
|
winston.error(err);
|
|
|
|
} else {
|
|
|
|
fileSystem.writeFile(notePath, req.body, 'utf-8', function(err) {
|
|
|
|
if (err) { winston.error(err); }
|
|
|
|
winston.info('new contents from client written to allNotes.txt');
|
|
|
|
var exec = require('child_process').exec;
|
|
|
|
var cmd = 'cd note-data && git commit -am "Notes modified via POST request, ' + now + '"';
|
|
|
|
winston.info(cmd);
|
|
|
|
exec(cmd, function(error, stdout, stderr) {
|
|
|
|
if (error) { winston.error(error); }
|
|
|
|
winston.info(stdout);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-02-18 16:51:51 -05:00
|
|
|
});
|
2016-02-18 18:03:22 -05:00
|
|
|
// Stage and commit changes to allNotes.txt.
|
2016-02-16 17:52:38 -05:00
|
|
|
res.status(204).send('POST received');
|
|
|
|
});
|
|
|
|
|
2016-02-22 17:37:11 -05:00
|
|
|
// This is broken due to permission error from bitbucket.
|
|
|
|
// Not sure how to auth container git user to bitbucket...comment it all out for now.
|
|
|
|
//setInterval(function() {
|
|
|
|
//// Push to bitbucker every 30min.
|
|
|
|
//var exec = require('child_process').exec;
|
|
|
|
//var cmd = 'cd note-data && git push';
|
|
|
|
//winston.info(cmd);
|
|
|
|
//exec(cmd, function(error, stdout, stderr) {
|
|
|
|
//if (error) { winston.error(error); }
|
|
|
|
//winston.info(stdout);
|
|
|
|
//});
|
|
|
|
//}, 30 * 60 * 1000);
|
|
|
|
|
2016-02-16 17:52:38 -05:00
|
|
|
app.listen(3000, function() {
|
|
|
|
winston.info("Started on PORT 3000");
|
|
|
|
});
|