Fix review comments: remove schemaDiscovery service
Signed-off-by: David Festal <dfestal@redhat.com>
This commit is contained in:
@@ -4,8 +4,8 @@
|
||||
|
||||
```ts
|
||||
import { Config } from '@backstage/config';
|
||||
import { ConfigSchema } from '@backstage/config-loader';
|
||||
import express from 'express';
|
||||
import { JsonObject } from '@backstage/types';
|
||||
import { Logger } from 'winston';
|
||||
import { PluginDatabaseManager } from '@backstage/backend-common';
|
||||
|
||||
@@ -14,9 +14,6 @@ export function createRouter(options: RouterOptions): Promise<express.Router>;
|
||||
|
||||
// @public (undocumented)
|
||||
export interface RouterOptions {
|
||||
additionalSchemas?: {
|
||||
[context: string]: JsonObject;
|
||||
};
|
||||
appPackageName: string;
|
||||
// (undocumented)
|
||||
config: Config;
|
||||
@@ -24,6 +21,7 @@ export interface RouterOptions {
|
||||
disableConfigInjection?: boolean;
|
||||
// (undocumented)
|
||||
logger: Logger;
|
||||
schema?: ConfigSchema;
|
||||
staticFallbackHandler?: express.Handler;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -19,7 +19,11 @@ import { resolve as resolvePath } from 'path';
|
||||
import { Logger } from 'winston';
|
||||
import { AppConfig, Config } from '@backstage/config';
|
||||
import { JsonObject } from '@backstage/types';
|
||||
import { loadConfigSchema, readEnvConfig } from '@backstage/config-loader';
|
||||
import {
|
||||
ConfigSchema,
|
||||
loadConfigSchema,
|
||||
readEnvConfig,
|
||||
} from '@backstage/config-loader';
|
||||
|
||||
type InjectOptions = {
|
||||
appConfigs: AppConfig[];
|
||||
@@ -74,7 +78,7 @@ type ReadOptions = {
|
||||
env: { [name: string]: string | undefined };
|
||||
appDistDir: string;
|
||||
config: Config;
|
||||
additionalSchemas?: { [context: string]: JsonObject };
|
||||
schema?: ConfigSchema;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -91,10 +95,11 @@ export async function readConfigs(options: ReadOptions): Promise<AppConfig[]> {
|
||||
const serializedSchema = await fs.readJson(schemaPath);
|
||||
|
||||
try {
|
||||
const schema = await loadConfigSchema({
|
||||
serialized: serializedSchema,
|
||||
additionalSchemas: options.additionalSchemas,
|
||||
});
|
||||
const schema =
|
||||
options.schema ||
|
||||
(await loadConfigSchema({
|
||||
serialized: serializedSchema,
|
||||
}));
|
||||
|
||||
const frontendConfigs = await schema.process(
|
||||
[{ data: config.get() as JsonObject, context: 'app' }],
|
||||
|
||||
@@ -19,10 +19,13 @@ import {
|
||||
coreServices,
|
||||
createBackendPlugin,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { schemaDiscoveryServiceRef } from '@backstage/backend-plugin-api/alpha';
|
||||
import { createRouter } from './router';
|
||||
import { loggerToWinstonLogger } from '@backstage/backend-common';
|
||||
import { staticFallbackHandlerExtensionPoint } from '@backstage/plugin-app-node';
|
||||
import {
|
||||
configSchemaExtensionPoint,
|
||||
staticFallbackHandlerExtensionPoint,
|
||||
} from '@backstage/plugin-app-node';
|
||||
import { ConfigSchema } from '@backstage/config-loader';
|
||||
|
||||
/**
|
||||
* The App plugin is responsible for serving the frontend app bundle and static assets.
|
||||
@@ -32,6 +35,7 @@ export const appPlugin = createBackendPlugin({
|
||||
pluginId: 'app',
|
||||
register(env) {
|
||||
let staticFallbackHandler: express.Handler | undefined;
|
||||
let schema: ConfigSchema | undefined;
|
||||
|
||||
env.registerExtensionPoint(staticFallbackHandlerExtensionPoint, {
|
||||
setStaticFallbackHandler(handler) {
|
||||
@@ -44,17 +48,28 @@ export const appPlugin = createBackendPlugin({
|
||||
},
|
||||
});
|
||||
|
||||
env.registerExtensionPoint(configSchemaExtensionPoint, {
|
||||
setConfigSchema(configSchema) {
|
||||
if (schema) {
|
||||
throw new Error(
|
||||
'Attempted to set config schema for the app-backend twice',
|
||||
);
|
||||
}
|
||||
schema = configSchema;
|
||||
},
|
||||
});
|
||||
|
||||
env.registerInit({
|
||||
deps: {
|
||||
logger: coreServices.logger,
|
||||
config: coreServices.rootConfig,
|
||||
database: coreServices.database,
|
||||
httpRouter: coreServices.httpRouter,
|
||||
schemaDiscovery: schemaDiscoveryServiceRef,
|
||||
},
|
||||
async init({ logger, config, database, httpRouter, schemaDiscovery }) {
|
||||
async init({ logger, config, database, httpRouter }) {
|
||||
const appPackageName =
|
||||
config.getOptionalString('app.packageName') ?? 'app';
|
||||
|
||||
const winstonLogger = loggerToWinstonLogger(logger);
|
||||
|
||||
const router = await createRouter({
|
||||
@@ -63,9 +78,7 @@ export const appPlugin = createBackendPlugin({
|
||||
database,
|
||||
appPackageName,
|
||||
staticFallbackHandler,
|
||||
additionalSchemas: (
|
||||
await schemaDiscovery?.getAdditionalSchemas()
|
||||
)?.schemas,
|
||||
schema,
|
||||
});
|
||||
httpRouter.use(router);
|
||||
},
|
||||
|
||||
@@ -37,7 +37,7 @@ import {
|
||||
CACHE_CONTROL_NO_CACHE,
|
||||
CACHE_CONTROL_REVALIDATE_CACHE,
|
||||
} from '../lib/headers';
|
||||
import { JsonObject } from '@backstage/types';
|
||||
import { ConfigSchema } from '@backstage/config-loader';
|
||||
|
||||
// express uses mime v1 while we only have types for mime v2
|
||||
type Mime = { lookup(arg0: string): string };
|
||||
@@ -89,14 +89,10 @@ export interface RouterOptions {
|
||||
|
||||
/**
|
||||
*
|
||||
* Provides a map of additional config schemas, in addition to the serialized schemas
|
||||
* generated during the application build.
|
||||
* This is useful when additional plugins are dynamically loaded in the application at start,
|
||||
* which were not part of the application build. This option allows feeding the corresponding
|
||||
* JSON schemas.
|
||||
* Provides a ConfigSchema.
|
||||
*
|
||||
*/
|
||||
additionalSchemas?: { [context: string]: JsonObject };
|
||||
schema?: ConfigSchema;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
@@ -133,7 +129,7 @@ export async function createRouter(
|
||||
config,
|
||||
appDistDir,
|
||||
env: process.env,
|
||||
additionalSchemas: options.additionalSchemas,
|
||||
schema: options.schema,
|
||||
});
|
||||
|
||||
injectedConfigPath = await injectConfig({ appConfigs, logger, staticDir });
|
||||
|
||||
@@ -3,9 +3,23 @@
|
||||
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
||||
|
||||
```ts
|
||||
import { ConfigSchema } from '@backstage/config-loader';
|
||||
import { ExtensionPoint } from '@backstage/backend-plugin-api';
|
||||
import { Handler } from 'express';
|
||||
|
||||
// @public
|
||||
export interface ConfigSchemaExtensionPoint {
|
||||
setConfigSchema(configSchema: ConfigSchema): void;
|
||||
}
|
||||
|
||||
// @public
|
||||
export const configSchemaExtensionPoint: ExtensionPoint<ConfigSchemaExtensionPoint>;
|
||||
|
||||
// @public
|
||||
export function loadCompiledConfigSchema(
|
||||
appDistDir: string,
|
||||
): Promise<ConfigSchema | undefined>;
|
||||
|
||||
// @public
|
||||
export interface StaticFallbackHandlerExtensionPoint {
|
||||
setStaticFallbackHandler(handler: Handler): void;
|
||||
|
||||
@@ -29,7 +29,9 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"@backstage/backend-plugin-api": "workspace:^",
|
||||
"@backstage/config-loader": "workspace:^",
|
||||
"@types/express": "^4.17.6",
|
||||
"express": "^4.17.1"
|
||||
"express": "^4.17.1",
|
||||
"fs-extra": "10.1.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
import { createExtensionPoint } from '@backstage/backend-plugin-api';
|
||||
import { ConfigSchema } from '@backstage/config-loader';
|
||||
import { Handler } from 'express';
|
||||
|
||||
/**
|
||||
@@ -50,3 +51,25 @@ export const staticFallbackHandlerExtensionPoint =
|
||||
createExtensionPoint<StaticFallbackHandlerExtensionPoint>({
|
||||
id: 'app.staticFallbackHandler',
|
||||
});
|
||||
|
||||
/**
|
||||
* The interface for {@link configSchemaExtensionPoint}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface ConfigSchemaExtensionPoint {
|
||||
/**
|
||||
* Sets the config schema. This can only be done once.
|
||||
*/
|
||||
setConfigSchema(configSchema: ConfigSchema): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* An extension point the exposes the ability to override the config schema used by the frontend application.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export const configSchemaExtensionPoint =
|
||||
createExtensionPoint<ConfigSchemaExtensionPoint>({
|
||||
id: 'app.configSchema',
|
||||
});
|
||||
|
||||
@@ -23,4 +23,8 @@
|
||||
export {
|
||||
staticFallbackHandlerExtensionPoint,
|
||||
type StaticFallbackHandlerExtensionPoint,
|
||||
configSchemaExtensionPoint,
|
||||
type ConfigSchemaExtensionPoint,
|
||||
} from './extensions';
|
||||
|
||||
export { loadCompiledConfigSchema } from './schema';
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2024 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import fs from 'fs-extra';
|
||||
import { resolve as resolvePath } from 'path';
|
||||
import { ConfigSchema, loadConfigSchema } from '@backstage/config-loader';
|
||||
|
||||
/**
|
||||
* Loads the config schema that is embedded in the frontend build.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export async function loadCompiledConfigSchema(
|
||||
appDistDir: string,
|
||||
): Promise<ConfigSchema | undefined> {
|
||||
const schemaPath = resolvePath(appDistDir, '.config-schema.json');
|
||||
if (await fs.pathExists(schemaPath)) {
|
||||
const serializedSchema = await fs.readJson(schemaPath);
|
||||
|
||||
return await loadConfigSchema({
|
||||
serialized: serializedSchema,
|
||||
});
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user