miolo 0.0.1 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,14 +19,14 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar
19
19
 
20
20
  function init_cacher(config) {
21
21
  var client = _redis.default.createClient(config.redis.port, config.redis.host).on('connect', function () {
22
- console.info("".concat((0, _farrapaColors.PURPLE)('REDIS'), " Connection established!"));
22
+ console.info("".concat((0, _farrapaColors.magenta)('REDIS'), " Connection established!"));
23
23
  }).on('error', function (err) {
24
24
  var msg;
25
25
 
26
26
  try {
27
- if (err instanceof _redis.default.ReplyError) msg = "".concat((0, _farrapaColors.PURPLE)('REDIS'), " ").concat((0, _farrapaColors.RED)('Error ' + err.code), " Command: ").concat(err.command, " ").concat(err.toString());else msg = "".concat((0, _farrapaColors.PURPLE)('REDIS'), " ").concat((0, _farrapaColors.RED)('Error ' + err.code), " ").concat(err.toString());
27
+ if (err instanceof _redis.default.ReplyError) msg = "".concat((0, _farrapaColors.magenta)('REDIS'), " ").concat((0, _farrapaColors.red)('Error ' + err.code), " Command: ").concat(err.command, " ").concat(err.toString());else msg = "".concat((0, _farrapaColors.magenta)('REDIS'), " ").concat((0, _farrapaColors.red)('Error ' + err.code), " ").concat(err.toString());
28
28
  } catch (e) {
29
- msg = "".concat((0, _farrapaColors.PURPLE)('REDIS'), " ").concat((0, _farrapaColors.RED)('Error '), " ").concat(e);
29
+ msg = "".concat((0, _farrapaColors.magenta)('REDIS'), " ").concat((0, _farrapaColors.red)('Error '), " ").concat(e);
30
30
  }
31
31
 
32
32
  console.error(msg);
@@ -35,7 +35,7 @@ module.exports = {
35
35
 
36
36
  },
37
37
  options: {
38
- log: 'debug'
38
+ log: 'info'
39
39
  }
40
40
  },
41
41
  routes: {// crud: {
package/lib/db/conn.js CHANGED
@@ -7,9 +7,9 @@ exports.init_db_connection = init_db_connection;
7
7
 
8
8
  var {
9
9
  getConnection
10
- } = require('calustra-conn');
10
+ } = require('calustra');
11
11
 
12
12
  function init_db_connection(config) {
13
- var conn = getConnection(config.connection, config.options);
13
+ var conn = getConnection(config);
14
14
  return conn;
15
15
  }
@@ -15,17 +15,18 @@ var {
15
15
  combine,
16
16
  timestamp,
17
17
  _label,
18
- printf
18
+ printf,
19
+ errors
19
20
  } = format;
20
21
 
21
22
  var init_logger = (config, emailer) => {
22
23
  var LEVEL_COLORS = {
23
- silly: _farrapaColors.LIGHT_CYAN,
24
- debug: _farrapaColors.LIGHT_BLUE,
25
- verbose: _farrapaColors.LIGHT_GREEN,
26
- info: _farrapaColors.YELLOW,
27
- warn: _farrapaColors.LIGHT_RED,
28
- error: _farrapaColors.RED
24
+ silly: _farrapaColors.gray,
25
+ debug: _farrapaColors.magenta,
26
+ verbose: _farrapaColors.cyan,
27
+ info: _farrapaColors.blue,
28
+ warn: _farrapaColors.yellow,
29
+ error: _farrapaColors.red
29
30
  };
30
31
  var myFormat = printf(info => {
31
32
  var _config$format;
@@ -34,7 +35,8 @@ var init_logger = (config, emailer) => {
34
35
  var tm = new Date(info.timestamp);
35
36
  var ts = tm.toLocaleString((config === null || config === void 0 ? void 0 : (_config$format = config.format) === null || _config$format === void 0 ? void 0 : _config$format.locale) || 'en'); //const ts= tm.toString().substr(4, 20)
36
37
 
37
- return "[miolo] ".concat(lc(ts), " ").concat(lc(info.level), " ").concat(info.message);
38
+ var log = "[miolo] ".concat(lc(ts), " ").concat(lc(info.level), " ").concat(info.message);
39
+ return info.stack ? "".concat(log, "\n").concat(info.stack) : log;
38
40
  });
39
41
  var _log_transports = []; //
40
42
  // Console transport
@@ -87,7 +89,9 @@ var init_logger = (config, emailer) => {
87
89
 
88
90
  var logger = createLogger({
89
91
  level: (config === null || config === void 0 ? void 0 : config.level) || 'silly',
90
- format: combine(timestamp(), myFormat),
92
+ format: combine(errors({
93
+ stack: true
94
+ }), timestamp(), myFormat),
91
95
  transports: _log_transports
92
96
  });
93
97
  return logger;
@@ -28,9 +28,9 @@ function init_sys_check_and_log(logger) {
28
28
  var perc = Math.round(used * 100 / total, 2);
29
29
 
30
30
  if (perc > 80) {
31
- logger.error("[SERVER][".concat((0, _farrapaColors.CYAN)('SysCheck'), "] RAM ").concat((0, _farrapaColors.YELLOW)(used), " MB used of ").concat((0, _farrapaColors.GREEN)(total), " MB (").concat((0, _farrapaColors.YELLOW)(perc), " %)"));
31
+ logger.error("[SERVER][".concat((0, _farrapaColors.cyan)('SysCheck'), "] RAM ").concat((0, _farrapaColors.yellow)(used), " MB used of ").concat((0, _farrapaColors.green)(total), " MB (").concat((0, _farrapaColors.yellow)(perc), " %)"));
32
32
  } else {
33
- logger.info("[SERVER][".concat((0, _farrapaColors.CYAN)('SysCheck'), "] RAM ").concat((0, _farrapaColors.YELLOW)(used), " MB used of ").concat((0, _farrapaColors.GREEN)(total), " MB (").concat((0, _farrapaColors.YELLOW)(perc), " %)"));
33
+ logger.info("[SERVER][".concat((0, _farrapaColors.cyan)('SysCheck'), "] RAM ").concat((0, _farrapaColors.yellow)(used), " MB used of ").concat((0, _farrapaColors.green)(total), " MB (").concat((0, _farrapaColors.yellow)(perc), " %)"));
34
34
  }
35
35
 
36
36
  diskspace.check('/', function (err, result) {
@@ -39,9 +39,9 @@ function init_sys_check_and_log(logger) {
39
39
  var free = Math.round(toGB(result.free), 2);
40
40
 
41
41
  if (free < 1) {
42
- logger.error("[SERVER][".concat((0, _farrapaColors.CYAN)('SysCheck'), "] DISK ").concat((0, _farrapaColors.YELLOW)(used), " GB used of ").concat((0, _farrapaColors.GREEN)(total), " MB (").concat((0, _farrapaColors.YELLOW)(free), " GB free)"));
42
+ logger.error("[SERVER][".concat((0, _farrapaColors.cyan)('SysCheck'), "] DISK ").concat((0, _farrapaColors.yellow)(used), " GB used of ").concat((0, _farrapaColors.green)(total), " MB (").concat((0, _farrapaColors.yellow)(free), " GB free)"));
43
43
  } else {
44
- logger.info("[SERVER][".concat((0, _farrapaColors.CYAN)('SysCheck'), "] DISK ").concat((0, _farrapaColors.YELLOW)(used), " GB used of ").concat((0, _farrapaColors.GREEN)(total), " MB (").concat((0, _farrapaColors.YELLOW)(free), " GB free)"));
44
+ logger.info("[SERVER][".concat((0, _farrapaColors.cyan)('SysCheck'), "] DISK ").concat((0, _farrapaColors.yellow)(used), " GB used of ").concat((0, _farrapaColors.green)(total), " MB (").concat((0, _farrapaColors.yellow)(free), " GB free)"));
45
45
  }
46
46
  });
47
47
  }
@@ -34,15 +34,15 @@ function init_socket(logger) {
34
34
  });
35
35
  io.on('connection', function (ctx, _data) {
36
36
  var i = getInfo(ctx);
37
- logger.debug("".concat((0, _farrapaColors.BLUE)('SOCKET'), " Connection from ").concat(i.ip, " ").concat(i.id));
37
+ logger.debug("".concat((0, _farrapaColors.blue)('SOCKET'), " Connection from ").concat(i.ip, " ").concat(i.id));
38
38
  });
39
39
  io.on('disconnect', function (ctx, data) {
40
40
  var i = getInfo(ctx);
41
- logger.debug("".concat((0, _farrapaColors.BLUE)('SOCKET'), " Disconnected ").concat(i.ip, " ").concat(i.id, " => ").concat(data));
41
+ logger.debug("".concat((0, _farrapaColors.blue)('SOCKET'), " Disconnected ").concat(i.ip, " ").concat(i.id, " => ").concat(data));
42
42
  });
43
43
  io.on('error', function (ctx, data) {
44
44
  var i = getInfo(ctx);
45
- logger.error("".concat((0, _farrapaColors.BLUE)('SOCKET'), " Error on ").concat(i.ip, " ").concat(i.id, " => ").concat(data));
45
+ logger.error("".concat((0, _farrapaColors.blue)('SOCKET'), " Error on ").concat(i.ip, " ").concat(i.id, " => ").concat(data));
46
46
  });
47
47
  return io;
48
48
  }
@@ -15,12 +15,14 @@ var _logger = require("../logger");
15
15
 
16
16
  var _cron = require("./engines/cron");
17
17
 
18
+ var _context = require("./middleware/context");
19
+
20
+ var _body = require("./middleware/body");
21
+
18
22
  var _catcher = require("./middleware/catcher");
19
23
 
20
24
  var _static = require("./middleware/static");
21
25
 
22
- var _body = require("./middleware/body");
23
-
24
26
  var _conn = require("../db/conn");
25
27
 
26
28
  var _request = require("./middleware/request");
@@ -51,7 +53,15 @@ function _miolo() {
51
53
  var config = (0, _config.init_config)(sconfig);
52
54
  var emailer = (0, _emailer.init_emailer)(config.mail.options, config.mail.defaults);
53
55
  var logger = (0, _logger.init_logger)(config.log, emailer);
54
- var app = new _koa.default(); // Compress and body parser
56
+ var conn;
57
+
58
+ if (config.database) {
59
+ conn = (0, _conn.init_db_connection)(config.database);
60
+ }
61
+
62
+ var app = new _koa.default(); // Assign miolo stuff to ctx
63
+
64
+ (0, _context.init_context_middleware)(app, config, logger, emailer, conn); // Compress and body parser
55
65
 
56
66
  (0, _body.init_body_middleware)(app); // override koa's undocumented error handler
57
67
 
@@ -59,13 +69,13 @@ function _miolo() {
59
69
 
60
70
  (0, _static.init_static_middleware)(app, (_config$http = config.http) === null || _config$http === void 0 ? void 0 : _config$http.static); // Feed and log request
61
71
 
62
- (0, _request.init_request_middleware)(app, logger); // Create context/session
72
+ (0, _request.init_request_middleware)(app); // Create context/session
63
73
 
64
- (0, _session.init_session_middleware)(app, config === null || config === void 0 ? void 0 : config.session, logger); // attach the default robots.txt
74
+ (0, _session.init_session_middleware)(app, config === null || config === void 0 ? void 0 : config.session); // attach the default robots.txt
65
75
 
66
76
  (0, _robots.init_route_robots)(app); // Middleware for caching JS errors
67
77
 
68
- (0, _catch_js_error.init_route_catch_js_error)(app, '/sys/jserror', logger); // auth middleware
78
+ (0, _catch_js_error.init_route_catch_js_error)(app, '/sys/jserror'); // auth middleware
69
79
 
70
80
  if (config !== null && config !== void 0 && (_config$auth = config.auth) !== null && _config$auth !== void 0 && _config$auth.basic) {
71
81
  var {
@@ -84,12 +94,6 @@ function _miolo() {
84
94
  } // Routes to /crud
85
95
 
86
96
 
87
- var conn;
88
-
89
- if (config.database) {
90
- conn = (0, _conn.init_db_connection)(config.database);
91
- }
92
-
93
97
  if (config !== null && config !== void 0 && (_config$routes = config.routes) !== null && _config$routes !== void 0 && _config$routes.crud) {
94
98
  var {
95
99
  init_crud_router
@@ -112,7 +116,7 @@ function _miolo() {
112
116
 
113
117
 
114
118
  if (render == undefined || render.html != undefined) {
115
- (0, _html_render.init_route_html_render)(app, render === null || render === void 0 ? void 0 : render.html, logger);
119
+ (0, _html_render.init_route_html_render)(app, render === null || render === void 0 ? void 0 : render.html);
116
120
  } else if (render.middleware != undefined) {
117
121
  app.use(render.middleware);
118
122
  }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.init_context_middleware = void 0;
7
+
8
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
9
+
10
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
11
+
12
+ var init_context_middleware = (app, config, logger, emailer, conn) => {
13
+ function context_middleware(_x, _x2) {
14
+ return _context_middleware.apply(this, arguments);
15
+ }
16
+
17
+ function _context_middleware() {
18
+ _context_middleware = _asyncToGenerator(function* (ctx, next) {
19
+ // Assign miolo stuff to ctx
20
+ ctx.miolo = {
21
+ config,
22
+ logger,
23
+ emailer,
24
+ conn
25
+ };
26
+ yield next();
27
+ });
28
+ return _context_middleware.apply(this, arguments);
29
+ }
30
+
31
+ app.use(context_middleware);
32
+ };
33
+
34
+ exports.init_context_middleware = init_context_middleware;
@@ -24,7 +24,7 @@ var REQUEST_COUNTER = 1;
24
24
  * Middleware for feed and log the request
25
25
  */
26
26
 
27
- function init_request_middleware(app, logger) {
27
+ function init_request_middleware(app) {
28
28
  function request_middleware(_x, _x2) {
29
29
  return _request_middleware.apply(this, arguments);
30
30
  }
@@ -33,6 +33,7 @@ function init_request_middleware(app, logger) {
33
33
  _request_middleware = _asyncToGenerator(function* (ctx, next) {
34
34
  var _ctx$state;
35
35
 
36
+ var logger = ctx.miolo.logger;
36
37
  REQUEST_COUNTER += 1;
37
38
  ctx.requestId = REQUEST_COUNTER; // Patch for koa-better-body
38
39
 
@@ -42,11 +43,11 @@ function init_request_middleware(app, logger) {
42
43
 
43
44
  var ip = ctx.headers["x-real-ip"] || '127.0.0.1';
44
45
  ctx.request.ip = ip;
45
- logger.info("req begin ".concat(ctx.requestId, " - ip ").concat(ip, " - ").concat((0, _farrapaColors.CYAN)(ctx.request.method), " ").concat((0, _farrapaColors.CYAN)(ctx.request.url), " ").concat(ctx.request.body != undefined ? JSON.stringify(ctx.request.body) : ''));
46
+ logger.info("req begin ".concat(ctx.requestId, " - ip ").concat(ip, " - ").concat((0, _farrapaColors.cyan)(ctx.request.method), " ").concat((0, _farrapaColors.cyan)(ctx.request.url), " ").concat(ctx.request.body != undefined ? JSON.stringify(ctx.request.body) : ''));
46
47
  yield next();
47
48
  var user = (ctx === null || ctx === void 0 ? void 0 : (_ctx$state = ctx.state) === null || _ctx$state === void 0 ? void 0 : _ctx$state.user) || {};
48
49
  var elapsed = parseFloat((_perf_hooks.performance.now() - started) / 1000.0).toFixed(2);
49
- var tcolor = elapsed < 1.0 ? _farrapaColors.GREEN : elapsed < 2.0 ? _farrapaColors.YELLOW : _farrapaColors.RED;
50
+ var tcolor = elapsed < 1.0 ? _farrapaColors.green : elapsed < 2.0 ? _farrapaColors.yellow : _farrapaColors.red;
50
51
  logger.info("req end ".concat(ctx.requestId, " ").concat(user ? " - uid ".concat(user.id) : '', " => ").concat(tcolor("DONE in ".concat(elapsed, " seconds"))));
51
52
  });
52
53
  return _request_middleware.apply(this, arguments);
@@ -15,7 +15,7 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try
15
15
 
16
16
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
17
17
 
18
- function init_route_catch_js_error(app, route, logger) {
18
+ function init_route_catch_js_error(app, route) {
19
19
  function catch_js_error(_x) {
20
20
  return _catch_js_error.apply(this, arguments);
21
21
  }
@@ -27,7 +27,8 @@ function init_route_catch_js_error(app, route, logger) {
27
27
  path,
28
28
  agent
29
29
  } = ctx.request.body;
30
- var msg = "".concat((0, _farrapaColors.RED)('[JS Error]'), " on ").concat((0, _farrapaColors.BLUE)(path), ": ").concat(error.msg) + "".concat((0, _farrapaColors.RED)('[JS Error]'), " File => ").concat(error.file) + "".concat((0, _farrapaColors.RED)('[JS Error]'), " Line => ").concat(error.line) + "".concat((0, _farrapaColors.RED)('[JS Error]'), " Col => ").concat(error.col) + "".concat((0, _farrapaColors.RED)('[JS Error]'), " Error => ").concat(JSON.stringify(error.error)) + "".concat((0, _farrapaColors.RED)('[JS Error]'), " Agent => ").concat(agent);
30
+ var logger = ctx.miolo.logger;
31
+ var msg = "".concat((0, _farrapaColors.red)('[JS Error]'), " on ").concat((0, _farrapaColors.blue)(path), ": ").concat(error.msg) + "".concat((0, _farrapaColors.red)('[JS Error]'), " File => ").concat(error.file) + "".concat((0, _farrapaColors.red)('[JS Error]'), " Line => ").concat(error.line) + "".concat((0, _farrapaColors.red)('[JS Error]'), " Col => ").concat(error.col) + "".concat((0, _farrapaColors.red)('[JS Error]'), " Error => ").concat(JSON.stringify(error.error)) + "".concat((0, _farrapaColors.red)('[JS Error]'), " Agent => ").concat(agent);
31
32
  logger.error(msg);
32
33
  ctx.body = {
33
34
  result: 1
@@ -23,7 +23,7 @@ var indexHTMLPath = (0, _path.resolve)(__dirname, 'fallback_index.html');
23
23
 
24
24
  var indexHTML = _fs.default.readFileSync(indexHTMLPath, 'utf8');
25
25
 
26
- function init_route_html_render(app, html, logger) {
26
+ function init_route_html_render(app, html) {
27
27
  // Server-side render
28
28
  function html_render(_x) {
29
29
  return _html_render.apply(this, arguments);
@@ -31,12 +31,13 @@ function init_route_html_render(app, html, logger) {
31
31
 
32
32
  function _html_render() {
33
33
  _html_render = _asyncToGenerator(function* (ctx) {
34
+ var logger = ctx.miolo.logger;
34
35
  var reqid = ctx.requestId;
35
36
  var ip = ctx.headers["x-real-ip"] || '127.0.0.1';
36
37
  var method = ctx.request.method;
37
38
  var url = ctx.request.url;
38
39
  var what = html != undefined ? 'provided html' : 'fallback page';
39
- logger.info("".concat(reqid, " - ").concat(ip, " : ").concat((0, _farrapaColors.CYAN)(method), " ").concat((0, _farrapaColors.CYAN)(url), " => Rendering ").concat(what));
40
+ logger.info("".concat(reqid, " - ").concat(ip, " : ").concat((0, _farrapaColors.cyan)(method), " ").concat((0, _farrapaColors.cyan)(url), " => Rendering ").concat(what));
40
41
  ctx.body = html || indexHTML;
41
42
  });
42
43
  return _html_render.apply(this, arguments);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miolo",
3
- "version": "0.0.1",
3
+ "version": "0.0.5",
4
4
  "description": "miolo",
5
5
  "main": "lib/index.js",
6
6
  "author": "Afialapis <info@afialapis.com>",
@@ -15,11 +15,11 @@
15
15
  "babel-plugin-module-resolver": "^4.1.0",
16
16
  "basic-auth": "^2.0.1",
17
17
  "cron": "^1.8.2",
18
- "calustra-conn": "0.0.12",
19
- "calustra-router": "0.0.12",
18
+ "calustra": "0.0.17",
19
+ "calustra-router": "0.0.17",
20
20
  "diskspace": "^2.0.0",
21
21
  "eslint": "^8.7.0",
22
- "farrapa-colors": "^1.0.0",
22
+ "farrapa-colors": "^1.0.3",
23
23
  "koa": "^2.13.4",
24
24
  "koa-better-body": "^3.3.9",
25
25
  "koa-bodyparser": "^4.3.0",
@@ -1,22 +1,22 @@
1
1
  import redis from 'redis'
2
2
  import { promisify } from 'util'
3
- import { RED, PURPLE } from 'farrapa-colors'
3
+ import { red, magenta } from 'farrapa-colors'
4
4
 
5
5
  function init_cacher(config) {
6
6
 
7
7
  const client = redis.createClient(config.redis.port, config.redis.host)
8
8
  .on('connect', function () {
9
- console.info(`${PURPLE('REDIS')} Connection established!`)
9
+ console.info(`${magenta('REDIS')} Connection established!`)
10
10
  })
11
11
  .on('error', function (err) {
12
12
  let msg
13
13
  try {
14
14
  if (err instanceof redis.ReplyError)
15
- msg = `${PURPLE('REDIS')} ${RED('Error ' + err.code)} Command: ${err.command} ${err.toString()}`
15
+ msg = `${magenta('REDIS')} ${red('Error ' + err.code)} Command: ${err.command} ${err.toString()}`
16
16
  else
17
- msg = `${PURPLE('REDIS')} ${RED('Error ' + err.code)} ${err.toString()}`
17
+ msg = `${magenta('REDIS')} ${red('Error ' + err.code)} ${err.toString()}`
18
18
  } catch(e) {
19
- msg = `${PURPLE('REDIS')} ${RED('Error ')} ${e}`
19
+ msg = `${magenta('REDIS')} ${red('Error ')} ${e}`
20
20
  }
21
21
  console.error(msg)
22
22
  })
@@ -30,7 +30,7 @@ module.exports= {
30
30
  idleTimeoutMillis: 10000, // The maximum time, in milliseconds, that a connection can be idle before being released. Use with combination of evict for proper working, for more details read https://github.com/coopernurse/node-pool/issues/178#issuecomment-327110870,
31
31
  },
32
32
  options: {
33
- log: 'debug'
33
+ log: 'info'
34
34
  },
35
35
  },
36
36
  routes: {
package/src/db/conn.js CHANGED
@@ -1,8 +1,8 @@
1
- const {getConnection} = require('calustra-conn')
1
+ const {getConnection} = require('calustra')
2
2
 
3
3
  function init_db_connection(config) {
4
4
 
5
- const conn= getConnection(config.connection, config.options)
5
+ const conn= getConnection(config)
6
6
 
7
7
  return conn
8
8
  }
@@ -1,18 +1,18 @@
1
- import { LIGHT_CYAN, LIGHT_BLUE, LIGHT_GREEN, YELLOW, LIGHT_RED, RED } from 'farrapa-colors'
1
+ import { red, blue, cyan, magenta, yellow, gray } from 'farrapa-colors'
2
2
  /* https://github.com/winstonjs/winston/issues/925 */
3
3
  /* https://github.com/winstonjs/winston/issues/287 */
4
4
  const { createLogger, format, transports } = require('winston')
5
- const { combine, timestamp, _label, printf } = format
5
+ const { combine, timestamp, _label, printf, errors } = format
6
6
 
7
7
 
8
8
  const init_logger = (config, emailer) => {
9
9
  const LEVEL_COLORS= {
10
- silly : LIGHT_CYAN,
11
- debug : LIGHT_BLUE,
12
- verbose: LIGHT_GREEN,
13
- info : YELLOW,
14
- warn : LIGHT_RED,
15
- error : RED
10
+ silly : gray,
11
+ debug : magenta,
12
+ verbose: cyan,
13
+ info : blue,
14
+ warn : yellow,
15
+ error : red
16
16
  }
17
17
 
18
18
  const myFormat = printf(info => {
@@ -20,7 +20,10 @@ const init_logger = (config, emailer) => {
20
20
  const tm = new Date(info.timestamp)
21
21
  const ts= tm.toLocaleString(config?.format?.locale || 'en')
22
22
  //const ts= tm.toString().substr(4, 20)
23
- return `[miolo] ${lc(ts)} ${lc(info.level)} ${info.message}`
23
+ const log= `[miolo] ${lc(ts)} ${lc(info.level)} ${info.message}`
24
+ return info.stack
25
+ ? `${log}\n${info.stack}`
26
+ : log;
24
27
  })
25
28
 
26
29
 
@@ -78,6 +81,7 @@ const init_logger = (config, emailer) => {
78
81
  const logger = createLogger({
79
82
  level: config?.level || 'silly',
80
83
  format: combine(
84
+ errors({ stack: true }),
81
85
  timestamp(),
82
86
  myFormat
83
87
  ),
@@ -1,6 +1,6 @@
1
1
  const os = require('os')
2
2
  const diskspace = require('diskspace')
3
- import { CYAN, GREEN, YELLOW } from 'farrapa-colors'
3
+ import { cyan, green, yellow } from 'farrapa-colors'
4
4
 
5
5
  function init_sys_check_and_log(logger) {
6
6
  function toMB(bytes) {
@@ -22,9 +22,9 @@ function init_sys_check_and_log(logger) {
22
22
  const perc= Math.round( (used*100)/total, 2)
23
23
 
24
24
  if (perc>80) {
25
- logger.error(`[SERVER][${CYAN('SysCheck')}] RAM ${YELLOW(used)} MB used of ${GREEN(total)} MB (${YELLOW(perc)} %)`)
25
+ logger.error(`[SERVER][${cyan('SysCheck')}] RAM ${yellow(used)} MB used of ${green(total)} MB (${yellow(perc)} %)`)
26
26
  } else {
27
- logger.info(`[SERVER][${CYAN('SysCheck')}] RAM ${YELLOW(used)} MB used of ${GREEN(total)} MB (${YELLOW(perc)} %)`)
27
+ logger.info(`[SERVER][${cyan('SysCheck')}] RAM ${yellow(used)} MB used of ${green(total)} MB (${yellow(perc)} %)`)
28
28
  }
29
29
 
30
30
 
@@ -35,9 +35,9 @@ function init_sys_check_and_log(logger) {
35
35
  const free = Math.round(toGB(result.free), 2)
36
36
 
37
37
  if (free<1) {
38
- logger.error(`[SERVER][${CYAN('SysCheck')}] DISK ${YELLOW(used)} GB used of ${GREEN(total)} MB (${YELLOW(free)} GB free)`)
38
+ logger.error(`[SERVER][${cyan('SysCheck')}] DISK ${yellow(used)} GB used of ${green(total)} MB (${yellow(free)} GB free)`)
39
39
  } else {
40
- logger.info(`[SERVER][${CYAN('SysCheck')}] DISK ${YELLOW(used)} GB used of ${GREEN(total)} MB (${YELLOW(free)} GB free)`)
40
+ logger.info(`[SERVER][${cyan('SysCheck')}] DISK ${yellow(used)} GB used of ${green(total)} MB (${yellow(free)} GB free)`)
41
41
  }
42
42
  });
43
43
  }
@@ -1,5 +1,5 @@
1
1
  import IO from 'koa-socket-2'
2
- import { BLUE } from 'farrapa-colors'
2
+ import { blue } from 'farrapa-colors'
3
3
 
4
4
  function init_socket(logger) {
5
5
 
@@ -18,17 +18,17 @@ function init_socket(logger) {
18
18
 
19
19
  io.on('connection', function (ctx, _data) {
20
20
  const i= getInfo(ctx)
21
- logger.debug(`${BLUE('SOCKET')} Connection from ${i.ip} ${i.id}`)
21
+ logger.debug(`${blue('SOCKET')} Connection from ${i.ip} ${i.id}`)
22
22
  })
23
23
 
24
24
  io.on('disconnect', function (ctx, data) {
25
25
  const i = getInfo(ctx)
26
- logger.debug(`${BLUE('SOCKET')} Disconnected ${i.ip} ${i.id} => ${data}`)
26
+ logger.debug(`${blue('SOCKET')} Disconnected ${i.ip} ${i.id} => ${data}`)
27
27
  })
28
28
 
29
29
  io.on('error', function (ctx, data) {
30
30
  const i = getInfo(ctx)
31
- logger.error(`${BLUE('SOCKET')} Error on ${i.ip} ${i.id} => ${data}`)
31
+ logger.error(`${blue('SOCKET')} Error on ${i.ip} ${i.id} => ${data}`)
32
32
  })
33
33
 
34
34
  return io
@@ -5,9 +5,11 @@ import { init_logger } from 'src/logger'
5
5
  import { init_cron } from './engines/cron'
6
6
  // import {init_socket} from './engines/socket'
7
7
 
8
+ import { init_context_middleware } from './middleware/context'
9
+ import { init_body_middleware } from './middleware/body'
8
10
  import { init_catcher_middleware } from './middleware/catcher'
9
11
  import { init_static_middleware } from './middleware/static'
10
- import { init_body_middleware } from './middleware/body'
12
+
11
13
  import { init_db_connection } from 'src/db/conn'
12
14
 
13
15
  import { init_request_middleware } from './middleware/request'
@@ -23,8 +25,15 @@ async function miolo(sconfig, render) {
23
25
  const emailer = init_emailer(config.mail.options, config.mail.defaults)
24
26
  const logger = init_logger(config.log, emailer)
25
27
 
28
+ let conn
29
+ if (config.database) {
30
+ conn= init_db_connection(config.database)
31
+ }
32
+
26
33
  const app = new Koa()
27
34
 
35
+ // Assign miolo stuff to ctx
36
+ init_context_middleware(app, config, logger, emailer, conn)
28
37
  // Compress and body parser
29
38
  init_body_middleware(app)
30
39
 
@@ -35,16 +44,16 @@ async function miolo(sconfig, render) {
35
44
  init_static_middleware(app, config.http?.static)
36
45
 
37
46
  // Feed and log request
38
- init_request_middleware(app, logger)
47
+ init_request_middleware(app)
39
48
 
40
49
  // Create context/session
41
- init_session_middleware(app, config?.session, logger)
50
+ init_session_middleware(app, config?.session)
42
51
 
43
52
  // attach the default robots.txt
44
53
  init_route_robots(app)
45
54
 
46
55
  // Middleware for caching JS errors
47
- init_route_catch_js_error(app, '/sys/jserror', logger)
56
+ init_route_catch_js_error(app, '/sys/jserror')
48
57
 
49
58
  // auth middleware
50
59
  if (config?.auth?.basic) {
@@ -58,11 +67,6 @@ async function miolo(sconfig, render) {
58
67
  }
59
68
 
60
69
  // Routes to /crud
61
- let conn
62
- if (config.database) {
63
- conn= init_db_connection(config.database)
64
- }
65
-
66
70
  if (config?.routes?.crud) {
67
71
  const {init_crud_router} = require('./routes/crud')
68
72
  await init_crud_router(app, conn, config.routes.crud)
@@ -80,7 +84,7 @@ async function miolo(sconfig, render) {
80
84
 
81
85
  // Middleware for html render
82
86
  if (render==undefined || render.html!=undefined) {
83
- init_route_html_render(app, render?.html, logger)
87
+ init_route_html_render(app, render?.html)
84
88
  } else if (render.middleware != undefined) {
85
89
  app.use(render.middleware)
86
90
  }
@@ -0,0 +1,22 @@
1
+
2
+ const init_context_middleware = ( app, config, logger, emailer, conn ) => {
3
+ async function context_middleware(ctx, next) {
4
+
5
+ // Assign miolo stuff to ctx
6
+ ctx.miolo= {
7
+ config,
8
+ logger,
9
+ emailer,
10
+ conn
11
+ }
12
+
13
+ await next()
14
+
15
+ }
16
+
17
+ app.use(context_middleware)
18
+
19
+
20
+ }
21
+
22
+ export { init_context_middleware }
@@ -1,5 +1,5 @@
1
1
  import { performance } from 'perf_hooks'
2
- import { CYAN, GREEN, YELLOW, RED } from 'farrapa-colors'
2
+ import { cyan, green, yellow, red } from 'farrapa-colors'
3
3
 
4
4
  let REQUEST_COUNTER= 1
5
5
 
@@ -7,10 +7,12 @@ let REQUEST_COUNTER= 1
7
7
  * Middleware for feed and log the request
8
8
  */
9
9
 
10
- function init_request_middleware(app, logger) {
10
+ function init_request_middleware(app) {
11
11
 
12
12
  async function request_middleware(ctx, next) {
13
13
 
14
+ const logger = ctx.miolo.logger
15
+
14
16
  REQUEST_COUNTER+= 1
15
17
  ctx.requestId= REQUEST_COUNTER
16
18
 
@@ -22,7 +24,7 @@ function init_request_middleware(app, logger) {
22
24
 
23
25
  ctx.request.ip = ip
24
26
 
25
- logger.info(`req begin ${ctx.requestId} - ip ${ip} - ${CYAN(ctx.request.method)} ${CYAN(ctx.request.url)} ${ctx.request.body!=undefined ? JSON.stringify(ctx.request.body) : ''}`)
27
+ logger.info(`req begin ${ctx.requestId} - ip ${ip} - ${cyan(ctx.request.method)} ${cyan(ctx.request.url)} ${ctx.request.body!=undefined ? JSON.stringify(ctx.request.body) : ''}`)
26
28
 
27
29
  await next()
28
30
 
@@ -32,10 +34,10 @@ function init_request_middleware(app, logger) {
32
34
  const elapsed = parseFloat( (performance.now() - started) / 1000.0 ).toFixed(2)
33
35
 
34
36
  const tcolor= elapsed < 1.0
35
- ? GREEN
37
+ ? green
36
38
  : elapsed < 2.0
37
- ? YELLOW
38
- : RED
39
+ ? yellow
40
+ : red
39
41
 
40
42
  logger.info(`req end ${ctx.requestId} ${user ? ` - uid ${user.id}` : ''} => ${tcolor(`DONE in ${elapsed} seconds`)}`)
41
43
  }
@@ -1,17 +1,19 @@
1
- import { BLUE, RED } from 'farrapa-colors'
1
+ import { blue, red } from 'farrapa-colors'
2
2
  import Router from '@koa/router'
3
3
 
4
- function init_route_catch_js_error(app, route, logger) {
4
+ function init_route_catch_js_error(app, route) {
5
+
5
6
  async function catch_js_error(ctx) {
6
7
  const { error, path, agent } = ctx.request.body
8
+ const logger = ctx.miolo.logger
7
9
 
8
10
  const msg=
9
- `${RED('[JS Error]')} on ${BLUE(path)}: ${error.msg}` +
10
- `${RED('[JS Error]')} File => ${error.file}` +
11
- `${RED('[JS Error]')} Line => ${error.line}` +
12
- `${RED('[JS Error]')} Col => ${error.col}` +
13
- `${RED('[JS Error]')} Error => ${JSON.stringify(error.error)}` +
14
- `${RED('[JS Error]')} Agent => ${agent}`
11
+ `${red('[JS Error]')} on ${blue(path)}: ${error.msg}` +
12
+ `${red('[JS Error]')} File => ${error.file}` +
13
+ `${red('[JS Error]')} Line => ${error.line}` +
14
+ `${red('[JS Error]')} Col => ${error.col}` +
15
+ `${red('[JS Error]')} Error => ${JSON.stringify(error.error)}` +
16
+ `${red('[JS Error]')} Agent => ${agent}`
15
17
 
16
18
  logger.error(msg)
17
19
 
@@ -1,16 +1,18 @@
1
1
 
2
2
  import fs from 'fs'
3
3
  import { resolve } from 'path'
4
- import { CYAN } from 'farrapa-colors'
4
+ import { cyan } from 'farrapa-colors'
5
5
  import Router from '@koa/router'
6
6
 
7
7
  const indexHTMLPath= resolve(__dirname, 'fallback_index.html')
8
8
  const indexHTML = fs.readFileSync(indexHTMLPath, 'utf8')
9
9
 
10
- function init_route_html_render(app, html, logger) {
10
+ function init_route_html_render(app, html) {
11
11
 
12
12
  // Server-side render
13
13
  async function html_render(ctx) {
14
+ const logger= ctx.miolo.logger
15
+
14
16
  const reqid= ctx.requestId
15
17
  const ip= ctx.headers["x-real-ip"] || '127.0.0.1'
16
18
  const method= ctx.request.method
@@ -18,7 +20,7 @@ function init_route_html_render(app, html, logger) {
18
20
  const what = html!=undefined
19
21
  ? 'provided html'
20
22
  : 'fallback page'
21
- logger.info(`${reqid} - ${ip} : ${CYAN(method)} ${CYAN(url)} => Rendering ${what}`)
23
+ logger.info(`${reqid} - ${ip} : ${cyan(method)} ${cyan(url)} => Rendering ${what}`)
22
24
 
23
25
  ctx.body = html || indexHTML
24
26
  }