mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-05 03:05:40 -05:00
* fix: remove circular dependency redux depended on templates/Challenges/redux and vice versa. This meant that import order mattered and confusing bugs could arise. (cherry picked from commit 7d67a4e70922bbb3051f2f9982dcc69e240d43dc) * feat: require imports to be in alphabetical order Import order generally does not matter, but there are edge cases (circular imports and css imports, for example) where changing order changes behaviour (cherry picked from commit b8d1393a91ec6e068caf8e8498a5c95df68c2b2c) * chore: order imports * fix: lift up challenge description + title comps This brings the classic Show closer to the others as they now all create the description and title components * fix: remove donation-saga/index circular import (cherry picked from commit 51a44ca668a700786d2744feffeae4fdba5fd207) * refactor: extract action-types from settings (cherry picked from commit 25e26124d691c84a0d0827d41dafb761c686fadd) * fix: lint errors * feat: prevent useless renames
101 lines
2.4 KiB
JavaScript
101 lines
2.4 KiB
JavaScript
const path = require('path');
|
|
require('dotenv').config({ path: path.resolve(__dirname, '../../../.env') });
|
|
|
|
const Sentry = require('@sentry/node');
|
|
const createDebugger = require('debug');
|
|
const _ = require('lodash');
|
|
const loopback = require('loopback');
|
|
const boot = require('loopback-boot');
|
|
const morgan = require('morgan');
|
|
|
|
const { sentry } = require('../../../config/secrets');
|
|
const { setupPassport } = require('./component-passport');
|
|
|
|
const log = createDebugger('fcc:server');
|
|
const reqLogFormat = ':date[iso] :status :method :response-time ms - :url';
|
|
|
|
// force logger to always output
|
|
// this may be brittle
|
|
log.enabled = true;
|
|
|
|
if (sentry.dns === 'dsn_from_sentry_dashboard') {
|
|
log('Sentry reporting disabled unless DSN is provided.');
|
|
} else {
|
|
Sentry.init({
|
|
dsn: sentry.dns
|
|
});
|
|
log('Sentry initialized');
|
|
}
|
|
|
|
const app = loopback();
|
|
|
|
app.set('state namespace', '__fcc__');
|
|
app.set('port', process.env.API_PORT || 3000);
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
app.use(loopback.token());
|
|
app.use(
|
|
morgan(reqLogFormat, { stream: { write: msg => log(_.split(msg, '\n')[0]) } })
|
|
);
|
|
app.disable('x-powered-by');
|
|
|
|
const createLogOnce = () => {
|
|
let called = false;
|
|
return str => {
|
|
if (called) {
|
|
return null;
|
|
}
|
|
called = true;
|
|
return log(str);
|
|
};
|
|
};
|
|
const logOnce = createLogOnce();
|
|
|
|
boot(app, __dirname, err => {
|
|
if (err) {
|
|
// rethrowing the error here because any error thrown in the boot stage
|
|
// is silent
|
|
logOnce('The below error was thrown in the boot stage');
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
setupPassport(app);
|
|
|
|
const { db } = app.datasources;
|
|
db.on(
|
|
'connected',
|
|
_.once(() => log('db connected'))
|
|
);
|
|
app.start = _.once(function () {
|
|
const server = app.listen(app.get('port'), function () {
|
|
app.emit('started');
|
|
log(
|
|
'freeCodeCamp server listening on port %d in %s',
|
|
app.get('port'),
|
|
app.get('env')
|
|
);
|
|
log(`connecting to db at ${db.settings.url}`);
|
|
});
|
|
|
|
process.on('SIGINT', () => {
|
|
log('Shutting down server');
|
|
server.close(() => {
|
|
log('Server is closed');
|
|
});
|
|
log('closing db connection');
|
|
db.disconnect().then(() => {
|
|
log('DB connection closed');
|
|
// exit process
|
|
// this may close kept alive sockets
|
|
// eslint-disable-next-line no-process-exit
|
|
process.exit(0);
|
|
});
|
|
});
|
|
});
|
|
|
|
module.exports = app;
|
|
|
|
if (require.main === module) {
|
|
app.start();
|
|
}
|