1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/app.js
Ivar Conradi Østhus d01c9d2dac
fix: use airbnb lint rules directly (#583)
This drops usage of finn-eslint rules as they are no
longer maintained.
2020-04-14 22:29:11 +02:00

63 lines
1.8 KiB
JavaScript

'use strict';
const express = require('express');
const compression = require('compression');
const favicon = require('serve-favicon');
const cookieParser = require('cookie-parser');
const path = require('path');
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');
module.exports = function(config) {
const app = express();
const baseUriPath = config.baseUriPath || '';
app.set('trust proxy');
app.disable('x-powered-by');
app.set('port', config.port);
app.locals.baseUriPath = baseUriPath;
if (typeof config.preHook === 'function') {
config.preHook(app);
}
app.use(compression());
app.use(cookieParser());
app.use(express.json({ strict: false }));
app.use(unleashSession(config));
app.use(responseTime(config));
app.use(requestLogger(config));
if (config.publicFolder) {
app.use(favicon(path.join(config.publicFolder, 'favicon.ico')));
app.use(baseUriPath, express.static(config.publicFolder));
}
if (config.adminAuthentication === 'unsecure') {
simpleAuthentication(baseUriPath, app);
}
if (config.adminAuthentication === 'none') {
noAuthentication(baseUriPath, app);
}
if (typeof config.preRouterHook === 'function') {
config.preRouterHook(app);
}
// Setup API routes
app.use(`${baseUriPath}/`, new IndexRouter(config).router);
if (process.env.NODE_ENV !== 'production') {
app.use(errorHandler());
}
return app;
};