mirror of
https://github.com/pa11y/pa11y-dashboard.git
synced 2025-09-24 14:21:13 +00:00

* Add `.nvmrc` → 14 * Fix package.json's URLs * Update to Pa11y CI's version (`https` etc) * Add Node 16 to matrix, and make linter run each time * Standardise `.editorconfig` * Upgrade to `actions/checkout@4` and `actions/setup-node@3` * Replace `npm install` with `npm ci` * Replace `wait-action` with `sleep 10s` * Rewrite to detach config, use promises, and replace `request` with `fetch` (adding `node-fetch` until Node 18) * Delete small single-use helper * Bump to `pa11y-webservice@4.1` and use caret from here (we control the dep) * Return to `this.last`, fix syntax errors * Update copyright to 2023 and remove unused ref * Fix troubleshooting link * Remove symbols, since they could become outdated * Remove missing link * Replace emoji note with GitHub Markdown note * Fix setup link * Replace br with double space * Replace `sh` with `console` for terminal output * Rename Mongo DB used in integration tests to avoid clash locally * Remove JSCS and references to other unused tools * Remove tooling tasks update * Compress definitions and layout where it makes sense or brings clarity * Rename db used in tests to `pa11y-dashboard-integration-test`, to avoid clash with `pa11y-webservice`'s own test DB * Use briefer syntax * Fix integration test setup & config * Reverse function order to return early * Update to `pa11y-lint-config@3`, update `ecmaVersion` to 2020, remove some rule overrides * Fix linting errors, remove cruft * Move linting and broken integration test command into npm scripts (it remains broken) * Revert `node-fetch` to `^2.7.0` (can't do ESM right now) * Upgrade to `pa11y-webservice@^4.2` from `^4.1` * Upgrade to `mocha@^9.2` from `^8.4` (can't do `10` yet because it drops Node 12) * Use backticks for property names * Fix anchor link for 'installing MongoDB' * Reorganise test workflow and add Node `18` and `20` to test matrix * Fix `lockfile-version` to `2` * Apply support policy * Replace Travis badge with one for Actions * Define some more links, separate code blocks * Reflect greater confidence in support for recent versions of MongoDB * Test against MongoDB versions 3-7 * Remove Make tasks `all`, `ci`, `clean`, `install`, `node_modules`, `lint` * Fix integration test command in workflow * Restore shallow Mocha command for now * Move linting into own step `lint` to avoid duplicated warnings * Give the `test` workflow a better name now that it uses a bigger matrix * Drop back to ES2019 from ES2020 for Node 12, remove use of `?.` * Reduce `--slow` to `4000` * Remove a `describe.only` 👀 * Fix broken test for add new item → standard * Rename availability check function * Fix task count check when testing task creation * Return fully to the original `new` logic * Use `127.0.0.1` consistently to fix (possible) IPV6 issue * Fix Cheerio call in failing test * Add MongoDB 2, and tweak other final versions * Lower case and shorten test name, to sit well alongside lint job * Replace `underscore(.groupBy)` with `lodash/groupby` * Capitalise Puppeteer * Say we test against MongoDB 2 as well * Document two replacements of Make with npm scripts * Revise requirements, permitting Node 16+ and describing Ubuntu issue * Support Node 16, 18, 20 * Install `lodash.keys` and fix Lodash mistakes * Fix rebase autoresolution error affecting `index.js` 👀 * This is Pa11y Dashboard not Webservice * Fix MongoDB link * Remove unused link def for Puppeteer * Shush markdown linter * Actually use `pa11y-webservice@4.3` * Improve support table * Don't `fail-fast` * Remove dependency `underscore` * Fix dashboard's port definition for integration test * Set `NODE_ENV=test` for integration tests * Use `mocha@10` * Extend the sleep to rule out the service not starting in time * Wait on port `4000` instead of sleeping * Fix `wait-on-action` to `v1.1.0` * Fix includes of `lodash.groupby` and `lodash.keys` * Label the port waiting action * Fix to `pa11y-webservice@4.2.0` until dep issue resolved * Set waiter action to start after 1s, time out after 30s, and log * Upgrade to `pa11y-webservice@^4.3.1` from `~4.2.0` * Remove comment about recent versions of MongoDB, since we test with them now
191 lines
5.4 KiB
JavaScript
191 lines
5.4 KiB
JavaScript
// This file is part of Pa11y Dashboard.
|
|
//
|
|
// Pa11y Dashboard is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Pa11y Dashboard is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Pa11y Dashboard. If not, see <http://www.gnu.org/licenses/>.
|
|
'use strict';
|
|
|
|
const bodyParser = require('body-parser');
|
|
const compression = require('compression');
|
|
const createClient = require('pa11y-webservice-client-node');
|
|
const {EventEmitter} = require('events');
|
|
const express = require('express');
|
|
const hbs = require('express-hbs');
|
|
const morgan = require('morgan');
|
|
const {nanoid} = require('nanoid');
|
|
const http = require('http');
|
|
const pkg = require('./package.json');
|
|
|
|
module.exports = initApp;
|
|
|
|
function initApp(config, callback) {
|
|
config = defaultConfig(config);
|
|
|
|
let webserviceUrl = config.webservice;
|
|
if (typeof webserviceUrl === 'object') {
|
|
webserviceUrl = `http://${webserviceUrl.host}:${webserviceUrl.port}/`;
|
|
}
|
|
|
|
const app = new EventEmitter();
|
|
|
|
app.express = express();
|
|
app.server = http.createServer(app.express);
|
|
app.webservice = createClient(webserviceUrl);
|
|
|
|
loadMiddleware(app);
|
|
|
|
// View engine
|
|
loadViewEngine(app, config);
|
|
|
|
// Load routes
|
|
loadRoutes(app, config);
|
|
|
|
// Error handling
|
|
loadErrorHandling(app, config, callback);
|
|
}
|
|
|
|
function defaultConfig(config) {
|
|
if (typeof config.noindex !== 'boolean') {
|
|
config.noindex = true;
|
|
}
|
|
if (typeof config.readonly !== 'boolean') {
|
|
config.readonly = false;
|
|
}
|
|
return config;
|
|
}
|
|
|
|
function loadMiddleware(app) {
|
|
app.express.use(compression());
|
|
|
|
// Adds an ID to every request, used later for logging
|
|
app.express.use(addRequestId);
|
|
|
|
// Logging middleware
|
|
morgan.token('id', request => {
|
|
return request.id;
|
|
});
|
|
|
|
// Log the start of all HTTP requests
|
|
const startLog = '[:date[iso] #:id] Started :method :url for :remote-addr';
|
|
// Immediate: true is required to log the request
|
|
// before the response happens
|
|
app.express.use(morgan(startLog, {immediate: true}));
|
|
|
|
// Log the end of all HTTP requests
|
|
const endLog = '[:date[iso] #:id] Completed :status :res[content-length] in :response-time ms';
|
|
app.express.use(morgan(endLog));
|
|
|
|
// Public files
|
|
app.express.use(express.static(`${__dirname}/public`, {
|
|
maxAge: (process.env.NODE_ENV === 'production' ? 604800000 : 0)
|
|
}));
|
|
|
|
// General express config
|
|
app.express.disable('x-powered-by');
|
|
app.express.use(bodyParser.urlencoded({
|
|
extended: true
|
|
}));
|
|
}
|
|
|
|
function loadViewEngine(app, config) {
|
|
app.express.engine('html', hbs.express4({
|
|
extname: '.html',
|
|
contentHelperName: 'content',
|
|
layoutsDir: `${__dirname}/view/layout`,
|
|
partialsDir: `${__dirname}/view/partial`,
|
|
defaultLayout: `${__dirname}/view/layout/default`
|
|
}));
|
|
app.express.set('views', `${__dirname}/view`);
|
|
app.express.set('view engine', 'html');
|
|
|
|
// View helpers
|
|
require('./view/helper/date')(hbs);
|
|
require('./view/helper/string')(hbs);
|
|
require('./view/helper/url')(hbs);
|
|
require('./view/helper/conditionals')(hbs);
|
|
|
|
// Populate view locals
|
|
app.express.locals = {
|
|
lang: 'en',
|
|
year: (new Date()).getFullYear(),
|
|
version: pkg.version,
|
|
repo: pkg.homepage,
|
|
bugtracker: pkg.bugs,
|
|
noindex: config.noindex,
|
|
readonly: config.readonly,
|
|
siteMessage: config.siteMessage,
|
|
settings: {}
|
|
};
|
|
|
|
app.express.use((request, response, next) => {
|
|
response.locals.isHomePage = (request.path === '/');
|
|
response.locals.host = request.hostname;
|
|
next();
|
|
});
|
|
}
|
|
|
|
function loadRoutes(app, config) {
|
|
// Because there's some overlap between the different routes,
|
|
// they have to be loaded in a specific order in order to avoid
|
|
// passing mongo the wrong id which would result in
|
|
// "ObjectID generation failed." errors (e.g. #277)
|
|
require('./route/index')(app);
|
|
require('./route/result/download')(app);
|
|
|
|
if (!config.readonly) {
|
|
require('./route/new')(app);
|
|
require('./route/task/delete')(app);
|
|
require('./route/task/run')(app);
|
|
require('./route/task/edit')(app);
|
|
require('./route/task/ignore')(app);
|
|
require('./route/task/unignore')(app);
|
|
}
|
|
|
|
// Needs to be loaded after `/route/new`
|
|
require('./route/task/index')(app);
|
|
// Needs to be loaded after `/route/task/edit`
|
|
require('./route/result/index')(app);
|
|
}
|
|
|
|
function loadErrorHandling(app, config, callback) {
|
|
app.express.get('*', (request, response) => {
|
|
response.status(404);
|
|
response.render('404');
|
|
});
|
|
app.express.use((error, request, response, next) => {
|
|
/* eslint no-unused-vars: 'off' */
|
|
if (error.code === 'ECONNREFUSED') {
|
|
error = new Error('Could not connect to Pa11y Webservice');
|
|
}
|
|
app.emit('route-error', error);
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
response.locals.error = error;
|
|
}
|
|
response.status(500);
|
|
response.render('500');
|
|
});
|
|
|
|
app.server.listen(config.port, error => {
|
|
callback(error, app);
|
|
});
|
|
}
|
|
|
|
// Express middleware
|
|
function addRequestId(request, response, next) {
|
|
// Create a random request (nano)id, 10 characters long
|
|
// Nano ids are [0-9A-Za-z_-] so chance of collision is 1 in 64^10
|
|
// If a site has so much traffic that this chance is too high
|
|
// we probably have worse things to worry about
|
|
request.id = nanoid(10);
|
|
next();
|
|
}
|