Skip to content

Commit

Permalink
Splitting out server and app for additional coverage reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
coobr01 committed Sep 18, 2024
1 parent 289701c commit ecd3393
Show file tree
Hide file tree
Showing 6 changed files with 784 additions and 184 deletions.
111 changes: 111 additions & 0 deletions app/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const path = require('path');
const express = require('express');
const helmet = require('helmet');
const noCache = require('nocache');
const cors = require('cors');
const favicon = require('serve-favicon');
const basicAuth = require('express-basic-auth');
const { getEnvironment } = require('./server/utilities/environment');
const logger = require('./server/utilities/logger');
const log = logger.logger;

const app = express();

app.use(
helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
}),
);
app.use(noCache());
app.use(
helmet.hsts({
maxAge: 31536000,
}),
);

app.use(cors());

/****************************************************************
Which environment
****************************************************************/
const { isLocal, isTest, isDevelopment, isStaging } = getEnvironment();

if (isLocal) {
log.info('Environment = local');
app.enable('isLocal');
} else if (isTest) {
log.info('Environment = test');
app.enable('isTest');
}

if (isDevelopment) log.info('Environment = development');
if (isStaging) log.info('Environment = staging');
if (!isLocal && !isTest && !isDevelopment && !isStaging)
log.info('Environment = preprod or production');

/****************************************************************
Setup basic auth for non-staging and non-production
****************************************************************/
if (isDevelopment || isStaging) {
if (
process.env.LEW_BASIC_AUTH_USER_NAME == null ||
process.env.LEW_BASIC_AUTH_USER_PWD == null
) {
log.error(
'Either the basic LEW user name or password environmental variable is not set.',
);
}

const user_json =
'{"' +
process.env.LEW_BASIC_AUTH_USER_NAME +
'" : "' +
process.env.LEW_BASIC_AUTH_USER_PWD +
'"}';
const user_obj = JSON.parse(user_json);

app.use(
basicAuth({
users: user_obj,
challenge: true,
unauthorizedResponse: getUnauthorizedResponse,
}),
);
}

function getUnauthorizedResponse(req) {
return req.auth ? 'Invalid credentials' : 'No credentials provided';
}

/****************************************************************
Setup server and routes
****************************************************************/
app.use(
'/',
express.static(path.join(__dirname, 'public'), { index: ['index.html'] }),
);
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));

/****************************************************************
Enable CORS/Preflight/OPTIONS request
****************************************************************/
app.options('*', cors());

/****************************************************************
Custom application routes
****************************************************************/
require('./server/routes/index')(app);

/****************************************************************
Worst case error handling for 404 and 500 issues
****************************************************************/
app.use(function (req, res, next) {
res.status(404).sendFile(path.join(__dirname, 'public', '404.html'));
});

app.use(function (err, req, res, next) {
res.status(500).sendFile(path.join(__dirname, 'public', '500.html'));
});

module.exports = app;
128 changes: 16 additions & 112 deletions app/app/server.js
Original file line number Diff line number Diff line change
@@ -1,129 +1,33 @@
const express = require('express');
const helmet = require('helmet');
const noCache = require('nocache');
const cors = require('cors');
const favicon = require('serve-favicon');
const basicAuth = require('express-basic-auth');
const path = require('path');
const logger = require('./server/utilities/logger.js');

const app = express();
app.use(
helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
}),
);
app.use(noCache());
app.use(
helmet.hsts({
maxAge: 31536000,
}),
);
app.use(cors());
const logger = require('./server/utilities/logger');
const log = logger.logger;
const browserSyncPort = 9091;
let port = process.env.PORT || 9090;
const browserSync_port = 9091;

/****************************************************************
Which environment
****************************************************************/
let isLocal = false;
let isDevelopment = false;
let isStaging = false;

if (process.env.NODE_ENV) {
isLocal = 'local' === process.env.NODE_ENV.toLowerCase();
isDevelopment = 'development' === process.env.NODE_ENV.toLowerCase();
isStaging = 'staging' === process.env.NODE_ENV.toLowerCase();
}

