Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request bcgov#104 from jujaga/infra/node16lts
Browse files Browse the repository at this point in the history
Update to Node 16 LTS
  • Loading branch information
Naomi committed Dec 9, 2021
2 parents b3ba9eb + 0e7144a commit fa42a79
Show file tree
Hide file tree
Showing 33 changed files with 2,611 additions and 6,347 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/unit-tests.yaml
Expand Up @@ -14,9 +14,9 @@ jobs:
fail-fast: true
matrix:
node-version:
- 10.x
- 12.x
- 14.x
- '12.x'
- '14.x'
- '16.x'
steps:
- name: Checkout Repository
uses: actions/checkout@v2
Expand Down Expand Up @@ -46,7 +46,7 @@ jobs:
env:
CI: true
- name: Save Coverage Results
if: matrix.node-version == '14.x'
if: matrix.node-version == '16.x'
uses: actions/upload-artifact@v2
with:
name: coverage-app
Expand Down
5 changes: 2 additions & 3 deletions app/README.md
Expand Up @@ -26,8 +26,8 @@ Networking

Logging

* `morgan` - HTTP request logger ([npm](https://www.npmjs.com/package/morgan))
* `npmlog` - General log framework ([npm](https://www.npmjs.com/package/npmlog))
* `express-winston` - HTTP request logger ([npm](https://www.npmjs.com/package/express-winston))
* `winston` - General log framework ([npm](https://www.npmjs.com/package/winston))

Templating

Expand Down Expand Up @@ -114,7 +114,6 @@ For more details, please consult the config library [documentation](https://gith
| `SERVER_BODYLIMIT` | Maximum body length the API will accept |
| `SERVER_LOGFILE` | Writes logs to specific file location if defined |
| `SERVER_LOGLEVEL` | Server log verbosity. Options: `silly`, `verbose`, `debug`, `info`, `warn`, `error` |
| `SERVER_MORGANFORMAT` | Morgan format style. Options: `dev`, `combined` |
| `SERVER_PORT` | Port server is listening to |
| `SERVER_SMTPHOST` | The SMTP server this app will leverage |

Expand Down
68 changes: 16 additions & 52 deletions app/app.js
@@ -1,18 +1,15 @@
const compression = require('compression');
const config = require('config');
const express = require('express');
const fs = require('fs');
const log = require('npmlog');
const moment = require('moment');
const morgan = require('morgan');
const Problem = require('api-problem');
const Writable = require('stream').Writable;

const keycloak = require('./src/components/keycloak');
const utils = require('./src/components/utils');
const v1Router = require('./src/routes/v1');
const log = require('./src/components/log')(module.filename);
const httpLogger = require('./src/components/log').httpLogger;

const { authorizedParty } = require('./src/middleware/authorizedParty');
const v1Router = require('./src/routes/v1');

const DataConnection = require('./src/services/dataConn');
const EmailConnection = require('./src/services/emailConn');
Expand Down Expand Up @@ -41,29 +38,8 @@ app.use(express.urlencoded({
extended: false
}));

// Logging Setup
log.level = config.get('server.logLevel');
log.addLevel('debug', 1500, { fg: 'cyan' });

let logFileStream;
let teeStream;
if (config.has('server.logFile')) {
// Write to logFile in append mode
logFileStream = fs.createWriteStream(config.get('server.logFile'), { flags: 'a' });
teeStream = new Writable({
objectMode: true,
write: (data, _, done) => {
process.stdout.write(data);
logFileStream.write(data);
done();
}
});
log.disableColor();
log.stream = teeStream;
}

// Print out configuration settings in verbose startup
log.verbose('Config', utils.prettyStringify(config));
log.verbose('Config', { config: config });

// Suppresses warning about moment deprecating a default fallback on non ISO/RFC2822 date formats
// We will just force it to use the new Date constructor - https://stackoverflow.com/a/34521624
Expand All @@ -80,17 +56,9 @@ const emailConnection = new EmailConnection();
if (process.env.NODE_ENV !== 'test') {
// make sure authorized party middleware loaded before the mail api tracking...
app.use(authorizedParty);
const morganOpts = {
// Skip logging kube-probe requests
skip: (req) => req.headers['user-agent'] && req.headers['user-agent'].includes('kube-probe')
};
if (config.has('server.logFile')) {
morganOpts.stream = teeStream;
}
// load up morgan to log the requests
app.use(morgan(config.get('server.morganFormat'), morganOpts));
// Initialize connections and exit if unsuccessful
initializeConnections();
app.use(httpLogger);
}

// Use Keycloak OIDC Middleware
Expand Down Expand Up @@ -129,7 +97,7 @@ app.use(/(\/api)?/, apiRouter);
// eslint-disable-next-line no-unused-vars
app.use((err, _req, res, _next) => {
if (err.stack) {
log.error(err.stack);
log.error(err);
}

if (err instanceof Problem) {
Expand All @@ -153,7 +121,7 @@ app.use((_req, res) => {
// Prevent unhandled promise errors from crashing application
process.on('unhandledRejection', err => {
if (err && err.stack) {
log.error(err.stack);
log.error(err);
}
});

Expand All @@ -171,7 +139,7 @@ process.on('exit', () => {
* Shuts down this application after at least 5 seconds.
*/
function shutdown() {
log.info('Received kill signal. Shutting down...');
log.info('Received kill signal. Shutting down...', { function: 'shutdown' });
queueConnection.pause();

// Wait 5 seconds before starting cleanup
Expand All @@ -183,10 +151,10 @@ function shutdown() {
* Cleans up connections in this application.
*/
function cleanup() {
log.info('Service no longer accepting traffic');
log.info('Service no longer accepting traffic', { function: 'cleanup' });
state.shutdown = true;

log.info('Cleaning up...');
log.info('Cleaning up...', { function: 'cleanup' });
clearInterval(probeId);

queueConnection.close(() => {
Expand Down Expand Up @@ -225,21 +193,17 @@ function initializeConnections() {
if (results[2] !== undefined) {
state.connections.email = results[2];
}

if (state.connections.data) log.info('DataConnection', 'Reachable');
if (state.connections.queue) log.info('QueueConnection', 'Reachable');
if (state.connections.email) log.info('EmailConnection', 'Reachable');
})
.catch(error => {
log.error(error.message);
log.error('initializeConnections', `Initialization failed: Database OK = ${state.connections.data}, Queue OK = ${state.connections.queue}, Email OK = ${state.connections.email}`);
log.error(error.message, { function: 'initializeConnections' });
})
.finally(() => {
log.info(`Connection Statuses: Database = ${state.connections.data}, Queue = ${state.connections.queue}, Email = ${state.connections.email}`, { connections: state.connections, function: 'initializeConnections' });
state.ready = Object.values(state.connections).every(x => x);
mountServices();
});
} catch (error) {
log.error('initializeConnections', 'Connection initialization failure', error.message);
log.error('Connection initialization failure', error.message, { function: 'initializeConnections' });
if (!state.ready) {
process.exitCode = 1;
shutdown();
Expand Down Expand Up @@ -268,8 +232,8 @@ function checkConnections() {
state.connections.queue = results[1];
state.ready = Object.values(state.connections).every(x => x);
state.mounted = results[1];
if (!wasMounted && state.mounted && state.ready) log.info('Service ready to accept traffic');
log.verbose(JSON.stringify(state));
if (!wasMounted && state.mounted && state.ready) log.info('Service ready to accept traffic', { function: 'checkConnections' });
log.verbose('State', { function: 'initializeConnections', state: state });
if (!state.ready) {
process.exitCode = 1;
shutdown();
Expand All @@ -290,7 +254,7 @@ function mountServices() {
queueConnection.queue.on('failed', QueueListener.onFailed);
queueConnection.queue.on('drained', QueueListener.onDrained);
queueConnection.queue.on('removed', QueueListener.onRemoved);
log.debug('QueueConnection', 'Listener workers attached');
log.verbose('Listener workers attached', { function: 'mountServices' });
}

module.exports = app;
2 changes: 1 addition & 1 deletion app/bin/www
Expand Up @@ -5,7 +5,7 @@
*/
const config = require('config');
const http = require('http');
const log = require('npmlog');
const log = require('../src/components/log')(module.filename);

const app = require('../app');

Expand Down
1 change: 0 additions & 1 deletion app/config/custom-environment-variables.json
Expand Up @@ -23,7 +23,6 @@
"logFile": "SERVER_LOGFILE",
"logLevel": "SERVER_LOGLEVEL",
"maxAttempts": "SERVER_MAXATTEMPTS",
"morganFormat": "SERVER_MORGANFORMAT",
"port": "SERVER_PORT",
"smtpHost": "SERVER_SMTPHOST"
}
Expand Down
3 changes: 1 addition & 2 deletions app/config/default.json
Expand Up @@ -15,9 +15,8 @@
"server": {
"attachmentLimit": "25mb",
"bodyLimit": "100mb",
"logLevel": "debug",
"logLevel": "http",
"maxAttempts": "3",
"morganFormat": "dev",
"port": "3000",
"smtpHost": "apps.smtp.gov.bc.ca"
}
Expand Down

0 comments on commit fa42a79

Please sign in to comment.