From 164ce3ee258af2fca57677f4619db0ed1e6b367d Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Thu, 8 Aug 2024 11:00:36 +0200 Subject: [PATCH] refactor(user-settings-backend): deprecate legacy create Router Signed-off-by: Camila Belo --- .changeset/heavy-suits-judge.md | 5 ++ plugins/user-settings-backend/api-report.md | 11 +-- plugins/user-settings-backend/package.json | 3 +- .../src/database/DatabaseUserSettingsStore.ts | 8 +- .../user-settings-backend/src/deprecated.ts | 83 +++++++++++++++++++ plugins/user-settings-backend/src/index.ts | 2 +- .../src/service/router.test.ts | 8 +- .../src/service/router.ts | 3 - yarn.lock | 2 +- 9 files changed, 108 insertions(+), 17 deletions(-) create mode 100644 .changeset/heavy-suits-judge.md create mode 100644 plugins/user-settings-backend/src/deprecated.ts diff --git a/.changeset/heavy-suits-judge.md b/.changeset/heavy-suits-judge.md new file mode 100644 index 0000000000..f3b16a25f3 --- /dev/null +++ b/.changeset/heavy-suits-judge.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-user-settings-backend': minor +--- + +In preparation to stop supporting to the legacy backend system, the `createRouter` function is now deprecated and we strongly recommend you [migrate](https://backstage.io/docs/backend-system/building-backends/migrating) your backend to the new system. diff --git a/plugins/user-settings-backend/api-report.md b/plugins/user-settings-backend/api-report.md index 06dad6a82a..04aecca574 100644 --- a/plugins/user-settings-backend/api-report.md +++ b/plugins/user-settings-backend/api-report.md @@ -8,18 +8,15 @@ import express from 'express'; import { IdentityApi } from '@backstage/plugin-auth-node'; import { SignalsService } from '@backstage/plugin-signals-node'; -// @public +// @public @deprecated export function createRouter(options: RouterOptions): Promise; -// @public (undocumented) -export interface RouterOptions { - // (undocumented) +// @public @deprecated +export type RouterOptions = { database: DatabaseService; - // (undocumented) identity: IdentityApi; - // (undocumented) signals?: SignalsService; -} +}; // (No @packageDocumentation comment for this package) ``` diff --git a/plugins/user-settings-backend/package.json b/plugins/user-settings-backend/package.json index ab91735143..c26c1cd071 100644 --- a/plugins/user-settings-backend/package.json +++ b/plugins/user-settings-backend/package.json @@ -52,7 +52,7 @@ "test": "backstage-cli package test" }, "dependencies": { - "@backstage/backend-common": "workspace:^", + "@backstage/backend-defaults": "workspace:^", "@backstage/backend-plugin-api": "workspace:^", "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", @@ -64,6 +64,7 @@ "express": "^4.17.1", "express-promise-router": "^4.1.0", "knex": "^3.0.0", + "lodash": "^4.17.21", "winston": "^3.2.1", "yn": "^4.0.0" }, diff --git a/plugins/user-settings-backend/src/database/DatabaseUserSettingsStore.ts b/plugins/user-settings-backend/src/database/DatabaseUserSettingsStore.ts index 99c4b2cff4..5a051e8806 100644 --- a/plugins/user-settings-backend/src/database/DatabaseUserSettingsStore.ts +++ b/plugins/user-settings-backend/src/database/DatabaseUserSettingsStore.ts @@ -14,8 +14,10 @@ * limitations under the License. */ -import { PluginDatabaseManager } from '@backstage/backend-common'; -import { resolvePackagePath } from '@backstage/backend-plugin-api'; +import { + resolvePackagePath, + DatabaseService, +} from '@backstage/backend-plugin-api'; import { NotFoundError } from '@backstage/errors'; import { JsonValue } from '@backstage/types'; import { Knex } from 'knex'; @@ -43,7 +45,7 @@ export type RawDbUserSettingsRow = { */ export class DatabaseUserSettingsStore implements UserSettingsStore { static async create(options: { - database: PluginDatabaseManager; + database: DatabaseService; }): Promise { const { database } = options; const client = await database.getClient(); diff --git a/plugins/user-settings-backend/src/deprecated.ts b/plugins/user-settings-backend/src/deprecated.ts new file mode 100644 index 0000000000..04e75df97a --- /dev/null +++ b/plugins/user-settings-backend/src/deprecated.ts @@ -0,0 +1,83 @@ +/* + * 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 express from 'express'; +import { merge } from 'lodash'; +import * as winston from 'winston'; + +import { ConfigReader } from '@backstage/config'; +import { DatabaseService } from '@backstage/backend-plugin-api'; +import { WinstonLogger } from '@backstage/backend-defaults/rootLogger'; +import { MiddlewareFactory } from '@backstage/backend-defaults/rootHttpRouter'; +import { IdentityApi } from '@backstage/plugin-auth-node'; +import { SignalsService } from '@backstage/plugin-signals-node'; + +import { createRouter as _createRouter } from './service'; + +/** + * Type for the options passed to the "createRouter" function. + * + * @public + * @deprecated This type is only exported for legacy reasons and will be removed in the future. + */ +export type RouterOptions = { + database: DatabaseService; + identity: IdentityApi; + signals?: SignalsService; +}; + +/** + * Create the user settings backend routes. + * + * @public + * @deprecated This function is only exported for legacy reasons and will be removed in the future. + * Please {@link https://backstage.io/docs/backend-system/building-backends/migrating | migrate } to use the new backend system and follow these {@link https://github.com/backstage/backstage/tree/master/plugins/user-settings-backend#new-backend | instructions } to install the user settings backend plugin. + */ +export async function createRouter( + options: RouterOptions, +): Promise { + const router = await _createRouter(options); + const config = new ConfigReader({}); + const logger = winston + .createLogger( + merge( + { + level: process.env.LOG_LEVEL || 'info', + format: winston.format.combine( + WinstonLogger.redacter().format, + process.env.NODE_ENV === 'production' + ? winston.format.json() + : WinstonLogger.colorFormat(), + ), + transports: [ + new winston.transports.Console({ + silent: + process.env.JEST_WORKER_ID !== undefined && + !process.env.LOG_LEVEL, + }), + ], + }, + {}, + ), + ) + .child({ service: 'backstage' }); + const middleware = MiddlewareFactory.create({ + config, + logger, + }); + router.use(middleware.error()); + return router; +} diff --git a/plugins/user-settings-backend/src/index.ts b/plugins/user-settings-backend/src/index.ts index 04d8accdaf..395a8570bf 100644 --- a/plugins/user-settings-backend/src/index.ts +++ b/plugins/user-settings-backend/src/index.ts @@ -14,5 +14,5 @@ * limitations under the License. */ -export * from './service'; +export * from './deprecated'; export * from './database'; diff --git a/plugins/user-settings-backend/src/service/router.test.ts b/plugins/user-settings-backend/src/service/router.test.ts index 90b6fb587f..a690096e15 100644 --- a/plugins/user-settings-backend/src/service/router.test.ts +++ b/plugins/user-settings-backend/src/service/router.test.ts @@ -20,6 +20,7 @@ import { UserSettingsStore } from '../database/UserSettingsStore'; import { createRouterInternal } from './router'; import { SignalsService } from '@backstage/plugin-signals-node'; import { mockCredentials, mockServices } from '@backstage/backend-test-utils'; +import { MiddlewareFactory } from '@backstage/backend-defaults/rootHttpRouter'; describe('createRouter', () => { const userSettingsStore: jest.Mocked = { @@ -40,7 +41,12 @@ describe('createRouter', () => { signals: signalService as SignalsService, }); - app = express().use(router); + const errorHandler = MiddlewareFactory.create({ + config: mockServices.rootConfig(), + logger: mockServices.rootLogger(), + }).error(); + + app = express().use(router).use(errorHandler); }); afterEach(() => { diff --git a/plugins/user-settings-backend/src/service/router.ts b/plugins/user-settings-backend/src/service/router.ts index 3efb7da0f9..eacc35a5ae 100644 --- a/plugins/user-settings-backend/src/service/router.ts +++ b/plugins/user-settings-backend/src/service/router.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { errorHandler } from '@backstage/backend-common'; import { AuthenticationError, InputError } from '@backstage/errors'; import { IdentityApi } from '@backstage/plugin-auth-node'; import express, { Request } from 'express'; @@ -156,7 +155,5 @@ export async function createRouterInternal( res.status(204).end(); }); - router.use(errorHandler()); - return router; } diff --git a/yarn.lock b/yarn.lock index f09d8e9695..9bf0c2a708 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7766,7 +7766,6 @@ __metadata: version: 0.0.0-use.local resolution: "@backstage/plugin-user-settings-backend@workspace:plugins/user-settings-backend" dependencies: - "@backstage/backend-common": "workspace:^" "@backstage/backend-defaults": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^" @@ -7782,6 +7781,7 @@ __metadata: express: ^4.17.1 express-promise-router: ^4.1.0 knex: ^3.0.0 + lodash: ^4.17.21 supertest: ^6.1.3 winston: ^3.2.1 yn: ^4.0.0