Repository URL to install this package:
|
Version:
0.6.0 ▾
|
@truesparrow/common-server-js
/
common-server-middleware.js
|
|---|
"use strict";
/** Defines factories for middleware which ensures the basic structure of any `truesparrow` server. */
Object.defineProperty(exports, "__esModule", { value: true });
const Rollbar = require("rollbar");
const common_js_1 = require("@truesparrow/common-js");
/** @private */
const newBunyanLoggerMiddleware = require('express-bunyan-logger');
/** @private */
const Bunyan2Loggly = require('bunyan-loggly');
/** @private */
const LOGGLY_BUFFER_SIZE = 10;
/** @private */
const LOGGLY_TIMEOUT_MS = 1000;
/**
* Create an express middleware component which takes care of the common structure of servers.
* This is meant for local usage - that is Local or Dev, but not Staging or Live.
* Should be the first middleware used in a middleware chain. It ensures that the later routers,
* handlers and middleware receive a properly formatted {@link Request}. More precisely, it
* populates the {@link Request.requestTime} field with the current time in UTC, configures
* {@link Request.logger} to be a bunyan logger instance configured for STDOUT logging and
* configures {@link Request.errorLog} to be a Rollbar instance with remote recording disabled.
* @param name - the name of the service.
* @param env - the environment in which the code is running.
* @param testDisable - disable logging for testing.
* @returns an {@link express.RequestHandler} which does all of the above.
*/
function newLocalCommonServerMiddleware(name, env, testDisable) {
const bunyanLoggerMiddleware = newBunyanLoggerMiddleware({
name: name,
streams: testDisable ? [] : [{
level: 'info',
stream: process.stdout
}],
truesparrow: {
serviceName: name,
env: common_js_1.envToString(env)
}
});
const rollbar = new Rollbar({
accessToken: 'FAKE_TOKEN_WONT_BE_USED_IN_LOCAL_OR_TEST',
logLevel: 'warning',
reportLevel: 'warning',
captureUncaught: true,
captureUnhandledRejections: true,
enabled: false,
payload: {
// TODO: fill in the person field!
serviceName: name,
environment: common_js_1.envToString(env)
}
});
return function (req, res, next) {
req.requestTime = new Date(Date.now());
req.errorLog = rollbar;
bunyanLoggerMiddleware(req, res, next);
};
}
exports.newLocalCommonServerMiddleware = newLocalCommonServerMiddleware;
/**
* Create an express middleware component which takes care of the common structure of servers.
* Should be the first middleware used in a middleware chain. It ensures that the later routers,
* handlers and middleware receive a properly formatted {@link Request}. More precisely, it
* populates the {@link Request.requestTime} field with the current time in UTC, configures
* {@link Request.logger} to be a bunyan logger instance configured for STDOUT and Loggly logging
* and configures {@link Request.errorLog} to be a Rollbar instance with remote recording enabled.
* @param name - the name of the service.
* @param env - the environment in which the code is running.
* @param logglyToken - the secret token required for Loggly communication.
* @param logglySubdomain - the subdomain assigned to us by Loggly.
* @param rollbarToken - the secret token required for Rollbar communication.
* @returns an {@link express.RequestHandler} which does all of the above.
*/
function newCommonServerMiddleware(name, env, logglyToken, logglySubdomain, rollbarToken) {
const bunyanLoggerMiddleware = newBunyanLoggerMiddleware({
name: name,
streams: [{
level: 'info',
stream: process.stdout
}, {
level: 'info',
type: 'raw',
stream: new Bunyan2Loggly({ token: logglyToken, subdomain: logglySubdomain }, LOGGLY_BUFFER_SIZE, LOGGLY_TIMEOUT_MS)
}],
truesparrow: {
serviceName: name,
env: common_js_1.envToString(env)
}
});
const rollbar = new Rollbar({
accessToken: rollbarToken,
logLevel: 'warning',
reportLevel: 'warning',
captureUncaught: true,
captureUnhandledRejections: true,
enabled: true,
payload: {
// TODO: fill in the person field!
serviceName: name,
environment: common_js_1.envToString(env)
}
});
return function (req, res, next) {
req.requestTime = new Date(Date.now());
req.errorLog = rollbar;
bunyanLoggerMiddleware(req, res, next);
};
}
exports.newCommonServerMiddleware = newCommonServerMiddleware;
//# sourceMappingURL=common-server-middleware.js.map