perf: Lazy-load public-api dependencies to reduce baseline memory usage (#5049)

* refactor: Load swagger and openapi dependencies conditionally

* disable public api in tests to reduce heal usage

* update the link and text in SettingsApiView when swagger ui is disabled
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-01-02 12:14:58 +01:00
committed by GitHub
parent b828cb31d6
commit a455cce7e6
9 changed files with 79 additions and 34 deletions

View File

@@ -531,6 +531,9 @@ export interface IPublicApiSettings {
enabled: boolean;
latestVersion: number;
path: string;
swaggerUi: {
enabled: boolean;
};
}
export interface IPackageVersions {

View File

@@ -3,27 +3,25 @@ import express, { Router } from 'express';
import fs from 'fs/promises';
import path from 'path';
import * as OpenApiValidator from 'express-openapi-validator';
import { HttpError } from 'express-openapi-validator/dist/framework/types';
import { OpenAPIV3 } from 'openapi-types';
import swaggerUi from 'swagger-ui-express';
import validator from 'validator';
import YAML from 'yamljs';
import type { HttpError } from 'express-openapi-validator/dist/framework/types';
import type { OpenAPIV3 } from 'openapi-types';
import type { JsonObject } from 'swagger-ui-express';
import config from '@/config';
import * as Db from '@/Db';
import { InternalHooksManager } from '@/InternalHooksManager';
import { getInstanceBaseUrl } from '@/UserManagement/UserManagementHelper';
function createApiRouter(
async function createApiRouter(
version: string,
openApiSpecPath: string,
handlersDirectory: string,
swaggerThemeCss: string,
publicApiEndpoint: string,
): Router {
): Promise<Router> {
const n8nPath = config.getEnv('path');
const swaggerDocument = YAML.load(openApiSpecPath) as swaggerUi.JsonObject;
const YAML = await import('yamljs');
const swaggerDocument = YAML.load(openApiSpecPath) as JsonObject;
// add the server depending on the config so the user can interact with the API
// from the Swagger UI
swaggerDocument.server = [
@@ -33,21 +31,26 @@ function createApiRouter(
];
const apiController = express.Router();
apiController.use(
`/${publicApiEndpoint}/${version}/docs`,
swaggerUi.serveFiles(swaggerDocument),
swaggerUi.setup(swaggerDocument, {
customCss: swaggerThemeCss,
customSiteTitle: 'n8n Public API UI',
customfavIcon: `${n8nPath}favicon.ico`,
}),
);
if (!config.getEnv('publicApi.swaggerUi.disabled')) {
const { serveFiles, setup } = await import('swagger-ui-express');
apiController.use(`/${publicApiEndpoint}/${version}`, express.json());
apiController.use(
`/${publicApiEndpoint}/${version}/docs`,
serveFiles(swaggerDocument),
setup(swaggerDocument, {
customCss: swaggerThemeCss,
customSiteTitle: 'n8n Public API UI',
customfavIcon: `${n8nPath}favicon.ico`,
}),
);
}
const { default: validator } = await import('validator');
const { middleware } = await import('express-openapi-validator');
apiController.use(
`/${publicApiEndpoint}/${version}`,
OpenApiValidator.middleware({
express.json(),
middleware({
apiSpec: openApiSpecPath,
operationHandlers: handlersDirectory,
validateRequests: true,
@@ -131,10 +134,12 @@ export const loadPublicApiVersions = async (
const css = (await fs.readFile(swaggerThemePath)).toString();
const versions = folders.filter((folderName) => folderName.startsWith('v'));
const apiRouters = versions.map((version) => {
const openApiPath = path.join(__dirname, version, 'openapi.yml');
return createApiRouter(version, openApiPath, __dirname, css, publicApiEndpoint);
});
const apiRouters = await Promise.all(
versions.map(async (version) => {
const openApiPath = path.join(__dirname, version, 'openapi.yml');
return createApiRouter(version, openApiPath, __dirname, css, publicApiEndpoint);
}),
);
return {
apiRouters,

View File

@@ -332,9 +332,12 @@ class App {
smtpSetup: isEmailSetUp(),
},
publicApi: {
enabled: config.getEnv('publicApi.disabled') === false,
enabled: !config.getEnv('publicApi.disabled'),
latestVersion: 1,
path: config.getEnv('publicApi.path'),
swaggerUi: {
enabled: !config.getEnv('publicApi.swaggerUi.disabled'),
},
},
workflowTagsDisabled: config.getEnv('workflowTagsDisabled'),
logLevel: config.getEnv('logs.level'),

View File

@@ -19,6 +19,9 @@ if (inE2ETests) {
EXTERNAL_FRONTEND_HOOKS_URLS: '',
N8N_PERSONALIZATION_ENABLED: 'false',
};
}
if (inTest) {
process.env.N8N_PUBLIC_API_DISABLED = 'true';
} else {
dotenv.config();
}

View File

@@ -637,6 +637,14 @@ export const schema = {
env: 'N8N_PUBLIC_API_ENDPOINT',
doc: 'Path for the public api endpoints',
},
swaggerUi: {
disabled: {
format: Boolean,
default: false,
env: 'N8N_PUBLIC_API_SWAGGERUI_DISABLED',
doc: 'Whether to disable the Swagger UI for the Public API',
},
},
},
workflowTagsDisabled: {