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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import express from 'express';
import path from 'path';
import cookieParser from 'cookie-parser';
import errorHandler from '@errors';
import expressJwt from 'express-jwt';
import morganDebug from 'morgan-debug';
import {obfuscateMetrics} from '@util/obfuscateMetrics';
import debug from 'debug';
import * as Sentry from '@sentry/node';
import * as Tracing from '@sentry/tracing';
import compression from 'compression';
// Inject custom checks for **express-validator**.
// See `validators/inject-custom-checks.ts` for more details.
import {injectCustomChecks} from '@app/validators';
injectCustomChecks();
/**
* Import router
*/
import config from '@config';
import authRouter from '@app/routes/auth';
import jwtCsrf from './routes/auth/jwt/jwt-csrf';
import {postLoginJwtValidator} from '@routes/auth/jwt/jwt-util';
import apiRouter from './routes/api';
import {userRouter} from '@routes/user';
const log = debug('group-car:app');
const app: express.Application = express();
// Add middleware
app.set('trust proxy', true);
// Disable powered by
app.disable('x-powered-by');
const nonTraceablePaths = [
'/swagger-stats/metrics',
];
/**
* Initialise sentry. Don't if testing
*/
Iif (process.env.NODE_ENV !== 'test') {
log('Sentry monitoring with dsn %s', config.metrics.dsn);
Sentry.init({
dsn: config.metrics.dsn,
integrations: [
new Sentry.Integrations.Http({tracing: true}),
new Tracing.Integrations.Express({app}),
],
tracesSampleRate: config.metrics.tracesSampleRate,
});
const tracingHandler = Sentry.Handlers.tracingHandler();
app.use(Sentry.Handlers.requestHandler());
app.use((req, res, next) => {
// Filter out paths which should not be traced
if (!nonTraceablePaths.includes(req.path)) {
tracingHandler(req, res, next);
} else {
next();
}
});
}
// Only log http request if a format string is provided
Iif (config.morgan.formatString !== null) {
app.use(morganDebug(
'group-car-http',
config.morgan.formatString,
{
skip: (req: express.Request) =>
nonTraceablePaths.includes(req.path),
},
));
}
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(jwtCsrf());
import swaggerStats from 'swagger-stats';
import fs from 'fs';
import yaml from 'js-yaml';
/*
* If metrics enabled, configure middleware
*/
Iif (config.metrics.enabled) {
try {
log('Metrics enabled');
const fileContents = fs.readFileSync(
'static/doc/openapi/openapi.yaml', 'utf-8');
const spec = yaml.load(fileContents) as Record<string, unknown>;
app.use(swaggerStats.getMiddleware({
swaggerSpec: spec,
onResponseFinish: obfuscateMetrics,
}));
log('Metrics initialised');
} catch (e) {
log('Could not initialise metrics: %s', e.message);
}
}
// Adding authentication routes
app.use('/auth', authRouter);
// Add user routers
app.use('/user', userRouter);
// Add api router
app.use(
'/api',
expressJwt({
secret: config.jwt.secret,
getToken: config.jwt.getToken,
requestProperty: 'auth',
algorithms: ['HS512'],
}),
postLoginJwtValidator,
apiRouter,
);
/**
* Add compression for static content
*/
app.use(compression());
/**
* Configure serving of documentation
*/
app.use(express.static('static'));
/**
* Configure static service
*/
Eif (!config.static.disabled) {
app.use(express.static(config.static.path));
app.get('/*', (_req, res) => {
res.sendFile(
path.join(
config.static.path,
'index.html'));
});
}
app.use(Sentry.Handlers.errorHandler());
// Register error handler
app.use(errorHandler);
export default app;
|