118 lines
2.7 KiB
JavaScript
118 lines
2.7 KiB
JavaScript
"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');
|
|
|
|
var nano = require('nano')('http://localhost:5984');
|
|
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);
|
|
|
|
|
|
router.get('/', function (req, res) {
|
|
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;
|