Commit 3e595d41e52fece3a0cf270c2bf92bc2f39db87d

Authored by Adhidarma Hadiwinoto
1 parent d258995776
Exists in master

logger-circular-buffer-web

Showing 2 changed files with 32 additions and 0 deletions Side-by-side Diff

logger-circular-buffer-web.js
... ... @@ -0,0 +1,31 @@
  1 +"use strict";
  2 +
  3 +const http = require('http');
  4 +const auth = require('basic-auth');
  5 +
  6 +const config = require('./config');
  7 +const logger = require('./logger');
  8 +
  9 +if (config && config.logger && && config.logger.circular_buffer_http && config.logger.circular_buffer_http.listen_port) {
  10 + http.createServer(function(req, res) {
  11 + var credentials = auth(req);
  12 +
  13 + if (!credentials && credentials.name != config.logger.circular_buffer.username && credentials.pass != config.logger.circular_buffer.password) {
  14 + res.statusCode = 401;
  15 + res.setHeader('WWW-Authenticate', 'Basic realm="example"');
  16 + res.end('Access denied');
  17 + }
  18 + else {
  19 +
  20 + logger.query({json: true, order: desc}, function(err, results) {
  21 + if (err) {
  22 + res.end('INVALID LOGGER');
  23 + }
  24 + else {
  25 + res.end(JSON.stringify(results));
  26 + }
  27 + });
  28 + }
  29 + }).listen(config.logger.circular_buffer_http.listen_port);
  30 + logger.verbose('Logger circular buffer http server listen on port ' + config.logger.circular_buffer_http.listen_port);
  31 +}
... ... @@ -19,6 +19,7 @@
19 19 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>",
20 20 "license": "ISC",
21 21 "dependencies": {
  22 + "basic-auth": "^2.0.0",
22 23 "lru-cache": "^4.1.1",
23 24 "request": "^2.81.0",
24 25 "strftime": "^0.10.0",