2021-03-11 22:51:58 +01:00
|
|
|
import { responseTimeMetrics } from './middleware/response-time-metrics';
|
|
|
|
import rbacMiddleware from './middleware/rbac-middleware';
|
2021-03-29 19:58:11 +02:00
|
|
|
import apiTokenMiddleware from './middleware/api-token-middleware';
|
|
|
|
import { AuthenticationType } from './types/core';
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
const express = require('express');
|
2020-09-01 21:19:46 +02:00
|
|
|
|
2018-08-22 17:39:09 +02:00
|
|
|
const compression = require('compression');
|
2016-06-18 21:53:18 +02:00
|
|
|
const favicon = require('serve-favicon');
|
|
|
|
const cookieParser = require('cookie-parser');
|
|
|
|
const path = require('path');
|
2016-11-13 15:31:28 +01:00
|
|
|
const errorHandler = require('errorhandler');
|
2020-04-14 22:29:11 +02:00
|
|
|
const IndexRouter = require('./routes');
|
2021-02-18 09:03:21 +01:00
|
|
|
const unleashDbSession = require('./middleware/session-db');
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2017-11-16 15:41:33 +01:00
|
|
|
const requestLogger = require('./middleware/request-logger');
|
2017-11-16 16:45:01 +01:00
|
|
|
const simpleAuthentication = require('./middleware/simple-authentication');
|
2021-04-09 13:46:53 +02:00
|
|
|
const ossAuthentication = require('./middleware/oss-authentication');
|
2019-06-08 12:50:59 +02:00
|
|
|
const noAuthentication = require('./middleware/no-authentication');
|
2020-10-01 21:47:40 +02:00
|
|
|
const secureHeaders = require('./middleware/secure-headers');
|
2016-12-01 17:43:08 +01:00
|
|
|
|
2020-12-17 19:43:01 +01:00
|
|
|
module.exports = function(config, services = {}) {
|
2016-06-18 21:53:18 +02:00
|
|
|
const app = express();
|
2016-11-09 22:31:49 +01:00
|
|
|
|
2016-12-27 21:03:50 +01:00
|
|
|
const baseUriPath = config.baseUriPath || '';
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2020-10-02 16:40:42 +02:00
|
|
|
app.set('trust proxy', true);
|
2017-06-29 11:12:44 +02:00
|
|
|
app.disable('x-powered-by');
|
2016-05-01 22:59:43 +02:00
|
|
|
app.set('port', config.port);
|
2016-05-01 18:20:10 +02:00
|
|
|
app.locals.baseUriPath = baseUriPath;
|
2016-12-28 21:04:26 +01:00
|
|
|
|
|
|
|
if (typeof config.preHook === 'function') {
|
2021-04-12 20:25:03 +02:00
|
|
|
config.preHook(app, config, services);
|
2016-12-28 21:04:26 +01:00
|
|
|
}
|
|
|
|
|
2018-08-22 17:39:09 +02:00
|
|
|
app.use(compression());
|
2016-05-01 22:59:43 +02:00
|
|
|
app.use(cookieParser());
|
2018-02-14 15:46:42 +01:00
|
|
|
app.use(express.json({ strict: false }));
|
2021-02-18 09:03:21 +01:00
|
|
|
app.use(unleashDbSession(config));
|
2021-02-16 14:30:08 +01:00
|
|
|
app.use(responseTimeMetrics(config));
|
2017-11-16 15:41:33 +01:00
|
|
|
app.use(requestLogger(config));
|
2020-10-01 21:47:40 +02:00
|
|
|
app.use(secureHeaders(config));
|
2020-09-28 21:54:44 +02:00
|
|
|
app.use(express.urlencoded({ extended: true }));
|
2017-11-16 15:41:33 +01:00
|
|
|
|
|
|
|
if (config.publicFolder) {
|
|
|
|
app.use(favicon(path.join(config.publicFolder, 'favicon.ico')));
|
|
|
|
app.use(baseUriPath, express.static(config.publicFolder));
|
2016-12-04 14:09:37 +01:00
|
|
|
}
|
2015-03-10 16:30:56 +01:00
|
|
|
|
2020-12-03 21:09:16 +01:00
|
|
|
if (config.enableOAS) {
|
|
|
|
app.use(`${baseUriPath}/oas`, express.static('docs/api/oas'));
|
|
|
|
}
|
|
|
|
|
2021-03-29 19:58:11 +02:00
|
|
|
if (config.adminAuthentication === AuthenticationType.none) {
|
|
|
|
noAuthentication(baseUriPath, app);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated. Will go away in v4.
|
|
|
|
if (config.adminAuthentication === AuthenticationType.unsecure) {
|
|
|
|
app.use(baseUriPath, apiTokenMiddleware(config, services));
|
2021-04-09 13:46:53 +02:00
|
|
|
simpleAuthentication(app, config, services);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.adminAuthentication === AuthenticationType.openSource) {
|
|
|
|
app.use(baseUriPath, apiTokenMiddleware(config, services));
|
|
|
|
ossAuthentication(app, config, services);
|
2017-11-16 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
2021-03-29 19:58:11 +02:00
|
|
|
if (config.adminAuthentication === AuthenticationType.enterprise) {
|
|
|
|
app.use(baseUriPath, apiTokenMiddleware(config, services));
|
|
|
|
config.authentication.customHook(app, config, services);
|
2019-06-08 12:50:59 +02:00
|
|
|
}
|
|
|
|
|
2021-03-29 19:58:11 +02:00
|
|
|
if (config.adminAuthentication === AuthenticationType.custom) {
|
|
|
|
app.use(baseUriPath, apiTokenMiddleware(config, services));
|
|
|
|
config.authentication.customHook(app, config, services);
|
2016-12-28 21:04:26 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 22:51:58 +01:00
|
|
|
app.use(baseUriPath, rbacMiddleware(config, services));
|
|
|
|
|
2021-03-29 19:58:11 +02:00
|
|
|
if (typeof config.preRouterHook === 'function') {
|
|
|
|
config.preRouterHook(app);
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
// Setup API routes
|
2020-12-17 19:43:01 +01:00
|
|
|
app.use(`${baseUriPath}/`, new IndexRouter(config, services).router);
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2016-06-18 09:19:57 +02:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2016-11-13 15:31:28 +01:00
|
|
|
app.use(errorHandler());
|
2016-06-18 09:19:57 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 18:20:10 +02:00
|
|
|
return app;
|
|
|
|
};
|