if (isLocal) log.info('Environment = local');
if (isDevelopment) log.info('Environment = development');
if (isStaging) log.info('Environment = staging');
if (!isLocal && !isDevelopment && !isStaging)
log.info('Environment = staging or production');

/****************************************************************
Setup basic auth for non-staging and non-production
****************************************************************/
if (isDevelopment || isStaging) {
if (
process.env.LEW_BASIC_AUTH_USER_NAME == null ||
process.env.LEW_BASIC_AUTH_USER_PWD == null
) {
log.error(
'Either the basic LEW user name or password environmental variable is not set.',
);
}

const user_json =
'{"' +
process.env.LEW_BASIC_AUTH_USER_NAME +
'" : "' +
process.env.LEW_BASIC_AUTH_USER_PWD +
'"}';
const user_obj = JSON.parse(user_json);

app.use(
basicAuth({
users: user_obj,
challenge: true,
unauthorizedResponse: getUnauthorizedResponse,
}),
);
}

function getUnauthorizedResponse(req) {
return req.auth ? 'Invalid credentials' : 'No credentials provided';
const { isLocal } = require('./server/utilities/environment');

let app;
try {
app = require('./app');
} catch (err) {
log.error('Error starting server: ' + err.message);
process.exit();
}

/****************************************************************
Setup server and routes
****************************************************************/
app.use(
'/',
express.static(path.join(__dirname, 'public'), { index: ['index.html'] }),
);
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));

/****************************************************************
Enable CORS/Preflight/OPTIONS request
****************************************************************/
app.options('*', cors());

/****************************************************************
Custom application routes
****************************************************************/
require('./server/routes/index')(app);

/****************************************************************
Worst case error handling for 404 and 500 issues
****************************************************************/
app.use(function (req, res, next) {
res.status(404).sendFile(path.join(__dirname, 'public', '400.html'));
});

app.use(function (err, req, res, next) {
res.status(500).sendFile(path.join(__dirname, 'public', '500.html'));
});

//For local testing of the production flow, use the same port as browersync to avoid
//different port usage to confuse testers/developers
if (port === 9090 && isLocal === false) {
port = browserSync_port;
}
// for local testing of the production flow, use the same port as browersync to avoid
// different port usage to confuse testers/developers
if (port === 9090 && !isLocal) port = browserSyncPort;

app.listen(port, function () {
if (isLocal) {
const browserSync = require('browser-sync');

log.info(`Application listening on port ${browserSync_port}`);
log.info(`Application listening on port ${browserSyncPort}`);

browserSync({
files: [path.join(__dirname, '/public/**')],
online: false,
open: false,
port: browserSync_port,
port: browserSyncPort,
proxy: 'localhost:' + port,
ui: false,
});
Expand Down
24 changes: 24 additions & 0 deletions app/app/server/utilities/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// determine which environment we are in
exports.getEnvironment = function () {
let isLocal = false;
let isTest = false;
let isDevelopment = false;
let isStaging = false;
let isProduction = false;

if (process.env.NODE_ENV) {
isLocal = 'local' === process.env.NODE_ENV.toLowerCase();
isTest = 'test' === process.env.NODE_ENV.toLowerCase();
isDevelopment = 'development' === process.env.NODE_ENV.toLowerCase();
isStaging = 'staging' === process.env.NODE_ENV.toLowerCase();
isProduction = 'production' === process.env.NODE_ENV.toLowerCase();
}

return {
isLocal,
isTest,
isDevelopment,
isStaging,
isProduction,
};
};
16 changes: 16 additions & 0 deletions app/app/tests/app.express.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const assert = require('assert');
const expect = require('chai').expect;
const should = require('chai').should();
const httpMocks = require('node-mocks-http');
const chai = require('chai');


describe('ServerCheck', () => {
it('Ensure Express app validity', () => {
let app;
expect(() => {
app = require('../app');
}).to.not.throw();
});

});
Loading

0 comments on commit ecd3393

Please sign in to comment.