2016-03-08 21:10:12 +00:00
|
|
|
"use strict";
|
|
|
|
/**
|
|
|
|
* Created by Martin on 22/02/2016.
|
|
|
|
*/
|
|
|
|
var express = require('express');
|
|
|
|
var http = require('http'), request = require('request'), cheerio = require('cheerio'), util = require('util');
|
|
|
|
var jsonfile = require('jsonfile'), fs = require('fs'), STRING = require('string');
|
|
|
|
var markdown = require( "markdown" ).markdown;
|
|
|
|
var log4js = require('log4js');
|
|
|
|
var logger = log4js.getLogger();
|
|
|
|
|
|
|
|
var router = express.Router();
|
|
|
|
|
|
|
|
var EventEmitter = require('events');
|
|
|
|
|
2016-05-12 14:30:22 +00:00
|
|
|
var authentication =require('basic-authentication')();
|
|
|
|
|
|
|
|
var authOptions = {
|
|
|
|
hash: 'sha256', // type of hash
|
|
|
|
file: 'htpasswd.txt', // path of file
|
|
|
|
suppress: true // suppress throwing Error if wrong user
|
|
|
|
};
|
|
|
|
|
2016-04-28 11:02:32 +00:00
|
|
|
var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
|
2016-03-08 21:10:12 +00:00
|
|
|
var busEmitter = new EventEmitter();
|
|
|
|
|
|
|
|
var db_name = 'logger';
|
|
|
|
var dbCouch = nano.use(db_name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function insertLog(obj) {
|
|
|
|
logger.debug('Inserting into couch...');
|
|
|
|
logger.info(util.inspect(obj));
|
|
|
|
var newObj = {body:obj};
|
|
|
|
dbCouch.insert(newObj, function(err, body,header) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Error inserting into couch');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
logger.debug('Insert done..');
|
|
|
|
}
|
|
|
|
|
|
|
|
var doInsertLog = (obj) =>{
|
|
|
|
// logger.info('sendSocket: ' + JSON.stringify(obj));
|
|
|
|
insertLog(obj);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Events
|
|
|
|
busEmitter.on('saveLog', doInsertLog);
|
|
|
|
|
|
|
|
|
2016-05-12 14:30:22 +00:00
|
|
|
router.get('/', authentication(authOptions), function (req, res) {
|
2016-03-08 21:10:12 +00:00
|
|
|
logger.debug('list..');
|
|
|
|
|
|
|
|
dbCouch.view('body','body',function(err, body) {
|
|
|
|
if (!err) {
|
|
|
|
|
|
|
|
var outJSON = [];
|
|
|
|
console.log(body);
|
|
|
|
body.rows.forEach(function(doc) {
|
|
|
|
|
|
|
|
outJSON.push({id:doc.id, body:markdown.toHTML( doc.value ) })
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.debug({data:{list:outJSON}});
|
|
|
|
res.render('index', {data:{list:outJSON}});
|
|
|
|
|
|
|
|
/* res.writeHead(500, {"ContentType": "application/json"});
|
|
|
|
res.end(JSON.stringify({data:{list:outJSON}}));
|
|
|
|
*/
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
res.writeHead(500, {"ContentType": "application/json"});
|
|
|
|
res.end(JSON.stringify({}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/entry/:id', function (req, res) {
|
|
|
|
logger.debug('entry..');
|
|
|
|
|
|
|
|
logger.debug(req.params.id);
|
|
|
|
|
|
|
|
dbCouch.get(req.params.id,function(err, body) {
|
|
|
|
if (!err) {
|
|
|
|
|
|
|
|
var outJSON = {};
|
|
|
|
outJSON.title = body.title;
|
|
|
|
outJSON.reduced = body.reduced;
|
|
|
|
//logger.debug(util.inspect(body));
|
|
|
|
res.writeHead(200, {"ContentType": "application/json"});
|
|
|
|
res.end(JSON.stringify(outJSON));
|
|
|
|
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
res.writeHead(500, {"ContentType": "application/json"});
|
|
|
|
res.end(JSON.stringify({}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post('/log', function (req, res) {
|
|
|
|
logger.debug('add entry..');
|
|
|
|
|
|
|
|
var t = STRING(req.body).replaceAll('%0A', '\n').s
|
|
|
|
|
|
|
|
logger.debug(t);
|
|
|
|
busEmitter.emit("saveLog", t);
|
|
|
|
|
|
|
|
res.writeHead(200, {"ContentType": "application/json"});
|
|
|
|
res.end(JSON.stringify({adding: t.url}));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/new', function (req, res) {
|
|
|
|
logger.debug('Save new');
|
|
|
|
busEmitter.emit("getBookmarkRes", req.query.url ,res);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|