From 8869b8ef301b6b41c00f79d06ae2c031f5b52b37 Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Thu, 16 May 2024 14:46:37 +0200 Subject: [PATCH] refactor: stop using the legacy standalone server Signed-off-by: Camila Belo --- .changeset/cyan-snails-peel.md | 12 ++ .changeset/many-moles-sing.md | 5 + plugins/app-backend/README.md | 14 ++- .../src/run.ts => app-backend/dev/index.ts} | 21 +--- plugins/app-backend/package.json | 1 + .../src/service/standaloneServer.ts | 51 --------- .../src/service/standaloneServer.ts | 66 ----------- plugins/catalog-backend/dev/index.ts | 21 ++++ plugins/catalog-backend/package.json | 1 + plugins/catalog-backend/src/run.ts | 33 ------ .../src/service/standaloneServer.ts | 86 -------------- plugins/devtools-backend/dev/index.ts | 21 ++++ plugins/devtools-backend/package.json | 1 + plugins/devtools-backend/src/run.ts | 33 ------ .../src/service/standaloneServer.ts | 69 ------------ .../example-todo-list-backend/dev/index.ts | 21 ++++ .../example-todo-list-backend/package.json | 1 + plugins/example-todo-list-backend/src/run.ts | 33 ------ .../src/service/standaloneServer.ts | 61 ---------- plugins/proxy-backend/dev/index.ts | 21 ++++ plugins/proxy-backend/package.json | 1 + plugins/proxy-backend/src/run.ts | 33 ------ .../src/service/standaloneServer.ts | 61 ---------- .../{src/run.ts => dev/index.ts} | 20 +--- plugins/search-backend/package.json | 1 + .../src/service/standaloneServer.ts | 79 ------------- plugins/techdocs-backend/dev/index.ts | 21 ++++ plugins/techdocs-backend/package.json | 1 + .../src/service/standaloneServer.ts | 105 ------------------ plugins/user-settings-backend/dev/index.ts | 49 ++++++++ plugins/user-settings-backend/package.json | 1 + plugins/user-settings-backend/src/run.ts | 34 ------ .../src/service/standaloneServer.ts | 83 -------------- yarn.lock | 8 ++ 34 files changed, 209 insertions(+), 860 deletions(-) create mode 100644 .changeset/cyan-snails-peel.md create mode 100644 .changeset/many-moles-sing.md rename plugins/{auth-backend/src/run.ts => app-backend/dev/index.ts} (57%) delete mode 100644 plugins/app-backend/src/service/standaloneServer.ts delete mode 100644 plugins/auth-backend/src/service/standaloneServer.ts create mode 100644 plugins/catalog-backend/dev/index.ts delete mode 100644 plugins/catalog-backend/src/run.ts delete mode 100644 plugins/catalog-backend/src/service/standaloneServer.ts create mode 100644 plugins/devtools-backend/dev/index.ts delete mode 100644 plugins/devtools-backend/src/run.ts delete mode 100644 plugins/devtools-backend/src/service/standaloneServer.ts create mode 100644 plugins/example-todo-list-backend/dev/index.ts delete mode 100644 plugins/example-todo-list-backend/src/run.ts delete mode 100644 plugins/example-todo-list-backend/src/service/standaloneServer.ts create mode 100644 plugins/proxy-backend/dev/index.ts delete mode 100644 plugins/proxy-backend/src/run.ts delete mode 100644 plugins/proxy-backend/src/service/standaloneServer.ts rename plugins/search-backend/{src/run.ts => dev/index.ts} (53%) delete mode 100644 plugins/search-backend/src/service/standaloneServer.ts create mode 100644 plugins/techdocs-backend/dev/index.ts delete mode 100644 plugins/techdocs-backend/src/service/standaloneServer.ts create mode 100644 plugins/user-settings-backend/dev/index.ts delete mode 100644 plugins/user-settings-backend/src/run.ts delete mode 100644 plugins/user-settings-backend/src/service/standaloneServer.ts diff --git a/.changeset/cyan-snails-peel.md b/.changeset/cyan-snails-peel.md new file mode 100644 index 0000000000..4bb159b70f --- /dev/null +++ b/.changeset/cyan-snails-peel.md @@ -0,0 +1,12 @@ +--- +'@backstage/plugin-user-settings-backend': patch +'@backstage/plugin-devtools-backend': patch +'@backstage/plugin-techdocs-backend': patch +'@backstage/plugin-catalog-backend': patch +'@backstage/plugin-search-backend': patch +'@backstage/plugin-proxy-backend': patch +'@backstage/plugin-auth-backend': patch +'@backstage/plugin-app-backend': patch +--- + +Migrate the `dev` server to use the new backend system. diff --git a/.changeset/many-moles-sing.md b/.changeset/many-moles-sing.md new file mode 100644 index 0000000000..e1f5203ed6 --- /dev/null +++ b/.changeset/many-moles-sing.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-common': patch +--- + +Deprecate the legacy `createServiceBuilder`. diff --git a/plugins/app-backend/README.md b/plugins/app-backend/README.md index 0f0c235ab5..4e95b90566 100644 --- a/plugins/app-backend/README.md +++ b/plugins/app-backend/README.md @@ -13,7 +13,19 @@ yarn --cwd packages/backend add @backstage/plugin-app-backend app By adding the app package as a dependency we ensure that it is built as part of the backend, and that it can be resolved at runtime. -Now add the plugin router to your app, creating it for example like this: +Now add the plugin to your app, creating it for example like this: + +### New Backend + +```ts +import { createBackend } from '@backstage/backend-defaults'; + +const backend = createBackend(); +backend.add(import('@backstage/plugin-app-backend/alpha')); +backend.start(); +``` + +### Old Backend ```ts const router = await createRouter({ diff --git a/plugins/auth-backend/src/run.ts b/plugins/app-backend/dev/index.ts similarity index 57% rename from plugins/auth-backend/src/run.ts rename to plugins/app-backend/dev/index.ts index 7732bbd41a..43c75d24df 100644 --- a/plugins/auth-backend/src/run.ts +++ b/plugins/app-backend/dev/index.ts @@ -1,5 +1,5 @@ /* - * Copyright 2020 The Backstage Authors + * 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. @@ -14,19 +14,8 @@ * limitations under the License. */ -import { getRootLogger } from '@backstage/backend-common'; -import { startStandaloneServer } from './service/standaloneServer'; +import { createBackend } from '@backstage/backend-defaults'; -const logger = getRootLogger(); - -startStandaloneServer({ logger }).catch(err => { - logger.error(err); - process.exit(1); -}); - -process.on('SIGINT', () => { - logger.info('CTRL+C pressed; exiting.'); - process.exit(0); -}); - -module.hot?.accept(); +const backend = createBackend(); +backend.add(import('../src/alpha')); +backend.start(); diff --git a/plugins/app-backend/package.json b/plugins/app-backend/package.json index d0b07f4041..4d53cdb407 100644 --- a/plugins/app-backend/package.json +++ b/plugins/app-backend/package.json @@ -66,6 +66,7 @@ }, "devDependencies": { "@backstage/backend-app-api": "workspace:^", + "@backstage/backend-defaults": "workspace:^", "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^", "@backstage/types": "workspace:^", diff --git a/plugins/app-backend/src/service/standaloneServer.ts b/plugins/app-backend/src/service/standaloneServer.ts deleted file mode 100644 index f218c9942e..0000000000 --- a/plugins/app-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2020 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 { Server } from 'http'; -import { createServiceBuilder } from '@backstage/backend-common'; -import { Config } from '@backstage/config'; -import { createRouter } from './router'; -import { LoggerService } from '@backstage/backend-plugin-api'; - -export interface ServerOptions { - port: number; - enableCors: boolean; - config: Config; - logger: LoggerService; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'app-backend' }); - logger.debug('Starting application server...'); - const router = await createRouter({ - logger, - config: options.config, - appPackageName: 'example-app', - }); - - const service = createServiceBuilder(module) - .setPort(options.port) - .addRouter('', router); - - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} - -module.hot?.accept(); diff --git a/plugins/auth-backend/src/service/standaloneServer.ts b/plugins/auth-backend/src/service/standaloneServer.ts deleted file mode 100644 index 834305553b..0000000000 --- a/plugins/auth-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2020 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 { - createServiceBuilder, - loadBackendConfig, - ServerTokenManager, - HostDiscovery, - DatabaseManager, -} from '@backstage/backend-common'; -import { Server } from 'http'; -import { LoggerService } from '@backstage/backend-plugin-api'; -import { createRouter } from './router'; -import { ConfigReader } from '@backstage/config'; - -export interface ServerOptions { - logger: LoggerService; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'auth-backend' }); - const config = await loadBackendConfig({ logger, argv: process.argv }); - const discovery = HostDiscovery.fromConfig(config); - - const manager = DatabaseManager.fromConfig( - new ConfigReader({ - backend: { - database: { client: 'better-sqlite3', connection: ':memory:' }, - }, - }), - ); - const database = manager.forPlugin('auth'); - - logger.debug('Starting application server...'); - const router = await createRouter({ - logger, - config, - database, - discovery, - tokenManager: ServerTokenManager.noop(), - }); - - const service = createServiceBuilder(module) - .enableCors({ origin: 'http://localhost:3000', credentials: true }) - .addRouter('/auth', router); - - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} diff --git a/plugins/catalog-backend/dev/index.ts b/plugins/catalog-backend/dev/index.ts new file mode 100644 index 0000000000..43c75d24df --- /dev/null +++ b/plugins/catalog-backend/dev/index.ts @@ -0,0 +1,21 @@ +/* + * 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 { createBackend } from '@backstage/backend-defaults'; + +const backend = createBackend(); +backend.add(import('../src/alpha')); +backend.start(); diff --git a/plugins/catalog-backend/package.json b/plugins/catalog-backend/package.json index 44f35dccf2..bdfcecf2b1 100644 --- a/plugins/catalog-backend/package.json +++ b/plugins/catalog-backend/package.json @@ -91,6 +91,7 @@ "zod": "^3.22.4" }, "devDependencies": { + "@backstage/backend-defaults": "workspace:^", "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^", "@backstage/plugin-permission-common": "workspace:^", diff --git a/plugins/catalog-backend/src/run.ts b/plugins/catalog-backend/src/run.ts deleted file mode 100644 index 0a3ed2b7f0..0000000000 --- a/plugins/catalog-backend/src/run.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2020 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 { getRootLogger } from '@backstage/backend-common'; -import yn from 'yn'; -import { startStandaloneServer } from './service/standaloneServer'; - -const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 7007; -const enableCors = yn(process.env.PLUGIN_CORS, { default: false }); -const logger = getRootLogger(); - -startStandaloneServer({ port, enableCors, logger }).catch(err => { - logger.error(err); - process.exit(1); -}); - -process.on('SIGINT', () => { - logger.info('CTRL+C pressed; exiting.'); - process.exit(0); -}); diff --git a/plugins/catalog-backend/src/service/standaloneServer.ts b/plugins/catalog-backend/src/service/standaloneServer.ts deleted file mode 100644 index 0755ab6301..0000000000 --- a/plugins/catalog-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2020 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 { - createServiceBuilder, - DatabaseManager, - HostDiscovery, - loadBackendConfig, - ServerTokenManager, - UrlReaders, -} from '@backstage/backend-common'; -import { ConfigReader } from '@backstage/config'; -import { ServerPermissionClient } from '@backstage/plugin-permission-node'; -import { Server } from 'http'; -import { applyDatabaseMigrations } from '../database/migrations'; -import { CatalogBuilder } from './CatalogBuilder'; -import { LoggerService } from '@backstage/backend-plugin-api'; - -export interface ServerOptions { - port: number; - enableCors: boolean; - logger: LoggerService; -} - -// TODO(freben): Migrate to the next catalog when it's in place -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'catalog-backend' }); - const config = await loadBackendConfig({ logger, argv: process.argv }); - const reader = UrlReaders.default({ logger, config }); - const manager = DatabaseManager.fromConfig( - new ConfigReader({ - backend: { - database: { client: 'better-sqlite3', connection: ':memory:' }, - }, - }), - ); - const database = manager.forPlugin('catalog'); - const discovery = HostDiscovery.fromConfig(config); - const tokenManager = ServerTokenManager.fromConfig(config, { - logger, - }); - const permissions = ServerPermissionClient.fromConfig(config, { - discovery, - tokenManager, - }); - - logger.debug('Creating application...'); - await applyDatabaseMigrations(await database.getClient()); - const builder = CatalogBuilder.create({ - logger, - database, - config, - reader, - permissions, - }); - const catalog = await builder.build(); - - logger.debug('Starting application server...'); - let service = createServiceBuilder(module) - .setPort(options.port) - .addRouter('/catalog', catalog.router); - if (options.enableCors) { - service = service.enableCors({ origin: 'http://localhost:3000' }); - } - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} - -module.hot?.accept(); diff --git a/plugins/devtools-backend/dev/index.ts b/plugins/devtools-backend/dev/index.ts new file mode 100644 index 0000000000..dc287d46cc --- /dev/null +++ b/plugins/devtools-backend/dev/index.ts @@ -0,0 +1,21 @@ +/* + * 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 { createBackend } from '@backstage/backend-defaults'; + +const backend = createBackend(); +backend.add(import('../src')); +backend.start(); diff --git a/plugins/devtools-backend/package.json b/plugins/devtools-backend/package.json index 00d8df80ae..35695d05cf 100644 --- a/plugins/devtools-backend/package.json +++ b/plugins/devtools-backend/package.json @@ -52,6 +52,7 @@ "yn": "^4.0.0" }, "devDependencies": { + "@backstage/backend-defaults": "workspace:^", "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^", "@types/ping": "^0.4.1", diff --git a/plugins/devtools-backend/src/run.ts b/plugins/devtools-backend/src/run.ts deleted file mode 100644 index d945aa13f0..0000000000 --- a/plugins/devtools-backend/src/run.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2022 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 { getRootLogger } from '@backstage/backend-common'; -import yn from 'yn'; -import { startStandaloneServer } from './service/standaloneServer'; - -const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 7007; -const enableCors = yn(process.env.PLUGIN_CORS, { default: false }); -const logger = getRootLogger(); - -startStandaloneServer({ port, enableCors, logger }).catch(err => { - logger.error(err); - process.exit(1); -}); - -process.on('SIGINT', () => { - logger.info('CTRL+C pressed; exiting.'); - process.exit(0); -}); diff --git a/plugins/devtools-backend/src/service/standaloneServer.ts b/plugins/devtools-backend/src/service/standaloneServer.ts deleted file mode 100644 index 136db7bf89..0000000000 --- a/plugins/devtools-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2022 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 { - createServiceBuilder, - HostDiscovery, - loadBackendConfig, - ServerTokenManager, -} from '@backstage/backend-common'; - -import { Server } from 'http'; -import { ServerPermissionClient } from '@backstage/plugin-permission-node'; -import { createRouter } from './router'; -import { LoggerService } from '@backstage/backend-plugin-api'; - -export interface ServerOptions { - port: number; - enableCors: boolean; - logger: LoggerService; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'devtools-backend-backend' }); - const config = await loadBackendConfig({ logger, argv: process.argv }); - const discovery = HostDiscovery.fromConfig(config); - const tokenManager = ServerTokenManager.fromConfig(config, { - logger, - }); - const permissions = ServerPermissionClient.fromConfig(config, { - discovery, - tokenManager, - }); - logger.debug('Starting application server...'); - const router = await createRouter({ - logger, - config, - permissions, - discovery, - }); - - let service = createServiceBuilder(module) - .setPort(options.port) - .addRouter('/devtools-backend', router); - if (options.enableCors) { - service = service.enableCors({ origin: 'http://localhost:3000' }); - } - - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} - -module.hot?.accept(); diff --git a/plugins/example-todo-list-backend/dev/index.ts b/plugins/example-todo-list-backend/dev/index.ts new file mode 100644 index 0000000000..dc287d46cc --- /dev/null +++ b/plugins/example-todo-list-backend/dev/index.ts @@ -0,0 +1,21 @@ +/* + * 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 { createBackend } from '@backstage/backend-defaults'; + +const backend = createBackend(); +backend.add(import('../src')); +backend.start(); diff --git a/plugins/example-todo-list-backend/package.json b/plugins/example-todo-list-backend/package.json index 15a9d16ee9..e8cd617be0 100644 --- a/plugins/example-todo-list-backend/package.json +++ b/plugins/example-todo-list-backend/package.json @@ -43,6 +43,7 @@ "yn": "^4.0.0" }, "devDependencies": { + "@backstage/backend-defaults": "workspace:^", "@backstage/cli": "workspace:^", "@types/supertest": "^2.0.8", "@types/uuid": "^9.0.0", diff --git a/plugins/example-todo-list-backend/src/run.ts b/plugins/example-todo-list-backend/src/run.ts deleted file mode 100644 index 0a3ed2b7f0..0000000000 --- a/plugins/example-todo-list-backend/src/run.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2020 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 { getRootLogger } from '@backstage/backend-common'; -import yn from 'yn'; -import { startStandaloneServer } from './service/standaloneServer'; - -const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 7007; -const enableCors = yn(process.env.PLUGIN_CORS, { default: false }); -const logger = getRootLogger(); - -startStandaloneServer({ port, enableCors, logger }).catch(err => { - logger.error(err); - process.exit(1); -}); - -process.on('SIGINT', () => { - logger.info('CTRL+C pressed; exiting.'); - process.exit(0); -}); diff --git a/plugins/example-todo-list-backend/src/service/standaloneServer.ts b/plugins/example-todo-list-backend/src/service/standaloneServer.ts deleted file mode 100644 index 7ed0c064b7..0000000000 --- a/plugins/example-todo-list-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2020 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 { - createServiceBuilder, - HostDiscovery, - loadBackendConfig, -} from '@backstage/backend-common'; -import { DefaultIdentityClient } from '@backstage/plugin-auth-node'; -import { Server } from 'http'; -import { createRouter } from './router'; -import { LoggerService } from '@backstage/backend-plugin-api'; - -export interface ServerOptions { - port: number; - enableCors: boolean; - logger: LoggerService; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'todo-list-backend' }); - logger.debug('Starting application server...'); - const config = await loadBackendConfig({ logger, argv: process.argv }); - const discovery = HostDiscovery.fromConfig(config); - const router = await createRouter({ - logger, - identity: DefaultIdentityClient.create({ - discovery, - issuer: await discovery.getExternalBaseUrl('auth'), - }), - }); - - let service = createServiceBuilder(module) - .setPort(options.port) - .addRouter('/todo-list', router); - if (options.enableCors) { - service = service.enableCors({ origin: 'http://localhost:3000' }); - } - - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} - -module.hot?.accept(); diff --git a/plugins/proxy-backend/dev/index.ts b/plugins/proxy-backend/dev/index.ts new file mode 100644 index 0000000000..43c75d24df --- /dev/null +++ b/plugins/proxy-backend/dev/index.ts @@ -0,0 +1,21 @@ +/* + * 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 { createBackend } from '@backstage/backend-defaults'; + +const backend = createBackend(); +backend.add(import('../src/alpha')); +backend.start(); diff --git a/plugins/proxy-backend/package.json b/plugins/proxy-backend/package.json index 0614956a18..039ceb3898 100644 --- a/plugins/proxy-backend/package.json +++ b/plugins/proxy-backend/package.json @@ -64,6 +64,7 @@ "yup": "^1.0.0" }, "devDependencies": { + "@backstage/backend-defaults": "workspace:^", "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^", "@backstage/config-loader": "workspace:^", diff --git a/plugins/proxy-backend/src/run.ts b/plugins/proxy-backend/src/run.ts deleted file mode 100644 index 0a3ed2b7f0..0000000000 --- a/plugins/proxy-backend/src/run.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2020 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 { getRootLogger } from '@backstage/backend-common'; -import yn from 'yn'; -import { startStandaloneServer } from './service/standaloneServer'; - -const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 7007; -const enableCors = yn(process.env.PLUGIN_CORS, { default: false }); -const logger = getRootLogger(); - -startStandaloneServer({ port, enableCors, logger }).catch(err => { - logger.error(err); - process.exit(1); -}); - -process.on('SIGINT', () => { - logger.info('CTRL+C pressed; exiting.'); - process.exit(0); -}); diff --git a/plugins/proxy-backend/src/service/standaloneServer.ts b/plugins/proxy-backend/src/service/standaloneServer.ts deleted file mode 100644 index 44a1e34090..0000000000 --- a/plugins/proxy-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2020 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 { - createServiceBuilder, - loadBackendConfig, - HostDiscovery, -} from '@backstage/backend-common'; -import { Server } from 'http'; -import { Logger } from 'winston'; -import { createRouter } from './router'; - -export interface ServerOptions { - port: number; - enableCors: boolean; - logger: Logger; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'proxy-backend' }); - - logger.debug('Creating application...'); - - const config = await loadBackendConfig({ logger, argv: process.argv }); - const discovery = HostDiscovery.fromConfig(config); - const router = await createRouter({ - config, - logger, - discovery, - }); - let service = createServiceBuilder(module) - .setPort(options.port) - .addRouter('/proxy', router); - if (options.enableCors) { - service = service.enableCors({ origin: 'http://localhost:3000' }); - } - - logger.debug('Starting application server...'); - - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} - -module.hot?.accept(); diff --git a/plugins/search-backend/src/run.ts b/plugins/search-backend/dev/index.ts similarity index 53% rename from plugins/search-backend/src/run.ts rename to plugins/search-backend/dev/index.ts index 53d4e4334a..9c108c0de1 100644 --- a/plugins/search-backend/src/run.ts +++ b/plugins/search-backend/dev/index.ts @@ -14,20 +14,8 @@ * limitations under the License. */ -import { getRootLogger } from '@backstage/backend-common'; -import yn from 'yn'; -import { startStandaloneServer } from './service/standaloneServer'; +import { createBackend } from '@backstage/backend-defaults'; -const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 7007; -const enableCors = yn(process.env.PLUGIN_CORS, { default: false }); -const logger = getRootLogger(); - -startStandaloneServer({ port, enableCors, logger }).catch(err => { - logger.error(err); - process.exit(1); -}); - -process.on('SIGINT', () => { - logger.info('CTRL+C pressed; exiting.'); - process.exit(0); -}); +const backend = createBackend(); +backend.add(import('../src/alpha')); +backend.start(); diff --git a/plugins/search-backend/package.json b/plugins/search-backend/package.json index 146e50d177..06a6b5a3ff 100644 --- a/plugins/search-backend/package.json +++ b/plugins/search-backend/package.json @@ -49,6 +49,7 @@ }, "dependencies": { "@backstage/backend-common": "workspace:^", + "@backstage/backend-defaults": "workspace:^", "@backstage/backend-openapi-utils": "workspace:^", "@backstage/backend-plugin-api": "workspace:^", "@backstage/config": "workspace:^", diff --git a/plugins/search-backend/src/service/standaloneServer.ts b/plugins/search-backend/src/service/standaloneServer.ts deleted file mode 100644 index 09fa4f87ee..0000000000 --- a/plugins/search-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2021 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 { - createServiceBuilder, - loadBackendConfig, - ServerTokenManager, - HostDiscovery, -} from '@backstage/backend-common'; -import { Server } from 'http'; -import { Logger } from 'winston'; -import { createRouter } from './router'; -import { - LunrSearchEngine, - IndexBuilder, -} from '@backstage/plugin-search-backend-node'; -import { ServerPermissionClient } from '@backstage/plugin-permission-node'; - -export interface ServerOptions { - port: number; - enableCors: boolean; - logger: Logger; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'search-backend' }); - const config = await loadBackendConfig({ logger, argv: process.argv }); - const searchEngine = new LunrSearchEngine({ logger }); - const indexBuilder = new IndexBuilder({ logger, searchEngine }); - const discovery = HostDiscovery.fromConfig(config); - const tokenManager = ServerTokenManager.fromConfig(config, { - logger, - }); - const permissions = ServerPermissionClient.fromConfig(config, { - discovery, - tokenManager, - }); - logger.debug('Starting application server...'); - - // TODO: stub out some documents/indices? - - const router = await createRouter({ - engine: indexBuilder.getSearchEngine(), - types: indexBuilder.getDocumentTypes(), - discovery, - permissions, - config, - logger, - }); - - let service = createServiceBuilder(module) - .setPort(options.port) - .addRouter('/search', router); - if (options.enableCors) { - service = service.enableCors({ origin: 'http://localhost:3000' }); - } - - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} - -module.hot?.accept(); diff --git a/plugins/techdocs-backend/dev/index.ts b/plugins/techdocs-backend/dev/index.ts new file mode 100644 index 0000000000..43c75d24df --- /dev/null +++ b/plugins/techdocs-backend/dev/index.ts @@ -0,0 +1,21 @@ +/* + * 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 { createBackend } from '@backstage/backend-defaults'; + +const backend = createBackend(); +backend.add(import('../src/alpha')); +backend.start(); diff --git a/plugins/techdocs-backend/package.json b/plugins/techdocs-backend/package.json index a5a8e0ba76..89e08878b6 100644 --- a/plugins/techdocs-backend/package.json +++ b/plugins/techdocs-backend/package.json @@ -73,6 +73,7 @@ "winston": "^3.2.1" }, "devDependencies": { + "@backstage/backend-defaults": "workspace:^", "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^", "@types/dockerode": "^3.3.0", diff --git a/plugins/techdocs-backend/src/service/standaloneServer.ts b/plugins/techdocs-backend/src/service/standaloneServer.ts deleted file mode 100644 index ce981633ca..0000000000 --- a/plugins/techdocs-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2020 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 { - CacheManager, - createServiceBuilder, - DockerContainerRunner, - HostDiscovery, - UrlReader, -} from '@backstage/backend-common'; -import { ConfigReader } from '@backstage/config'; -import { - DirectoryPreparer, - Generators, - Preparers, - Publisher, - TechdocsGenerator, -} from '@backstage/plugin-techdocs-node'; -import Docker from 'dockerode'; -import { Server } from 'http'; -import { Logger } from 'winston'; -import { createRouter } from './router'; - -export interface ServerOptions { - port: number; - enableCors: boolean; - logger: Logger; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'techdocs-backend' }); - const config = new ConfigReader({ - techdocs: { - publisher: { - type: 'local', - }, - }, - }); - const discovery = HostDiscovery.fromConfig(config); - const mockUrlReader: jest.Mocked = { - readUrl: jest.fn(), - readTree: jest.fn(), - search: jest.fn(), - }; - - logger.debug('Creating application...'); - const preparers = new Preparers(); - const directoryPreparer = DirectoryPreparer.fromConfig(config, { - logger, - reader: mockUrlReader, - }); - preparers.register('dir', directoryPreparer); - - const dockerClient = new Docker(); - const containerRunner = new DockerContainerRunner({ dockerClient }); - - const generators = new Generators(); - const techdocsGenerator = TechdocsGenerator.fromConfig(config, { - logger, - containerRunner, - }); - generators.register('techdocs', techdocsGenerator); - - const publisher = await Publisher.fromConfig(config, { logger, discovery }); - - const cache = CacheManager.fromConfig(config).forPlugin('techdocs'); - - logger.debug('Starting application server...'); - const router = await createRouter({ - preparers, - generators, - logger, - publisher, - config, - discovery, - cache, - }); - let service = createServiceBuilder(module) - .setPort(options.port) - .addRouter('/techdocs', router); - if (options.enableCors) { - service = service.enableCors({ origin: 'http://localhost:3000' }); - } - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} - -module.hot?.accept(); diff --git a/plugins/user-settings-backend/dev/index.ts b/plugins/user-settings-backend/dev/index.ts new file mode 100644 index 0000000000..31963edc95 --- /dev/null +++ b/plugins/user-settings-backend/dev/index.ts @@ -0,0 +1,49 @@ +/* + * 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 { createBackend } from '@backstage/backend-defaults'; +import { + coreServices, + createServiceFactory, +} from '@backstage/backend-plugin-api'; +import { IdentityApi } from '@backstage/plugin-auth-node'; + +const identityMock: IdentityApi = { + async getIdentity({ request }) { + const token = request.headers.authorization?.split(' ')[1]; + return { + identity: { + type: 'user', + ownershipEntityRefs: [], + userEntityRef: token || 'user:default/john_doe', + }, + token: token || 'no-token', + }; + }, +}; + +const backend = createBackend(); +backend.add( + createServiceFactory(() => ({ + service: coreServices.identity, + deps: {}, + async factory() { + return identityMock; + }, + })), +); +backend.add(import('../src/alpha')); +backend.start(); diff --git a/plugins/user-settings-backend/package.json b/plugins/user-settings-backend/package.json index ac5e5d9af2..1016d59256 100644 --- a/plugins/user-settings-backend/package.json +++ b/plugins/user-settings-backend/package.json @@ -60,6 +60,7 @@ "yn": "^4.0.0" }, "devDependencies": { + "@backstage/backend-defaults": "workspace:^", "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^", "@types/supertest": "^2.0.8", diff --git a/plugins/user-settings-backend/src/run.ts b/plugins/user-settings-backend/src/run.ts deleted file mode 100644 index 178de0f786..0000000000 --- a/plugins/user-settings-backend/src/run.ts +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2022 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 { getRootLogger } from '@backstage/backend-common'; -import yn from 'yn'; - -import { startStandaloneServer } from './service/standaloneServer'; - -const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 7007; -const enableCors = yn(process.env.PLUGIN_CORS, { default: false }); -const logger = getRootLogger(); - -startStandaloneServer({ port, enableCors, logger }).catch(err => { - logger.error(err); - process.exit(1); -}); - -process.on('SIGINT', () => { - logger.info('CTRL+C pressed; exiting.'); - process.exit(0); -}); diff --git a/plugins/user-settings-backend/src/service/standaloneServer.ts b/plugins/user-settings-backend/src/service/standaloneServer.ts deleted file mode 100644 index 48a640636f..0000000000 --- a/plugins/user-settings-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2022 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 { - createServiceBuilder, - DatabaseManager, -} from '@backstage/backend-common'; -import { ConfigReader } from '@backstage/config'; -import { IdentityApi } from '@backstage/plugin-auth-node'; -import { Server } from 'http'; -import { Logger } from 'winston'; -import { DatabaseUserSettingsStore } from '../database/DatabaseUserSettingsStore'; -import { createRouterInternal } from './router'; - -export interface ServerOptions { - port: number; - enableCors: boolean; - logger: Logger; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'storage-backend' }); - - const manager = DatabaseManager.fromConfig( - new ConfigReader({ - backend: { - database: { client: 'better-sqlite3', connection: ':memory:' }, - }, - }), - ); - const database = manager.forPlugin('user-settings'); - - logger.debug('Starting application server...'); - - const identityMock: IdentityApi = { - async getIdentity({ request }) { - const token = request.headers.authorization?.split(' ')[1]; - return { - identity: { - type: 'user', - ownershipEntityRefs: [], - userEntityRef: token || 'user:default/john_doe', - }, - token: token || 'no-token', - }; - }, - }; - - const router = await createRouterInternal({ - userSettingsStore: await DatabaseUserSettingsStore.create({ database }), - identity: identityMock, - }); - - let service = createServiceBuilder(module) - .setPort(options.port) - .addRouter('/user-settings', router); - - if (options.enableCors) { - service = service.enableCors({ origin: 'http://localhost:3000' }); - } - - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} - -module.hot?.accept(); diff --git a/yarn.lock b/yarn.lock index 728bac0c68..9b6baf6048 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4351,6 +4351,7 @@ __metadata: dependencies: "@backstage/backend-app-api": "workspace:^" "@backstage/backend-common": "workspace:^" + "@backstage/backend-defaults": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^" "@backstage/cli": "workspace:^" @@ -5268,6 +5269,7 @@ __metadata: resolution: "@backstage/plugin-catalog-backend@workspace:plugins/catalog-backend" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-defaults": "workspace:^" "@backstage/backend-openapi-utils": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-tasks": "workspace:^" @@ -5587,6 +5589,7 @@ __metadata: resolution: "@backstage/plugin-devtools-backend@workspace:plugins/devtools-backend" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-defaults": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^" "@backstage/cli": "workspace:^" @@ -6331,6 +6334,7 @@ __metadata: resolution: "@backstage/plugin-proxy-backend@workspace:plugins/proxy-backend" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-defaults": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^" "@backstage/cli": "workspace:^" @@ -7016,6 +7020,7 @@ __metadata: resolution: "@backstage/plugin-search-backend@workspace:plugins/search-backend" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-defaults": "workspace:^" "@backstage/backend-openapi-utils": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^" @@ -7245,6 +7250,7 @@ __metadata: resolution: "@backstage/plugin-techdocs-backend@workspace:plugins/techdocs-backend" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-defaults": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^" "@backstage/catalog-client": "workspace:^" @@ -7428,6 +7434,7 @@ __metadata: 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:^" "@backstage/cli": "workspace:^" @@ -9482,6 +9489,7 @@ __metadata: resolution: "@internal/plugin-todo-list-backend@workspace:plugins/example-todo-list-backend" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-defaults": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/errors": "workspace:^"