logger/server/loggerhandler.js
2016-12-22 16:01:24 +00:00

138 lines
3.5 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 authOptions = {
hash: 'sha256', // type of hash
file: 'htpasswd.txt', // path of file
suppress: true // suppress throwing Error if wrong user
};
var authentication =require('basic-authentication')(authOptions);
var busEmitter = new EventEmitter();
var credentials = {
"username": "25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix",
"password": "8e417af1b0462ca55726848846cc6b8696fc76defe9d1864cbc334be59549e0c",
"host": "25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix.cloudant.com",
"port": 443,
"url": "https://25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix:8e417af1b0462ca55726848846cc6b8696fc76defe9d1864cbc334be59549e0c@25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix.cloudant.com",
"database" : "logger"
};
var Cloudant = require('cloudant');
var cloudant = Cloudant({account:credentials.username, password:credentials.password});
var dbCloudant = cloudant.db.use(credentials.database);
function insertLog(obj) {
logger.debug('Inserting into couch...');
logger.info(util.inspect(obj));
var ts = new Date();
var newObj = {body:obj, timestamp:ts};
dbCloudant.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('/', authentication, function (req, res) {
logger.debug('list..');
dbCloudant.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);
dbCloudant.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;