Skip to content

Commit 2d925b2

Browse files
committed
feat(backend): add default root http router config
This change adds a default statically defined root http router service factory configuration, which can be disabled by setting the environment variable DISABLE_STATIC_ROOT_HTTP_ROUTER_CONFIG to "true". When this value is set to "true" the backend will not add this configuration statically, which will then allow the service factory configuration to be provided by a dynamic plugin. This change also adds a logger instance to inform the user of all of this as the backend starts up. If the variable is not set and a dynamic plugin tries to install a service factory configuration override, it will fail with an error from the ServiceRegistry as an attempt to register the same service twice. Signed-off-by: Stan Lewis <[email protected]>
1 parent 4505d41 commit 2d925b2

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

packages/backend/src/index.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,58 @@
11
import { createBackend } from '@backstage/backend-defaults';
2+
import { rootHttpRouterServiceFactory } from '@backstage/backend-defaults/rootHttpRouter';
23
import { dynamicPluginsFeatureLoader } from '@backstage/backend-dynamic-feature-service';
34
import { PackageRoles } from '@backstage/cli-node';
45

56
import * as path from 'path';
67

78
import { configureCorporateProxyAgent } from './corporate-proxy';
89
import { CommonJSModuleLoader } from './loader';
9-
import { transports } from './logger';
10+
import { createStaticLogger, transports } from './logger';
1011
import {
1112
healthCheckPlugin,
1213
metricsPlugin,
1314
pluginIDProviderService,
1415
rbacDynamicPluginsProvider,
1516
} from './modules';
1617

18+
// Create a logger to cover logging static initialization tasks
19+
const staticLogger = createStaticLogger({ service: 'developer-hub-init' });
20+
staticLogger.info('Starting Developer Hub backend');
21+
1722
// RHIDP-2217: adds support for corporate proxy
1823
configureCorporateProxyAgent();
1924

2025
const backend = createBackend();
2126

27+
// Install a static root HttpRouterServiceFactory configure function that can
28+
// be explicitly overridden by the user
29+
if (
30+
(process.env.DISABLE_STATIC_ROOT_HTTP_ROUTER_CONFIG || '').toLowerCase() !==
31+
'true'
32+
) {
33+
staticLogger.info('Using static root HttpRouterServiceFactory configuration');
34+
backend.add(
35+
rootHttpRouterServiceFactory({
36+
configure({ app, middleware, routes }) {
37+
if (process.env.NODE_ENV === 'development') {
38+
app.set('json spaces', 2);
39+
}
40+
app.use(middleware.helmet());
41+
app.use(middleware.cors());
42+
app.use(middleware.compression());
43+
app.use(middleware.logging());
44+
app.use(routes);
45+
app.use(middleware.notFound());
46+
app.use(middleware.error());
47+
},
48+
}),
49+
);
50+
} else {
51+
staticLogger.warn(
52+
'Static HttpRouterServiceFactory configuration disabled, allowing custom HttpRouterServiceFactory configuration provided by a dynamic plugin',
53+
);
54+
}
55+
2256
backend.add(
2357
dynamicPluginsFeatureLoader({
2458
schemaLocator(pluginPackage) {

packages/backend/src/logger/customLogger.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WinstonLogger } from '@backstage/backend-defaults/rootLogger';
12
import type { Config } from '@backstage/config';
23

34
import * as winston from 'winston';
@@ -73,3 +74,18 @@ export const transports = {
7374
];
7475
},
7576
};
77+
78+
export const createStaticLogger = ({ service }: { service: string }) => {
79+
const logger = WinstonLogger.create({
80+
meta: {
81+
service,
82+
},
83+
level: process.env.LOG_LEVEL || 'info',
84+
format:
85+
process.env.NODE_ENV === 'production'
86+
? defaultFormat
87+
: WinstonLogger.colorFormat(),
88+
transports: transports.log,
89+
});
90+
return logger;
91+
};

0 commit comments

Comments
 (0)