Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | // Load environment variables (GOOGLE_APPLICATION_CREDENTIALS, PUBSUB_INDEX, ...)
// as early as possible so every module below sees them.
require('dotenv').config();
const express = require('express');
const favicon = require('serve-favicon');
const path = require('path');
const config = require('./config');
// The API and the queue worker share this single instance. The Google Cloud
// Pub/Sub client can surface publish/subscribe failures (e.g. a transient auth
// error) as unhandled rejections from its internal batch flush, which would
// otherwise crash the whole process. Log and keep serving instead.
process.on('unhandledRejection', reason => {
console.error('[server] unhandled rejection:', reason);
});
const app = express();
// public assets
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, 'public/images', 'favicon.ico')));
app.use('/coverage', express.static(path.join(__dirname, '..', 'coverage')));
// ejs for view templates
app.engine('.html', require('ejs').__express);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
// load route
require('./route')(app);
// server
const port = process.env.PORT || 3000;
app.server = app.listen(port);
console.log(`listening on port ${port}`);
// Part III - start listening for zip jobs on the queue. The worker consuming the
// queue runs on the same instance as the API. It is required lazily so its
// (ESM / Google Cloud) dependencies are only loaded when the worker is enabled.
Iif (config.workerEnabled) {
require('./queue/consumer').startWorker();
}
// Memory Store TP - warm up the shared rate-limiter Redis connection at boot so
// the first throttled request doesn't pay the connection latency, and so an
// unreachable Redis fails over to the in-memory limiter early. Non-blocking and
// a no-op when Redis-backed limiting is disabled (e.g. the test environment).
require('./rate_limit/redis').connect().catch(() => {});
module.exports = app;
|