1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/app.js

71 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-10-26 10:43:11 +02:00
2016-06-18 21:53:18 +02:00
const express = require('express');
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');
const IndexRouter = require('./routes');
const unleashSession = require('./middleware/session');
const responseTime = require('./middleware/response-time');
const requestLogger = require('./middleware/request-logger');
const simpleAuthentication = require('./middleware/simple-authentication');
const noAuthentication = require('./middleware/no-authentication');
2020-10-01 21:47:40 +02:00
const secureHeaders = require('./middleware/secure-headers');
2017-06-28 10:17:14 +02:00
module.exports = function(config) {
2016-06-18 21:53:18 +02:00
const app = express();
2016-12-27 21:03:50 +01:00
const baseUriPath = config.baseUriPath || '';
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') {
config.preHook(app);
}
2018-08-22 17:39:09 +02:00
app.use(compression());
2016-05-01 22:59:43 +02:00
app.use(cookieParser());
app.use(express.json({ strict: false }));
app.use(unleashSession(config));
app.use(responseTime(config));
app.use(requestLogger(config));
2020-10-01 21:47:40 +02:00
app.use(secureHeaders(config));
app.use(express.urlencoded({ extended: true }));
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
if (config.enableOAS) {
app.use(`${baseUriPath}/oas`, express.static('docs/api/oas'));
}
if (config.adminAuthentication === 'unsecure') {
simpleAuthentication(baseUriPath, app);
}
if (config.adminAuthentication === 'none') {
noAuthentication(baseUriPath, app);
}
2016-12-28 21:04:26 +01:00
if (typeof config.preRouterHook === 'function') {
config.preRouterHook(app);
}
// Setup API routes
app.use(`${baseUriPath}/`, new IndexRouter(config).router);
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;
};