Skip to content

Commit

Permalink
Revert "refactor(core): Port endpoints config (no-changelog) (#10268)"
Browse files Browse the repository at this point in the history
This reverts commit 1608d25.
  • Loading branch information
despairblue committed Aug 2, 2024
1 parent 454381a commit 91a3552
Show file tree
Hide file tree
Showing 21 changed files with 228 additions and 275 deletions.
102 changes: 0 additions & 102 deletions packages/@n8n/config/src/configs/endpoints.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/@n8n/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { EventBusConfig } from './configs/event-bus';
import { NodesConfig } from './configs/nodes';
import { ExternalStorageConfig } from './configs/external-storage';
import { WorkflowsConfig } from './configs/workflows';
import { EndpointsConfig } from './configs/endpoints';

@Config
class UserManagementConfig {
Expand Down Expand Up @@ -72,7 +71,4 @@ export class GlobalConfig {
/** HTTP Protocol via which n8n can be reached */
@Env('N8N_PROTOCOL')
readonly protocol: 'http' | 'https' = 'http';

@Nested
readonly endpoints: EndpointsConfig;
}
34 changes: 1 addition & 33 deletions packages/@n8n/config/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,44 +145,12 @@ describe('GlobalConfig', () => {
onboardingFlowDisabled: false,
callerPolicyDefaultOption: 'workflowsFromSameOwner',
},
endpoints: {
metrics: {
enable: false,
prefix: 'n8n_',
includeWorkflowIdLabel: false,
includeDefaultMetrics: true,
includeMessageEventBusMetrics: false,
includeNodeTypeLabel: false,
includeCacheMetrics: false,
includeApiEndpoints: false,
includeApiPathLabel: false,
includeApiMethodLabel: false,
includeCredentialTypeLabel: false,
includeApiStatusCodeLabel: false,
},
additionalNonUIRoutes: '',
disableProductionWebhooksOnMainProcess: false,
disableUi: false,
form: 'form',
formTest: 'form-test',
formWaiting: 'form-waiting',
payloadSizeMax: 16,
rest: 'rest',
webhook: 'webhook',
webhookTest: 'webhook-test',
webhookWaiting: 'webhook-waiting',
},
};

it('should use all default values when no env variables are defined', () => {
process.env = {};
const config = Container.get(GlobalConfig);

// deepCopy for diff to show plain objects
// eslint-disable-next-line n8n-local-rules/no-json-parse-json-stringify
const deepCopy = <T>(obj: T): T => JSON.parse(JSON.stringify(obj));

expect(deepCopy(config)).toEqual(defaultConfig);
expect(config).toEqual(defaultConfig);
expect(mockFs.readFileSync).not.toHaveBeenCalled();
});

Expand Down
27 changes: 12 additions & 15 deletions packages/cli/src/AbstractServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export abstract class AbstractServer {

protected externalHooks: ExternalHooks;

protected globalConfig = Container.get(GlobalConfig);
protected protocol = Container.get(GlobalConfig).protocol;

protected sslKey: string;

Expand Down Expand Up @@ -74,15 +74,15 @@ export abstract class AbstractServer {
this.sslKey = config.getEnv('ssl_key');
this.sslCert = config.getEnv('ssl_cert');

this.restEndpoint = this.globalConfig.endpoints.rest;
this.restEndpoint = config.getEnv('endpoints.rest');

this.endpointForm = this.globalConfig.endpoints.form;
this.endpointFormTest = this.globalConfig.endpoints.formTest;
this.endpointFormWaiting = this.globalConfig.endpoints.formWaiting;
this.endpointForm = config.getEnv('endpoints.form');
this.endpointFormTest = config.getEnv('endpoints.formTest');
this.endpointFormWaiting = config.getEnv('endpoints.formWaiting');

this.endpointWebhook = this.globalConfig.endpoints.webhook;
this.endpointWebhookTest = this.globalConfig.endpoints.webhookTest;
this.endpointWebhookWaiting = this.globalConfig.endpoints.webhookWaiting;
this.endpointWebhook = config.getEnv('endpoints.webhook');
this.endpointWebhookTest = config.getEnv('endpoints.webhookTest');
this.endpointWebhookWaiting = config.getEnv('endpoints.webhookWaiting');

this.uniqueInstanceId = generateHostInstanceId(instanceType);

Expand Down Expand Up @@ -134,8 +134,7 @@ export abstract class AbstractServer {
}

async init(): Promise<void> {
const { app, sslKey, sslCert } = this;
const { protocol } = this.globalConfig;
const { app, protocol, sslKey, sslCert } = this;

if (protocol === 'https' && sslKey && sslCert) {
const https = await import('https');
Expand Down Expand Up @@ -262,16 +261,14 @@ export abstract class AbstractServer {
return;
}

const { protocol } = this.globalConfig;

this.logger.debug(`Shutting down ${protocol} server`);
this.logger.debug(`Shutting down ${this.protocol} server`);

this.server.close((error) => {
if (error) {
this.logger.error(`Error while shutting down ${protocol} server`, { error });
this.logger.error(`Error while shutting down ${this.protocol} server`, { error });
}

this.logger.debug(`${protocol} server shut down`);
this.logger.debug(`${this.protocol} server shut down`);
});
}
}
15 changes: 8 additions & 7 deletions packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { promisify } from 'util';
import cookieParser from 'cookie-parser';
import express from 'express';
import helmet from 'helmet';
import { GlobalConfig } from '@n8n/config';
import { InstanceSettings } from 'n8n-core';
import type { IN8nUISettings } from 'n8n-workflow';

Expand Down Expand Up @@ -80,16 +81,17 @@ export class Server extends AbstractServer {
private readonly loadNodesAndCredentials: LoadNodesAndCredentials,
private readonly orchestrationService: OrchestrationService,
private readonly postHogClient: PostHogClient,
private readonly globalConfig: GlobalConfig,
private readonly eventService: EventService,
) {
super('main');

this.testWebhooksEnabled = true;
this.webhooksEnabled = !this.globalConfig.endpoints.disableProductionWebhooksOnMainProcess;
this.webhooksEnabled = !config.getEnv('endpoints.disableProductionWebhooksOnMainProcess');
}

async start() {
if (!this.globalConfig.endpoints.disableUi) {
if (!config.getEnv('endpoints.disableUi')) {
const { FrontendService } = await import('@/services/frontend.service');
this.frontendService = Container.get(FrontendService);
}
Expand Down Expand Up @@ -131,7 +133,7 @@ export class Server extends AbstractServer {
await import('@/controllers/mfa.controller');
}

if (!this.globalConfig.endpoints.disableUi) {
if (!config.getEnv('endpoints.disableUi')) {
await import('@/controllers/cta.controller');
}

Expand Down Expand Up @@ -165,7 +167,7 @@ export class Server extends AbstractServer {
}

async configure(): Promise<void> {
if (this.globalConfig.endpoints.metrics.enable) {
if (config.getEnv('endpoints.metrics.enable')) {
const { PrometheusMetricsService } = await import('@/metrics/prometheus-metrics.service');
await Container.get(PrometheusMetricsService).init(this.app);
}
Expand Down Expand Up @@ -305,8 +307,7 @@ export class Server extends AbstractServer {
this.app.use('/icons/@:scope/:packageName/*/*.(svg|png)', serveIcons);
this.app.use('/icons/:packageName/*/*.(svg|png)', serveIcons);

const isTLSEnabled =
this.globalConfig.protocol === 'https' && !!(this.sslKey && this.sslCert);
const isTLSEnabled = this.protocol === 'https' && !!(this.sslKey && this.sslCert);
const isPreviewMode = process.env.N8N_PREVIEW_MODE === 'true';
const securityHeadersMiddleware = helmet({
contentSecurityPolicy: false,
Expand Down Expand Up @@ -340,7 +341,7 @@ export class Server extends AbstractServer {
this.restEndpoint,
this.endpointPresetCredentials,
isApiEnabled() ? '' : publicApiEndpoint,
...this.globalConfig.endpoints.additionalNonUIRoutes.split(':'),
...config.getEnv('endpoints.additionalNonUIRoutes').split(':'),
].filter((u) => !!u);
const nonUIRoutesRegex = new RegExp(`^/(${nonUIRoutes.join('|')})/?.*$`);
const historyApiHandler: express.RequestHandler = (req, res, next) => {
Expand Down
16 changes: 10 additions & 6 deletions packages/cli/src/WorkflowExecuteAdditionalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,19 +1002,23 @@ export async function getBase(
): Promise<IWorkflowExecuteAdditionalData> {
const urlBaseWebhook = Container.get(UrlService).getWebhookBaseUrl();

const globalConfig = Container.get(GlobalConfig);
const formWaitingBaseUrl = urlBaseWebhook + config.getEnv('endpoints.formWaiting');

const webhookBaseUrl = urlBaseWebhook + config.getEnv('endpoints.webhook');
const webhookTestBaseUrl = urlBaseWebhook + config.getEnv('endpoints.webhookTest');
const webhookWaitingBaseUrl = urlBaseWebhook + config.getEnv('endpoints.webhookWaiting');

const variables = await WorkflowHelpers.getVariables();

return {
credentialsHelper: Container.get(CredentialsHelper),
executeWorkflow,
restApiUrl: urlBaseWebhook + globalConfig.endpoints.rest,
restApiUrl: urlBaseWebhook + config.getEnv('endpoints.rest'),
instanceBaseUrl: urlBaseWebhook,
formWaitingBaseUrl: globalConfig.endpoints.formWaiting,
webhookBaseUrl: globalConfig.endpoints.webhook,
webhookWaitingBaseUrl: globalConfig.endpoints.webhookWaiting,
webhookTestBaseUrl: globalConfig.endpoints.webhookTest,
formWaitingBaseUrl,
webhookBaseUrl,
webhookWaitingBaseUrl,
webhookTestBaseUrl,
currentNodeParameters,
executionTimeoutTimestamp,
userId,
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Container, { Service } from 'typedi';
import { Service } from 'typedi';
import type { NextFunction, Response } from 'express';
import { createHash } from 'crypto';
import { JsonWebTokenError, TokenExpiredError } from 'jsonwebtoken';
Expand All @@ -14,7 +14,6 @@ import { Logger } from '@/Logger';
import type { AuthenticatedRequest } from '@/requests';
import { JwtService } from '@/services/jwt.service';
import { UrlService } from '@/services/url.service';
import { GlobalConfig } from '@n8n/config';

interface AuthJwtPayload {
/** User Id */
Expand All @@ -34,7 +33,7 @@ interface PasswordResetToken {
hash: string;
}

const restEndpoint = Container.get(GlobalConfig).endpoints.rest;
const restEndpoint = config.get('endpoints.rest');
// The browser-id check needs to be skipped on these endpoints
const skipBrowserIdCheckEndpoints = [
// we need to exclude push endpoint because we can't send custom header on websocket requests
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export abstract class BaseCommand extends Command {

protected license: License;

protected globalConfig = Container.get(GlobalConfig);
private globalConfig = Container.get(GlobalConfig);

/**
* How long to wait for graceful shutdown before force killing the process.
Expand Down
10 changes: 4 additions & 6 deletions packages/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ export class Start extends BaseCommand {

private async generateStaticAssets() {
// Read the index file and replace the path placeholder
const n8nPath = this.globalConfig.path;

const n8nPath = Container.get(GlobalConfig).path;
const restEndpoint = config.getEnv('endpoints.rest');
const hooksUrls = config.getEnv('externalFrontendHooksUrls');

let scriptsString = '';
Expand All @@ -151,9 +151,7 @@ export class Start extends BaseCommand {
];
if (filePath.endsWith('index.html')) {
streams.push(
replaceStream('{{REST_ENDPOINT}}', this.globalConfig.endpoints.rest, {
ignoreCase: false,
}),
replaceStream('{{REST_ENDPOINT}}', restEndpoint, { ignoreCase: false }),
replaceStream(closingTitleTag, closingTitleTag + scriptsString, {
ignoreCase: false,
}),
Expand Down Expand Up @@ -203,7 +201,7 @@ export class Start extends BaseCommand {
this.initWorkflowHistory();
this.logger.debug('Workflow history init complete');

if (!this.globalConfig.endpoints.disableUi) {
if (!config.getEnv('endpoints.disableUi')) {
await this.generateStaticAssets();
}
}
Expand Down
Loading

0 comments on commit 91a3552

Please sign in to comment.