All files / src/lib app.ts

83.53% Statements 71/85
78.26% Branches 18/23
66.67% Functions 2/3
83.53% Lines 71/85

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 15961x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x   61x     61x   61x 61x 61x 61x 61x   61x   61x           151x   151x   151x   151x 151x 151x 151x   151x 151x     151x   151x 22x     151x       151x 151x 151x 151x 64x   151x 151x 151x 151x 151x   151x 151x     151x 148x     151x                                 10x 10x           10x     10x 10x 10x     131x 131x                           151x         151x 36x       151x   151x 148x     151x 151x     151x       151x 5x 2x 2x     3x     151x    
import { publicFolder } from 'unleash-frontend';
import express, { Application, RequestHandler } from 'express';
import cors from 'cors';
import compression from 'compression';
import favicon from 'serve-favicon';
import cookieParser from 'cookie-parser';
import path from 'path';
import errorHandler from 'errorhandler';
import { responseTimeMetrics } from './middleware/response-time-metrics';
import rbacMiddleware from './middleware/rbac-middleware';
import apiTokenMiddleware from './middleware/api-token-middleware';
import { IUnleashServices } from './types/services';
import { IAuthType, IUnleashConfig } from './types/option';
import { IUnleashStores } from './types/stores';
 
import IndexRouter from './routes';
 
import requestLogger from './middleware/request-logger';
import demoAuthentication from './middleware/demo-authentication';
import ossAuthentication from './middleware/oss-authentication';
import noAuthentication from './middleware/no-authentication';
import secureHeaders from './middleware/secure-headers';
 
import { loadIndexHTML } from './util/load-index-html';
 
export default async function getApp(
    config: IUnleashConfig,
    stores: IUnleashStores,
    services: IUnleashServices,
    unleashSession?: RequestHandler,
): Promise<Application> {
    const app = express();
 
    const baseUriPath = config.server.baseUriPath || '';
 
    let indexHTML = await loadIndexHTML(config, publicFolder);
 
    app.set('trust proxy', true);
    app.disable('x-powered-by');
    app.set('port', config.server.port);
    app.locals.baseUriPath = baseUriPath;
 
    if (config.server.serverMetrics && config.eventBus) {
        app.use(responseTimeMetrics(config.eventBus));
    }
 
    app.use(requestLogger(config));
 
    if (typeof config.preHook === 'function') {
        config.preHook(app, config, services);
    }
 
    Iif (process.env.NODE_ENV === 'development') {
        app.use(cors());
    }
 
    app.use(compression());
    app.use(cookieParser());
    app.use(express.json({ strict: false }));
    if (unleashSession) {
        app.use(unleashSession);
    }
    app.use(secureHeaders(config));
    app.use(express.urlencoded({ extended: true }));
    app.use(favicon(path.join(publicFolder, 'favicon.ico')));
    app.use(baseUriPath, favicon(path.join(publicFolder, 'favicon.ico')));
    app.use(baseUriPath, express.static(publicFolder, { index: false }));
 
    if (config.enableOAS) {
        app.use(`${baseUriPath}/oas`, express.static('docs/api/oas'));
    }
 
    if (config.enableOAS && services.openApiService) {
        services.openApiService.useDocs(app);
    }
 
    switch (config.authentication.type) {
        case IAuthType.OPEN_SOURCE: {
            app.use(baseUriPath, apiTokenMiddleware(config, services));
            ossAuthentication(app, config.server.baseUriPath);
            break;
        }
        case IAuthType.ENTERPRISE: {
            app.use(baseUriPath, apiTokenMiddleware(config, services));
            config.authentication.customAuthHandler(app, config, services);
            break;
        }
        case IAuthType.HOSTED: {
            app.use(baseUriPath, apiTokenMiddleware(config, services));
            config.authentication.customAuthHandler(app, config, services);
            break;
        }
        case IAuthType.DEMO: {
            app.use(baseUriPath, apiTokenMiddleware(config, services));
            demoAuthentication(
                app,
                config.server.baseUriPath,
                services,
                config,
            );
            break;
        }
        case IAuthType.CUSTOM: {
            app.use(baseUriPath, apiTokenMiddleware(config, services));
            config.authentication.customAuthHandler(app, config, services);
            break;
        }
        case IAuthType.NONE: {
            noAuthentication(baseUriPath, app);
            break;
        }
        default: {
            app.use(baseUriPath, apiTokenMiddleware(config, services));
            demoAuthentication(
                app,
                config.server.baseUriPath,
                services,
                config,
            );
            break;
        }
    }
 
    app.use(
        baseUriPath,
        rbacMiddleware(config, stores, services.accessService),
    );
 
    if (typeof config.preRouterHook === 'function') {
        config.preRouterHook(app, config, services, stores);
    }
 
    // Setup API routes
    app.use(`${baseUriPath}/`, new IndexRouter(config, services).router);
 
    if (services.openApiService) {
        services.openApiService.useErrorHandler(app);
    }
 
    if (process.env.NODE_ENV !== 'production') {
        app.use(errorHandler());
    }
 
    app.get(`${baseUriPath}`, (req, res) => {
        res.send(indexHTML);
    });
 
    app.get(`${baseUriPath}/*`, (req, res) => {
        if (req.path.startsWith(`${baseUriPath}/api`)) {
            res.status(404).send({ message: '404 - Not found' });
            return;
        }
 
        res.send(indexHTML);
    });
 
    return app;
}