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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 79 additions and 34 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -637,6 +637,14 @@ export const schema = {
env: 'N8N_PUBLIC_API_ENDPOINT', env: 'N8N_PUBLIC_API_ENDPOINT',
doc: 'Path for the public api endpoints', 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: { workflowTagsDisabled: {

View file

@ -796,6 +796,9 @@ export interface IN8nUISettings {
enabled: boolean; enabled: boolean;
latestVersion: number; latestVersion: number;
path: string; path: string;
swaggerUi: {
enabled: boolean;
};
}; };
onboardingCallPromptEnabled: boolean; onboardingCallPromptEnabled: boolean;
allowedModules: { allowedModules: {
@ -1204,6 +1207,9 @@ export interface ISettingsState {
enabled: boolean; enabled: boolean;
latestVersion: number; latestVersion: number;
path: string; path: string;
swaggerUi: {
enabled: boolean;
};
}; };
onboardingCallPromptEnabled: boolean; onboardingCallPromptEnabled: boolean;
saveDataErrorExecution: string; saveDataErrorExecution: string;

View file

@ -1127,6 +1127,8 @@
"settings.api.view.info.webhook": "webhook node", "settings.api.view.info.webhook": "webhook node",
"settings.api.view.myKey": "My API Key", "settings.api.view.myKey": "My API Key",
"settings.api.view.tryapi": "Try it out using the", "settings.api.view.tryapi": "Try it out using the",
"settings.api.view.more-details": "You can find more details in",
"settings.api.view.external-docs": "the API documentation",
"settings.api.view.error": "Could not check if an api key already exists.", "settings.api.view.error": "Could not check if an api key already exists.",
"settings.version": "Version", "settings.version": "Version",
"settings.usageAndPlan.title": "Usage and plan", "settings.usageAndPlan.title": "Usage and plan",

View file

@ -38,6 +38,9 @@ export const useSettingsStore = defineStore(STORES.SETTINGS, {
enabled: false, enabled: false,
latestVersion: 0, latestVersion: 0,
path: '/', path: '/',
swaggerUi: {
enabled: false,
},
}, },
onboardingCallPromptEnabled: false, onboardingCallPromptEnabled: false,
saveDataErrorExecution: 'all', saveDataErrorExecution: 'all',
@ -57,6 +60,9 @@ export const useSettingsStore = defineStore(STORES.SETTINGS, {
isPublicApiEnabled(): boolean { isPublicApiEnabled(): boolean {
return this.api.enabled; return this.api.enabled;
}, },
isSwaggerUIEnabled(): boolean {
return this.api.swaggerUi.enabled;
},
publicApiLatestVersion(): number { publicApiLatestVersion(): number {
return this.api.latestVersion; return this.api.latestVersion;
}, },
@ -139,9 +145,7 @@ export const useSettingsStore = defineStore(STORES.SETTINGS, {
this.userManagement.enabled = settings.userManagement.enabled; this.userManagement.enabled = settings.userManagement.enabled;
this.userManagement.showSetupOnFirstLoad = !!settings.userManagement.showSetupOnFirstLoad; this.userManagement.showSetupOnFirstLoad = !!settings.userManagement.showSetupOnFirstLoad;
this.userManagement.smtpSetup = settings.userManagement.smtpSetup; this.userManagement.smtpSetup = settings.userManagement.smtpSetup;
this.api.enabled = settings.publicApi.enabled; this.api = settings.publicApi;
this.api.latestVersion = settings.publicApi.latestVersion;
this.api.path = settings.publicApi.path;
this.onboardingCallPromptEnabled = settings.onboardingCallPromptEnabled; this.onboardingCallPromptEnabled = settings.onboardingCallPromptEnabled;
}, },
async getSettings(): Promise<void> { async getSettings(): Promise<void> {

View file

@ -48,10 +48,16 @@
</n8n-card> </n8n-card>
<div :class="$style.hint"> <div :class="$style.hint">
<n8n-text size="small"> <n8n-text size="small">
{{ $locale.baseText('settings.api.view.tryapi') }} {{
$locale.baseText(`settings.api.view.${swaggerUIEnabled ? 'tryapi' : 'more-details'}`)
}}
</n8n-text> </n8n-text>
<n8n-link :to="apiPlaygroundPath" :newWindow="true" size="small"> <n8n-link :to="apiDocsURL" :newWindow="true" size="small">
{{ $locale.baseText('settings.api.view.apiPlayground') }} {{
$locale.baseText(
`settings.api.view.${swaggerUIEnabled ? 'apiPlayground' : 'external-docs'}`,
)
}}
</n8n-link> </n8n-link>
</div> </div>
</div> </div>
@ -78,9 +84,10 @@ import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/settings'; import { useSettingsStore } from '@/stores/settings';
import { useRootStore } from '@/stores/n8nRootStore'; import { useRootStore } from '@/stores/n8nRootStore';
import { useUsersStore } from '@/stores/users'; import { useUsersStore } from '@/stores/users';
import { DOCS_DOMAIN } from '@/constants';
export default mixins(showMessage).extend({ export default mixins(showMessage).extend({
name: 'SettingsPersonalView', name: 'SettingsApiView',
components: { components: {
CopyInput, CopyInput,
}, },
@ -89,7 +96,8 @@ export default mixins(showMessage).extend({
loading: false, loading: false,
mounted: false, mounted: false,
apiKey: '', apiKey: '',
apiPlaygroundPath: '', swaggerUIEnabled: false,
apiDocsURL: '',
}; };
}, },
mounted() { mounted() {
@ -97,7 +105,10 @@ export default mixins(showMessage).extend({
const baseUrl = this.rootStore.baseUrl; const baseUrl = this.rootStore.baseUrl;
const apiPath = this.settingsStore.publicApiPath; const apiPath = this.settingsStore.publicApiPath;
const latestVersion = this.settingsStore.publicApiLatestVersion; const latestVersion = this.settingsStore.publicApiLatestVersion;
this.apiPlaygroundPath = `${baseUrl}${apiPath}/v${latestVersion}/docs`; this.swaggerUIEnabled = this.settingsStore.isSwaggerUIEnabled;
this.apiDocsURL = this.swaggerUIEnabled
? `${baseUrl}${apiPath}/v${latestVersion}/docs`
: `https://${DOCS_DOMAIN}/api/api-reference/`;
}, },
computed: { computed: {
...mapStores(useRootStore, useSettingsStore, useUsersStore), ...mapStores(useRootStore, useSettingsStore, useUsersStore),