From f19b7e2f68f19d6759c4d5fc849262fce9adc11a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 31 May 2022 14:41:35 +0200 Subject: [PATCH 01/31] packages: add initial backend app and plugin API packages Signed-off-by: Patrik Oldsberg --- packages/backend-app-api/.eslintrc.js | 1 + packages/backend-app-api/README.md | 17 +++++++++ packages/backend-app-api/package.json | 44 ++++++++++++++++++++++++ packages/backend-app-api/src/index.ts | 23 +++++++++++++ packages/backend-plugin-api/.eslintrc.js | 1 + packages/backend-plugin-api/README.md | 17 +++++++++ packages/backend-plugin-api/package.json | 44 ++++++++++++++++++++++++ packages/backend-plugin-api/src/index.ts | 23 +++++++++++++ 8 files changed, 170 insertions(+) create mode 100644 packages/backend-app-api/.eslintrc.js create mode 100644 packages/backend-app-api/README.md create mode 100644 packages/backend-app-api/package.json create mode 100644 packages/backend-app-api/src/index.ts create mode 100644 packages/backend-plugin-api/.eslintrc.js create mode 100644 packages/backend-plugin-api/README.md create mode 100644 packages/backend-plugin-api/package.json create mode 100644 packages/backend-plugin-api/src/index.ts diff --git a/packages/backend-app-api/.eslintrc.js b/packages/backend-app-api/.eslintrc.js new file mode 100644 index 0000000000..e2a53a6ad2 --- /dev/null +++ b/packages/backend-app-api/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require('@backstage/cli/config/eslint-factory')(__dirname); diff --git a/packages/backend-app-api/README.md b/packages/backend-app-api/README.md new file mode 100644 index 0000000000..295a04c7a1 --- /dev/null +++ b/packages/backend-app-api/README.md @@ -0,0 +1,17 @@ +# @backstage/backend-app-api + +This package provides the core API used by Backstage backend apps. + +## Installation + +Add the library to your backend app package: + +```bash +# From your Backstage root directory +yarn add --cwd packages/backend @backstage/backend-app-api +``` + +## Documentation + +- [Backstage Readme](https://github.com/backstage/backstage/blob/master/README.md) +- [Backstage Documentation](https://github.com/backstage/backstage/blob/master/docs/README.md) diff --git a/packages/backend-app-api/package.json b/packages/backend-app-api/package.json new file mode 100644 index 0000000000..fe2d517086 --- /dev/null +++ b/packages/backend-app-api/package.json @@ -0,0 +1,44 @@ +{ + "name": "@backstage/backend-app-api", + "description": "Core API used by Backstage backend apps", + "version": "0.0.0", + "main": "src/index.ts", + "types": "src/index.ts", + "private": false, + "publishConfig": { + "access": "public", + "main": "dist/index.cjs.js", + "types": "dist/index.d.ts", + "alphaTypes": "dist/index.alpha.d.ts" + }, + "backstage": { + "role": "node-library" + }, + "homepage": "https://backstage.io", + "repository": { + "type": "git", + "url": "https://github.com/backstage/backstage", + "directory": "packages/backend-app-api" + }, + "keywords": [ + "backstage" + ], + "license": "Apache-2.0", + "scripts": { + "build": "backstage-cli package build --experimental-type-build", + "lint": "backstage-cli package lint", + "test": "backstage-cli package test", + "prepack": "backstage-cli package prepack", + "postpack": "backstage-cli package postpack", + "clean": "backstage-cli package clean", + "start": "backstage-cli package start" + }, + "dependencies": {}, + "devDependencies": { + "@backstage/cli": "^0.17.2-next.0" + }, + "files": [ + "dist", + "alpha" + ] +} diff --git a/packages/backend-app-api/src/index.ts b/packages/backend-app-api/src/index.ts new file mode 100644 index 0000000000..1888a9b037 --- /dev/null +++ b/packages/backend-app-api/src/index.ts @@ -0,0 +1,23 @@ +/* + * 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. + */ + +/** + * Core API used by Backstage backend apps. + * + * @packageDocumentation + */ + +export {}; diff --git a/packages/backend-plugin-api/.eslintrc.js b/packages/backend-plugin-api/.eslintrc.js new file mode 100644 index 0000000000..e2a53a6ad2 --- /dev/null +++ b/packages/backend-plugin-api/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require('@backstage/cli/config/eslint-factory')(__dirname); diff --git a/packages/backend-plugin-api/README.md b/packages/backend-plugin-api/README.md new file mode 100644 index 0000000000..9e28e220a1 --- /dev/null +++ b/packages/backend-plugin-api/README.md @@ -0,0 +1,17 @@ +# @backstage/backend-plugin-api + +This package provides the core API used by Backstage backend plugins. + +## Installation + +Add the library to your backend plugin package: + +```bash +# From your Backstage root directory +yarn add --cwd plugin/-backend @backstage/backend-plugin-api +``` + +## Documentation + +- [Backstage Readme](https://github.com/backstage/backstage/blob/master/README.md) +- [Backstage Documentation](https://github.com/backstage/backstage/blob/master/docs/README.md) diff --git a/packages/backend-plugin-api/package.json b/packages/backend-plugin-api/package.json new file mode 100644 index 0000000000..967ae734ee --- /dev/null +++ b/packages/backend-plugin-api/package.json @@ -0,0 +1,44 @@ +{ + "name": "@backstage/backend-plugin-api", + "description": "Core API used by Backstage backend plugins", + "version": "0.0.0", + "main": "src/index.ts", + "types": "src/index.ts", + "private": false, + "publishConfig": { + "access": "public", + "main": "dist/index.cjs.js", + "types": "dist/index.d.ts", + "alphaTypes": "dist/index.alpha.d.ts" + }, + "backstage": { + "role": "node-library" + }, + "homepage": "https://backstage.io", + "repository": { + "type": "git", + "url": "https://github.com/backstage/backstage", + "directory": "packages/backend-plugin-api" + }, + "keywords": [ + "backstage" + ], + "license": "Apache-2.0", + "scripts": { + "build": "backstage-cli package build --experimental-type-build", + "lint": "backstage-cli package lint", + "test": "backstage-cli package test", + "prepack": "backstage-cli package prepack", + "postpack": "backstage-cli package postpack", + "clean": "backstage-cli package clean", + "start": "backstage-cli package start" + }, + "dependencies": {}, + "devDependencies": { + "@backstage/cli": "^0.17.2-next.0" + }, + "files": [ + "dist", + "alpha" + ] +} diff --git a/packages/backend-plugin-api/src/index.ts b/packages/backend-plugin-api/src/index.ts new file mode 100644 index 0000000000..caef25e2ad --- /dev/null +++ b/packages/backend-plugin-api/src/index.ts @@ -0,0 +1,23 @@ +/* + * 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. + */ + +/** + * Core API used by Backstage backend plugins. + * + * @packageDocumentation + */ + +export {}; From 4ad817262cd48445bc0260c621405a0fc5621717 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 1 Jun 2022 11:24:40 +0200 Subject: [PATCH 02/31] backend-system: initial high-level API surface split Signed-off-by: Patrik Oldsberg --- packages/backend-app-api/src/wiring/types.ts | 27 ++++++ packages/backend-plugin-api/src/index.ts | 2 +- .../src/services/definitions/types.ts | 52 ++++++++++++ .../src/services/system/types.ts | 31 +++++++ .../backend-plugin-api/src/wiring/index.ts | 17 ++++ .../backend-plugin-api/src/wiring/types.ts | 85 +++++++++++++++++++ 6 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 packages/backend-app-api/src/wiring/types.ts create mode 100644 packages/backend-plugin-api/src/services/definitions/types.ts create mode 100644 packages/backend-plugin-api/src/services/system/types.ts create mode 100644 packages/backend-plugin-api/src/wiring/index.ts create mode 100644 packages/backend-plugin-api/src/wiring/types.ts diff --git a/packages/backend-app-api/src/wiring/types.ts b/packages/backend-app-api/src/wiring/types.ts new file mode 100644 index 0000000000..cf600ba1d0 --- /dev/null +++ b/packages/backend-app-api/src/wiring/types.ts @@ -0,0 +1,27 @@ +/* + * 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. + */ + +export interface BackendApp { + add(registrable: BackendRegistrable): void; +} + +interface CreateBackendOptions { + apis: AnyServiceFactory[]; +} + +export function createBackend(options?: CreateBackendOptions): Backend { + return new BackstageBackend(options?.apis ?? []); +} diff --git a/packages/backend-plugin-api/src/index.ts b/packages/backend-plugin-api/src/index.ts index caef25e2ad..da51896950 100644 --- a/packages/backend-plugin-api/src/index.ts +++ b/packages/backend-plugin-api/src/index.ts @@ -20,4 +20,4 @@ * @packageDocumentation */ -export {}; +export * from './wiring'; diff --git a/packages/backend-plugin-api/src/services/definitions/types.ts b/packages/backend-plugin-api/src/services/definitions/types.ts new file mode 100644 index 0000000000..8e85e1198d --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/types.ts @@ -0,0 +1,52 @@ +/* + * 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. + */ + +interface Logger { + log(message: string): void; + child(fields: { [name: string]: string }): Logger; +} + +interface ConfigApi { + getString(key: string): string; +} + +interface HttpRouterApi { + get(path: string): void; +} + +export const loggerApiRef = createServiceRef({ + id: 'core.logger', +}); + +export const configApiRef = createServiceRef({ + id: 'core.config', +}); + +export const httpRouterApiRef = createServiceRef({ + id: 'core.apiRouter', +}); + +// export type PluginEnvironment = { +// logger: Logger; +// cache: PluginCacheManager; +// database: PluginDatabaseManager; +// config: Config; +// reader: UrlReader; +// discovery: PluginEndpointDiscovery; +// tokenManager: TokenManager; +// permissions: PermissionEvaluator | PermissionAuthorizer; +// scheduler: PluginTaskScheduler; +// }; diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts new file mode 100644 index 0000000000..4b6b284a1d --- /dev/null +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -0,0 +1,31 @@ +/* + * 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. + */ + +export interface ServiceRef { + id: string; + T: T; + $$ref: 'service'; +} + +export function createServiceRef(options: { id: string }): ServiceRef { + return { + id: options.id, + get T(): T { + throw Error('NO T'); + }, + $$ref: 'service', // TODO: declare + }; +} diff --git a/packages/backend-plugin-api/src/wiring/index.ts b/packages/backend-plugin-api/src/wiring/index.ts new file mode 100644 index 0000000000..259fe2b2ea --- /dev/null +++ b/packages/backend-plugin-api/src/wiring/index.ts @@ -0,0 +1,17 @@ +/* + * 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. + */ + +export { BackendRegistrable } from './types'; diff --git a/packages/backend-plugin-api/src/wiring/types.ts b/packages/backend-plugin-api/src/wiring/types.ts new file mode 100644 index 0000000000..32a5afba0c --- /dev/null +++ b/packages/backend-plugin-api/src/wiring/types.ts @@ -0,0 +1,85 @@ +/* + * 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. + */ + +export interface ExtensionPoint { + id: string; + T: T; + $$ref: 'extension-point'; +} + +export function createExtensionPoint(options: { + id: string; +}): ExtensionPoint { + return { + id: options.id, + get T(): T { + throw Error('NO T'); + }, + $$ref: 'extension-point', // TODO: declare + }; +} + +export interface BackendInitRegistry { + registerExtensionPoint( + ref: ServiceRef, + impl: TExtensionPoint, + ): void; + registerInit(options: { + deps: { [name in keyof Deps]: ServiceRef }; + init: (deps: Deps) => Promise; + }): void; +} + +export interface BackendRegistrable { + id: string; + register(reg: BackendInitRegistry): void; +} + +export interface BackendPluginConfig { + id: string; + register(reg: BackendInitRegistry, options: TOptions): void; +} + +export function createBackendPlugin( + config: BackendPluginConfig, +): (option: TOptions) => BackendRegistrable { + return options => ({ + id: config.id, + register(register) { + return config.register(register, options); + }, + }); +} + +export interface BackendModuleConfig { + pluginId: string; + moduleId: string; + register( + reg: Omit, + options: TOptions, + ): void; +} + +export function createBackendModule( + config: BackendModuleConfig, +): (option: TOptions) => BackendRegistrable { + return options => ({ + id: `${config.pluginId}.${config.moduleId}`, + register(register) { + return config.register(register, options); + }, + }); +} From 39b24c78fec6d84662fac873cc01e85d1c978d0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Wed, 15 Jun 2022 14:27:23 +0200 Subject: [PATCH 03/31] Initial work on the backend/next setup for the catalog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Patrik Oldsberg Co-authored-by: blam Co-authored-by: Johan Haals Signed-off-by: Fredrik Adelöw --- packages/backend-app-api/api-report.md | 15 ++ packages/backend-app-api/package.json | 4 +- packages/backend-app-api/src/index.ts | 2 +- .../src/wiring/BackendInitializer.ts | 153 ++++++++++++++++++ .../src/wiring/BackstageBackend.ts | 44 +++++ .../src/wiring/CoreApiRegistry.ts | 65 ++++++++ packages/backend-app-api/src/wiring/types.ts | 25 ++- packages/backend-plugin-api/package.json | 4 +- packages/backend-plugin-api/src/index.ts | 2 + .../src/services/definitions/configApiRef.ts | 22 +++ .../services/definitions/httpRouterApiRef.ts | 25 +++ .../definitions/{types.ts => index.ts} | 31 +--- .../src/services/definitions/loggerApiRef.ts | 26 +++ .../src/services/system/types.ts | 41 ++++- .../backend-plugin-api/src/wiring/index.ts | 3 +- .../backend-plugin-api/src/wiring/types.ts | 21 ++- packages/backend/package.json | 2 + packages/backend/src/next/index.ts | 100 ++++++++++++ yarn.lock | 146 ++++++++++++++++- 19 files changed, 697 insertions(+), 34 deletions(-) create mode 100644 packages/backend-app-api/api-report.md create mode 100644 packages/backend-app-api/src/wiring/BackendInitializer.ts create mode 100644 packages/backend-app-api/src/wiring/BackstageBackend.ts create mode 100644 packages/backend-app-api/src/wiring/CoreApiRegistry.ts create mode 100644 packages/backend-plugin-api/src/services/definitions/configApiRef.ts create mode 100644 packages/backend-plugin-api/src/services/definitions/httpRouterApiRef.ts rename packages/backend-plugin-api/src/services/definitions/{types.ts => index.ts} (66%) create mode 100644 packages/backend-plugin-api/src/services/definitions/loggerApiRef.ts create mode 100644 packages/backend/src/next/index.ts diff --git a/packages/backend-app-api/api-report.md b/packages/backend-app-api/api-report.md new file mode 100644 index 0000000000..8c64858310 --- /dev/null +++ b/packages/backend-app-api/api-report.md @@ -0,0 +1,15 @@ +## API Report File for "@backstage/backend-app-api" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts +import { AnyServiceFactory } from '@backstage/backend-plugin-api'; +import { BackendRegistrable } from '@backstage/backend-plugin-api'; + +// Warning: (ae-forgotten-export) The symbol "CreateBackendOptions" needs to be exported by the entry point index.d.ts +// Warning: (ae-forgotten-export) The symbol "Backend" needs to be exported by the entry point index.d.ts +// Warning: (ae-missing-release-tag) "createBackend" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export function createBackend(options?: CreateBackendOptions): Backend; +``` diff --git a/packages/backend-app-api/package.json b/packages/backend-app-api/package.json index fe2d517086..ab88009d64 100644 --- a/packages/backend-app-api/package.json +++ b/packages/backend-app-api/package.json @@ -33,7 +33,9 @@ "clean": "backstage-cli package clean", "start": "backstage-cli package start" }, - "dependencies": {}, + "dependencies": { + "@backstage/backend-plugin-api": "^0.0.0" + }, "devDependencies": { "@backstage/cli": "^0.17.2-next.0" }, diff --git a/packages/backend-app-api/src/index.ts b/packages/backend-app-api/src/index.ts index 1888a9b037..33aca2b291 100644 --- a/packages/backend-app-api/src/index.ts +++ b/packages/backend-app-api/src/index.ts @@ -20,4 +20,4 @@ * @packageDocumentation */ -export {}; +export { createBackend } from './wiring/types'; diff --git a/packages/backend-app-api/src/wiring/BackendInitializer.ts b/packages/backend-app-api/src/wiring/BackendInitializer.ts new file mode 100644 index 0000000000..8c2f2714c4 --- /dev/null +++ b/packages/backend-app-api/src/wiring/BackendInitializer.ts @@ -0,0 +1,153 @@ +/* + * 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 { BackendRegistrable, ServiceRef } from '@backstage/backend-plugin-api'; +import { BackendRegisterInit, ApiHolder } from './types'; + +export class BackendInitializer { + #started = false; + #extensions = new Map(); + // #stops = []; + #registerInits = new Array(); + #apis = new Map, unknown>(); + #apiHolder: ApiHolder; + + constructor(apiHolder: ApiHolder) { + this.#apiHolder = apiHolder; + } + + async #getInitDeps( + deps: { [name: string]: ServiceRef }, + pluginId: string, + ) { + return Object.fromEntries( + await Promise.all( + Object.entries(deps).map(async ([name, apiRef]) => [ + name, + this.#apis.get(apiRef) || + (await this.#apiHolder.get(apiRef)!(pluginId)), + ]), + ), + ); + } + + add(extension: BackendRegistrable, options?: TOptions) { + if (this.#started) { + throw new Error( + 'extension can not be added after the backend has started', + ); + } + this.#extensions.set(extension, options); + } + + async start(): Promise { + console.log(`Starting backend`); + if (this.#started) { + throw new Error('Backend has already started'); + } + this.#started = true; + + for (const [extension] of this.#extensions) { + const provides = new Set>(); + + let registerInit: BackendRegisterInit | undefined = undefined; + + console.log('Registering', extension.id); + extension.register({ + registerExtensionPoint: (api, impl) => { + if (registerInit) { + throw new Error('registerInitApi called after registerInit'); + } + if (this.#apis.has(api)) { + throw new Error(`API ${api.id} already registered`); + } + this.#apis.set(api, impl); + provides.add(api); + }, + registerInit: registerOptions => { + if (registerInit) { + throw new Error('registerInit must only be called once'); + } + registerInit = { + id: extension.id, + provides, + consumes: new Set(Object.values(registerOptions.deps)), + deps: registerOptions.deps, + init: registerOptions.init as BackendRegisterInit['init'], + }; + }, + }); + + if (!registerInit) { + throw new Error( + `registerInit was not called by register in ${extension.id}`, + ); + } + + this.#registerInits.push(registerInit); + } + + this.validateSetup(); + + const orderedRegisterResults = this.#resolveInitOrder(this.#registerInits); + + for (const registerInit of orderedRegisterResults) { + // TODO: DI + const deps = await this.#getInitDeps(registerInit.deps, registerInit.id); + await registerInit.init(deps); + // Maybe return stop? or lifecycle API + // this.#stops.push(); + } + } + + // async stop(): Promise { + // for (const stop of this.#stops) { + // await stop.stop(); + // } + // } + + private validateSetup() {} + + #resolveInitOrder(registerInits: Array) { + let registerInitsToOrder = registerInits.slice(); + const orderedRegisterInits = new Array(); + + // TODO: Validate duplicates + + while (registerInitsToOrder.length > 0) { + const toRemove = new Set(); + + for (const registerInit of registerInitsToOrder) { + const unInitializedDependents = Array.from( + registerInit.provides, + // eslint-disable-next-line no-loop-func + ).filter(r => + registerInitsToOrder.some( + init => init !== registerInit && init.consumes.has(r), + ), + ); + + if (unInitializedDependents.length === 0) { + orderedRegisterInits.push(registerInit); + toRemove.add(registerInit); + } + } + + registerInitsToOrder = registerInitsToOrder.filter(r => !toRemove.has(r)); + } + return orderedRegisterInits; + } +} diff --git a/packages/backend-app-api/src/wiring/BackstageBackend.ts b/packages/backend-app-api/src/wiring/BackstageBackend.ts new file mode 100644 index 0000000000..ae27c4ee05 --- /dev/null +++ b/packages/backend-app-api/src/wiring/BackstageBackend.ts @@ -0,0 +1,44 @@ +/* + * 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 { + BackendRegistrable, + AnyServiceFactory, +} from '@backstage/backend-plugin-api'; +import { BackendInitializer } from './BackendInitializer'; +import { CoreApiRegistry } from './CoreApiRegistry'; +import { Backend } from './types'; + +export class BackstageBackend implements Backend { + #coreApis: CoreApiRegistry; + #initializer: BackendInitializer; + + constructor(apiFactories: AnyServiceFactory[]) { + this.#coreApis = new CoreApiRegistry(apiFactories); + this.#initializer = new BackendInitializer(this.#coreApis); + } + + add(extension: BackendRegistrable): void { + this.#initializer.add(extension); + } + + async start(): Promise { + await this.#initializer.start(); + } + + // async stop(): Promise { + // await this.#initializer.stop(); + // } +} diff --git a/packages/backend-app-api/src/wiring/CoreApiRegistry.ts b/packages/backend-app-api/src/wiring/CoreApiRegistry.ts new file mode 100644 index 0000000000..a144fc448d --- /dev/null +++ b/packages/backend-app-api/src/wiring/CoreApiRegistry.ts @@ -0,0 +1,65 @@ +/* + * 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 { + AnyServiceFactory, + ServiceRef, + FactoryFunc, +} from '@backstage/backend-plugin-api'; + +export class CoreApiRegistry { + readonly #implementations: Map>; + readonly #factories: Map; + + constructor(factories: AnyServiceFactory[]) { + this.#factories = new Map(factories.map(f => [f.service.id, f])); + this.#implementations = new Map(); + } + + get(ref: ServiceRef): FactoryFunc | undefined { + const factory = this.#factories.get(ref.id); + if (!factory) { + return undefined; + } + + return async (pluginId: string): Promise => { + if (this.#implementations.has(ref.id)) { + if (this.#implementations.get(ref.id)!.has(pluginId)) { + return this.#implementations.get(ref.id)!.get(pluginId) as T; + } + this.#implementations.set(ref.id, new Map()); + } else { + this.#implementations.set(ref.id, new Map()); + } + + const factoryDeps = Object.fromEntries( + Object.entries(factory.deps).map(([name, apiRef]) => [ + name, + this.get(apiRef)!, // TODO: throw + ]), + ); + + const factoryFunc = await factory.factory(factoryDeps); + const implementation = await factoryFunc(pluginId); + + this.#implementations.set( + ref.id, + this.#implementations.get(ref.id)!.set(pluginId, implementation), + ); + + return implementation as T; + }; + } +} diff --git a/packages/backend-app-api/src/wiring/types.ts b/packages/backend-app-api/src/wiring/types.ts index cf600ba1d0..44640cbc07 100644 --- a/packages/backend-app-api/src/wiring/types.ts +++ b/packages/backend-app-api/src/wiring/types.ts @@ -14,14 +14,35 @@ * limitations under the License. */ -export interface BackendApp { - add(registrable: BackendRegistrable): void; +import { + BackendRegistrable, + AnyServiceFactory, + ServiceRef, + FactoryFunc, +} from '@backstage/backend-plugin-api'; +import { BackstageBackend } from './BackstageBackend'; + +export interface Backend { + add(extension: BackendRegistrable): void; + start(): Promise; +} + +export interface BackendRegisterInit { + id: string; + consumes: Set>; + provides: Set>; + deps: { [name: string]: ServiceRef }; + init: (deps: { [name: string]: unknown }) => Promise; } interface CreateBackendOptions { apis: AnyServiceFactory[]; } +export type ApiHolder = { + get(api: ServiceRef): FactoryFunc | undefined; +}; + export function createBackend(options?: CreateBackendOptions): Backend { return new BackstageBackend(options?.apis ?? []); } diff --git a/packages/backend-plugin-api/package.json b/packages/backend-plugin-api/package.json index 967ae734ee..ccda227018 100644 --- a/packages/backend-plugin-api/package.json +++ b/packages/backend-plugin-api/package.json @@ -33,7 +33,9 @@ "clean": "backstage-cli package clean", "start": "backstage-cli package start" }, - "dependencies": {}, + "dependencies": { + "@backstage/config": "^1.0.1" + }, "devDependencies": { "@backstage/cli": "^0.17.2-next.0" }, diff --git a/packages/backend-plugin-api/src/index.ts b/packages/backend-plugin-api/src/index.ts index da51896950..e82218ca69 100644 --- a/packages/backend-plugin-api/src/index.ts +++ b/packages/backend-plugin-api/src/index.ts @@ -21,3 +21,5 @@ */ export * from './wiring'; +export * from './services/system/types'; +export * from './services/definitions'; diff --git a/packages/backend-plugin-api/src/services/definitions/configApiRef.ts b/packages/backend-plugin-api/src/services/definitions/configApiRef.ts new file mode 100644 index 0000000000..cca1445079 --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/configApiRef.ts @@ -0,0 +1,22 @@ +/* + * 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 { Config } from '@backstage/config'; +import { createServiceRef } from '../system/types'; + +export const configApiRef = createServiceRef({ + id: 'core.config', +}); diff --git a/packages/backend-plugin-api/src/services/definitions/httpRouterApiRef.ts b/packages/backend-plugin-api/src/services/definitions/httpRouterApiRef.ts new file mode 100644 index 0000000000..d54ebd36bf --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/httpRouterApiRef.ts @@ -0,0 +1,25 @@ +/* + * 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 { createServiceRef } from '../system/types'; + +export interface HttpRouterApi { + get(path: string): void; +} + +export const httpRouterApiRef = createServiceRef({ + id: 'core.httpRouter', +}); diff --git a/packages/backend-plugin-api/src/services/definitions/types.ts b/packages/backend-plugin-api/src/services/definitions/index.ts similarity index 66% rename from packages/backend-plugin-api/src/services/definitions/types.ts rename to packages/backend-plugin-api/src/services/definitions/index.ts index 8e85e1198d..b11b2f8c4e 100644 --- a/packages/backend-plugin-api/src/services/definitions/types.ts +++ b/packages/backend-plugin-api/src/services/definitions/index.ts @@ -14,31 +14,6 @@ * limitations under the License. */ -interface Logger { - log(message: string): void; - child(fields: { [name: string]: string }): Logger; -} - -interface ConfigApi { - getString(key: string): string; -} - -interface HttpRouterApi { - get(path: string): void; -} - -export const loggerApiRef = createServiceRef({ - id: 'core.logger', -}); - -export const configApiRef = createServiceRef({ - id: 'core.config', -}); - -export const httpRouterApiRef = createServiceRef({ - id: 'core.apiRouter', -}); - // export type PluginEnvironment = { // logger: Logger; // cache: PluginCacheManager; @@ -50,3 +25,9 @@ export const httpRouterApiRef = createServiceRef({ // permissions: PermissionEvaluator | PermissionAuthorizer; // scheduler: PluginTaskScheduler; // }; + +export { configApiRef } from './configApiRef'; +export { httpRouterApiRef } from './httpRouterApiRef'; +export type { HttpRouterApi } from './httpRouterApiRef'; +export { loggerApiRef } from './loggerApiRef'; +export type { Logger } from './loggerApiRef'; diff --git a/packages/backend-plugin-api/src/services/definitions/loggerApiRef.ts b/packages/backend-plugin-api/src/services/definitions/loggerApiRef.ts new file mode 100644 index 0000000000..348ffc9cf4 --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/loggerApiRef.ts @@ -0,0 +1,26 @@ +/* + * 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 { createServiceRef } from '../system/types'; + +export interface Logger { + log(message: string): void; + child(fields: { [name: string]: string }): Logger; +} + +export const loggerApiRef = createServiceRef({ + id: 'core.logger', +}); diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index 4b6b284a1d..86ad8d42f0 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -14,17 +14,56 @@ * limitations under the License. */ +/** + * TODO + * + * @public + */ export interface ServiceRef { id: string; + + /** + * Utility for getting the type of the service, using `typeof serviceRef.T`. + * Attempting to actually read this value will result in an exception. + */ T: T; + + toString(): string; + $$ref: 'service'; } +type TypesToServiceRef = { [key in keyof T]: ServiceRef }; +type DepsToDepFactories = { + [key in keyof T]: (pluginId: string) => Promise; +}; + +export type FactoryFunc = (pluginId: string) => Promise; + +export type ServiceFactory< + TApi, + TImpl extends TApi, + TDeps extends { [name in string]: unknown }, +> = { + service: ServiceRef; + deps: TypesToServiceRef; + factory(deps: DepsToDepFactories): Promise>; +}; + +export type AnyServiceFactory = ServiceFactory< + unknown, + unknown, + { [key in string]: unknown } +>; + export function createServiceRef(options: { id: string }): ServiceRef { return { id: options.id, get T(): T { - throw Error('NO T'); + throw new Error(`tried to read ServiceRef.T of ${this}`); + }, + toString() { + return `serviceRef{${options.id}}`; }, $$ref: 'service', // TODO: declare }; diff --git a/packages/backend-plugin-api/src/wiring/index.ts b/packages/backend-plugin-api/src/wiring/index.ts index 259fe2b2ea..9340dc8fa9 100644 --- a/packages/backend-plugin-api/src/wiring/index.ts +++ b/packages/backend-plugin-api/src/wiring/index.ts @@ -14,4 +14,5 @@ * limitations under the License. */ -export { BackendRegistrable } from './types'; +export type { BackendRegistrable } from './types'; +export * from './types'; diff --git a/packages/backend-plugin-api/src/wiring/types.ts b/packages/backend-plugin-api/src/wiring/types.ts index 32a5afba0c..746d47b355 100644 --- a/packages/backend-plugin-api/src/wiring/types.ts +++ b/packages/backend-plugin-api/src/wiring/types.ts @@ -14,9 +14,25 @@ * limitations under the License. */ +import { ServiceRef } from '../services/system/types'; + +/** + * TODO + * + * @public + */ export interface ExtensionPoint { id: string; + + /** + * Utility for getting the type of the extension point, using `typeof + * extensionPoint.T`. Attempting to actually read this value will result in an + * exception. + */ T: T; + + toString(): string; + $$ref: 'extension-point'; } @@ -26,7 +42,10 @@ export function createExtensionPoint(options: { return { id: options.id, get T(): T { - throw Error('NO T'); + throw new Error(`tried to read ExtensionPoint.T of ${this}`); + }, + toString() { + return `extensionPoint{${options.id}}`; }, $$ref: 'extension-point', // TODO: declare }; diff --git a/packages/backend/package.json b/packages/backend/package.json index df6fe5ec3b..7604f1e609 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -26,6 +26,8 @@ "build-image": "docker build ../.. -f Dockerfile --tag example-backend" }, "dependencies": { + "@backstage/backend-plugin-api": "^0.0.0", + "@backstage/backend-app-api": "^0.0.0", "@backstage/backend-common": "^0.14.1-next.2", "@backstage/backend-tasks": "^0.3.3-next.2", "@backstage/catalog-client": "^1.0.4-next.1", diff --git a/packages/backend/src/next/index.ts b/packages/backend/src/next/index.ts new file mode 100644 index 0000000000..221332f6ba --- /dev/null +++ b/packages/backend/src/next/index.ts @@ -0,0 +1,100 @@ +/* + * 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 { createBackend } from '@backstage/backend-app-api'; +import { + createBackendModule, + createBackendPlugin, + createServiceRef, +} from '@backstage/backend-plugin-api'; +// import { catalogPlugin } from '@backstage/plugin-catalog-backend'; +// import { scaffolderPlugin } from '@backstage/plugin-scaffolder-backend'; + +interface CatalogProcessor { + process(): void; +} + +interface CatalogProcessingInitApi { + addProcessor(processor: CatalogProcessor): void; +} + +export const catalogProcessingInitApiRef = + createServiceRef({ + id: 'catalog.processing', + }); + +class CatalogExtensionPointImpl implements CatalogProcessingInitApi { + #processors = new Array(); + + addProcessor(processor: CatalogProcessor): void { + this.#processors.push(processor); + } + + get processors() { + return this.#processors; + } +} + +export const catalogPlugin = createBackendPlugin({ + id: 'catalog', + register(env) { + const processingExtensions = new CatalogExtensionPointImpl(); + // plugins depending on this API will be initialized before this plugins init method is executed. + env.registerExtensionPoint( + catalogProcessingInitApiRef, + processingExtensions, + ); + + env.registerInit({ + deps: { + // logger: loggerApiRef, + }, + async init() { + console.log('I HAZ', processingExtensions.processors[0].process()); + console.log('I AM le CATALOG!'); + // logger.log('HELLO!'); + }, + }); + }, +}); + +export const scaffolderCatalogExtension = createBackendModule({ + moduleId: 'boop', + pluginId: 'catalog', + register(env) { + env.registerInit({ + deps: { + catalogProcessingInitApi: catalogProcessingInitApiRef, + }, + async init({ catalogProcessingInitApi }) { + catalogProcessingInitApi.addProcessor({ + process() { + console.log('Running scaffolder processor'); + }, + }); + }, + }); + }, +}); + +const backend = createBackend({ + apis: [], +}); + +// backend.add(scaffolderPlugin()); +backend.add(catalogPlugin({})); +backend.add(scaffolderCatalogExtension({})); +backend.start(); diff --git a/yarn.lock b/yarn.lock index e35aee85fb..fe5ff8cb76 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1557,6 +1557,126 @@ lodash "^4.17.21" uuid "^8.0.0" +"@backstage/cli@^0.17.2-next.0": + version "0.17.2" + resolved "https://registry.npmjs.org/@backstage/cli/-/cli-0.17.2.tgz#2387b8d24d8af4828b84baaa62e6b444ec4330e6" + integrity sha512-stRJWmokD7SXnclZ1dsVfA1stUP4PQPkbi3GkwY1zM84y4M20dU+YHde7MrTILMPnhMY+16WdTdfbeqLfXOJrw== + dependencies: + "@backstage/cli-common" "^0.1.9" + "@backstage/config" "^1.0.1" + "@backstage/config-loader" "^1.1.2" + "@backstage/errors" "^1.0.0" + "@backstage/release-manifests" "^0.0.4" + "@backstage/types" "^1.0.0" + "@hot-loader/react-dom-v16" "npm:@hot-loader/react-dom@^16.0.2" + "@hot-loader/react-dom-v17" "npm:@hot-loader/react-dom@^17.0.2" + "@manypkg/get-packages" "^1.1.3" + "@octokit/request" "^5.4.12" + "@rollup/plugin-commonjs" "^22.0.0" + "@rollup/plugin-json" "^4.1.0" + "@rollup/plugin-node-resolve" "^13.0.0" + "@rollup/plugin-yaml" "^3.1.0" + "@spotify/eslint-config-base" "^13.0.0" + "@spotify/eslint-config-react" "^13.0.0" + "@spotify/eslint-config-typescript" "^13.0.0" + "@sucrase/jest-plugin" "^2.1.1" + "@sucrase/webpack-loader" "^2.0.0" + "@svgr/plugin-jsx" "6.2.x" + "@svgr/plugin-svgo" "6.2.x" + "@svgr/rollup" "6.2.x" + "@svgr/webpack" "6.2.x" + "@types/webpack-env" "^1.15.2" + "@typescript-eslint/eslint-plugin" "^5.9.0" + "@typescript-eslint/parser" "^5.9.0" + "@yarnpkg/lockfile" "^1.1.0" + "@yarnpkg/parsers" "^3.0.0-rc.4" + bfj "^7.0.2" + buffer "^6.0.3" + chalk "^4.0.0" + chokidar "^3.3.1" + commander "^9.1.0" + css-loader "^6.5.1" + diff "^5.0.0" + esbuild "^0.14.10" + esbuild-loader "^2.18.0" + eslint "^8.6.0" + eslint-config-prettier "^8.3.0" + eslint-formatter-friendly "^7.0.0" + eslint-plugin-deprecation "^1.3.2" + eslint-plugin-import "^2.25.4" + eslint-plugin-jest "^26.1.2" + eslint-plugin-jsx-a11y "^6.5.1" + eslint-plugin-monorepo "^0.3.2" + eslint-plugin-react "^7.28.0" + eslint-plugin-react-hooks "^4.3.0" + eslint-webpack-plugin "^3.1.1" + express "^4.17.1" + fork-ts-checker-webpack-plugin "^7.0.0-alpha.8" + fs-extra "10.1.0" + glob "^7.1.7" + global-agent "^3.0.0" + handlebars "^4.7.3" + html-webpack-plugin "^5.3.1" + inquirer "^8.2.0" + jest "^27.5.1" + jest-css-modules "^2.1.0" + jest-runtime "^27.5.1" + jest-transform-yaml "^1.0.0" + json-schema "^0.4.0" + lodash "^4.17.21" + mini-css-extract-plugin "^2.4.2" + minimatch "5.1.0" + node-fetch "^2.6.7" + node-libs-browser "^2.2.1" + npm-packlist "^5.0.0" + ora "^5.3.0" + postcss "^8.1.0" + process "^0.11.10" + react-dev-utils "^12.0.0-next.60" + react-hot-loader "^4.13.0" + recursive-readdir "^2.2.2" + replace-in-file "^6.0.0" + rollup "^2.60.2" + rollup-plugin-dts "^4.0.1" + rollup-plugin-esbuild "^4.7.2" + rollup-plugin-postcss "^4.0.0" + rollup-pluginutils "^2.8.2" + run-script-webpack-plugin "^0.0.14" + semver "^7.3.2" + style-loader "^3.3.1" + sucrase "^3.20.2" + tar "^6.1.2" + terser-webpack-plugin "^5.1.3" + util "^0.12.3" + webpack "^5.66.0" + webpack-dev-server "^4.7.3" + webpack-node-externals "^3.0.0" + yaml "^1.10.0" + yml-loader "^2.1.0" + yn "^4.0.0" + zod "^3.11.6" + +"@backstage/config-loader@^1.1.2": + version "1.1.2" + resolved "https://registry.npmjs.org/@backstage/config-loader/-/config-loader-1.1.2.tgz#72cb0d7b2647f5a646bb279360bc34732e06521f" + integrity sha512-c5ZO7xDJn609DBIsYAWGE5kgh+7SPYUmG2ADtVX9SbXaql3VCafGlhc2hAZQa/O12W04qi3GgwGg0bqSFmx5uw== + dependencies: + "@backstage/cli-common" "^0.1.9" + "@backstage/config" "^1.0.1" + "@backstage/errors" "^1.0.0" + "@backstage/types" "^1.0.0" + "@types/json-schema" "^7.0.6" + ajv "^8.10.0" + chokidar "^3.5.2" + fs-extra "10.1.0" + json-schema "^0.4.0" + json-schema-merge-allof "^0.8.1" + json-schema-traverse "^1.0.0" + node-fetch "^2.6.7" + typescript-json-schema "^0.53.0" + yaml "^1.9.2" + yup "^0.32.9" + "@backstage/core-components@^0.9.0", "@backstage/core-components@^0.9.5": version "0.9.5" resolved "https://registry.npmjs.org/@backstage/core-components/-/core-components-0.9.5.tgz#5a0b34867aaee0549bfa67b39a69c09588fa3c7a" @@ -5555,7 +5675,7 @@ dependencies: "@rollup/pluginutils" "^3.0.8" -"@rollup/plugin-node-resolve@^13.0.6": +"@rollup/plugin-node-resolve@^13.0.0", "@rollup/plugin-node-resolve@^13.0.6": version "13.3.0" resolved "https://registry.npmjs.org/@rollup/plugin-node-resolve/-/plugin-node-resolve-13.3.0.tgz#da1c5c5ce8316cef96a2f823d111c1e4e498801c" integrity sha512-Lus8rbUo1eEcnS4yTFKLZrVumLPY+YayBdWXgFSHYhTT2iJbMhoaaBL3xl5NCdeRytErGr8tZ0L71BMRmnlwSw== @@ -20504,6 +20624,11 @@ path-case@^3.0.4: dot-case "^3.0.4" tslib "^2.0.3" +path-equal@1.1.2: + version "1.1.2" + resolved "https://registry.npmjs.org/path-equal/-/path-equal-1.1.2.tgz#260e7c449c4c2022f68cc5fa6e617e892858250d" + integrity sha512-p5kxPPwCdbf5AdXzT1bUBJomhgBlEjRBavYNr1XUpMFIE4Hnf2roueCMXudZK5tnaAu1tTmp3GPzqwJK45IHEA== + path-equal@^1.1.2: version "1.2.2" resolved "https://registry.npmjs.org/path-equal/-/path-equal-1.2.2.tgz#fa2997f0a829de22ec8f5f86461ca5590d49b832" @@ -23028,6 +23153,11 @@ run-parallel@^1.1.9: resolved "https://registry.npmjs.org/run-parallel/-/run-parallel-1.1.9.tgz#c9dd3a7cf9f4b2c4b6244e173a6ed866e61dd679" integrity sha512-DEqnSRTDw/Tc3FXf49zedI638Z9onwUotBMiUFKmrO2sdFKIbXamXGQ3Axd4qgphxKB4kw/qP1w5kTxnfU1B9Q== +run-script-webpack-plugin@^0.0.14: + version "0.0.14" + resolved "https://registry.npmjs.org/run-script-webpack-plugin/-/run-script-webpack-plugin-0.0.14.tgz#fe2362b32c1dab7a8af7a6f1246fc043690cedd7" + integrity sha512-DXe6lzzEVXjBr/74zd4m4yOfmz5P6GMjzhQxDDsViOmwG7cap8UCE6RgD5rT7zf4wM83a+ToHnpB3v4efUv5IA== + run-script-webpack-plugin@^0.1.0: version "0.1.1" resolved "https://registry.npmjs.org/run-script-webpack-plugin/-/run-script-webpack-plugin-0.1.1.tgz#dad3114be32eb864d2160306e4d9c52a2c1cfd59" @@ -25311,6 +25441,20 @@ typedarray@^0.0.6: resolved "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= +typescript-json-schema@^0.53.0: + version "0.53.1" + resolved "https://registry.npmjs.org/typescript-json-schema/-/typescript-json-schema-0.53.1.tgz#9204547f3e145169b40928998366ff6d28b81d32" + integrity sha512-Hg+RnOKUd38MOzC0rDft03a8xvwO+gCcj1F77smw2tCoZYQpFoLtrXWBGdvCX+REliko5WYel2kux17HPFqjLQ== + dependencies: + "@types/json-schema" "^7.0.9" + "@types/node" "^16.9.2" + glob "^7.1.7" + path-equal "1.1.2" + safe-stable-stringify "^2.2.0" + ts-node "^10.2.1" + typescript "~4.6.0" + yargs "^17.1.1" + typescript-json-schema@^0.54.0: version "0.54.0" resolved "https://registry.npmjs.org/typescript-json-schema/-/typescript-json-schema-0.54.0.tgz#b3fc42ad90df6a0f6ab57571ebc8b4d41125df4f" From 375a4036645dc829d2af39b0948e3ca43174f418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Wed, 15 Jun 2022 15:44:10 +0200 Subject: [PATCH 04/31] Add more service implementations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Patrik Oldsberg Co-authored-by: blam Co-authored-by: Johan Haals Signed-off-by: Fredrik Adelöw --- packages/backend-app-api/package.json | 4 +- .../services/implementations/configService.ts | 37 +++++++++++ .../services/implementations/loggerService.ts | 63 +++++++++++++++++++ .../implementations/urlReaderService.ts | 41 ++++++++++++ .../src/wiring/BackendInitializer.ts | 3 +- .../src/wiring/BackstageBackend.ts | 3 +- .../src/wiring/CoreApiRegistry.ts | 2 +- packages/backend-app-api/src/wiring/types.ts | 9 ++- packages/backend-plugin-api/package.json | 3 +- .../definitions/cacheManagerServiceRef.ts | 22 +++++++ .../services/definitions/cacheServiceRef.ts | 22 +++++++ .../{configApiRef.ts => configServiceRef.ts} | 2 +- .../definitions/databaseServiceRef.ts | 22 +++++++ .../definitions/discoveryServiceRef.ts | 22 +++++++ ...outerApiRef.ts => httpRouterServiceRef.ts} | 2 +- .../src/services/definitions/index.ts | 15 +++-- .../{loggerApiRef.ts => loggerServiceRef.ts} | 4 +- .../definitions/tokenManagerServiceRef.ts | 22 +++++++ .../definitions/urlReaderServiceRef.ts | 22 +++++++ .../src/services/system/types.ts | 8 +++ packages/backend/src/next/index.ts | 19 ++++-- 21 files changed, 325 insertions(+), 22 deletions(-) create mode 100644 packages/backend-app-api/src/services/implementations/configService.ts create mode 100644 packages/backend-app-api/src/services/implementations/loggerService.ts create mode 100644 packages/backend-app-api/src/services/implementations/urlReaderService.ts create mode 100644 packages/backend-plugin-api/src/services/definitions/cacheManagerServiceRef.ts create mode 100644 packages/backend-plugin-api/src/services/definitions/cacheServiceRef.ts rename packages/backend-plugin-api/src/services/definitions/{configApiRef.ts => configServiceRef.ts} (92%) create mode 100644 packages/backend-plugin-api/src/services/definitions/databaseServiceRef.ts create mode 100644 packages/backend-plugin-api/src/services/definitions/discoveryServiceRef.ts rename packages/backend-plugin-api/src/services/definitions/{httpRouterApiRef.ts => httpRouterServiceRef.ts} (91%) rename packages/backend-plugin-api/src/services/definitions/{loggerApiRef.ts => loggerServiceRef.ts} (89%) create mode 100644 packages/backend-plugin-api/src/services/definitions/tokenManagerServiceRef.ts create mode 100644 packages/backend-plugin-api/src/services/definitions/urlReaderServiceRef.ts diff --git a/packages/backend-app-api/package.json b/packages/backend-app-api/package.json index ab88009d64..02c5450021 100644 --- a/packages/backend-app-api/package.json +++ b/packages/backend-app-api/package.json @@ -34,7 +34,9 @@ "start": "backstage-cli package start" }, "dependencies": { - "@backstage/backend-plugin-api": "^0.0.0" + "@backstage/backend-plugin-api": "^0.0.0", + "@backstage/backend-common": "^0.14.0", + "winston": "^3.2.1" }, "devDependencies": { "@backstage/cli": "^0.17.2-next.0" diff --git a/packages/backend-app-api/src/services/implementations/configService.ts b/packages/backend-app-api/src/services/implementations/configService.ts new file mode 100644 index 0000000000..b066989069 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/configService.ts @@ -0,0 +1,37 @@ +/* + * 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 { loadBackendConfig } from '@backstage/backend-common'; +import { + configServiceRef, + createServiceFactory, + loggerServiceRef, +} from '@backstage/backend-plugin-api'; +import { loggerToWinstonLogger } from './loggerService'; + +export const configService = createServiceFactory({ + service: configServiceRef, + deps: { loggerFactory: loggerServiceRef }, + factory: async ({ loggerFactory }) => { + const logger = await loggerFactory('root'); + const config = await loadBackendConfig({ + argv: process.argv, + logger: loggerToWinstonLogger(logger), + }); + + return async () => config; + }, +}); diff --git a/packages/backend-app-api/src/services/implementations/loggerService.ts b/packages/backend-app-api/src/services/implementations/loggerService.ts new file mode 100644 index 0000000000..4b6eadfa1f --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/loggerService.ts @@ -0,0 +1,63 @@ +/* + * 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 { createRootLogger } from '@backstage/backend-common'; +import { + createServiceFactory, + Logger, + loggerServiceRef, +} from '@backstage/backend-plugin-api'; +import { Logger as WinstonLogger } from 'winston'; + +class BackstageLogger implements Logger { + constructor( + private readonly options: { + winston: WinstonLogger; + }, + ) {} + + info(message: string, ...meta: any[]): void { + this.options.winston.info(message, ...meta); + } + + child(fields: { [name: string]: string }): Logger { + return new BackstageLogger({ + winston: this.options.winston.child(fields), + }); + } + + toWinston() { + return this.options.winston; + } +} + +export function loggerToWinstonLogger(logger: Logger): WinstonLogger { + return (logger as BackstageLogger).toWinston(); +} + +export const loggerFactory = createServiceFactory({ + service: loggerServiceRef, + deps: {}, + factory: async () => { + const root = new BackstageLogger({ + winston: createRootLogger(), + }); + + return async (pluginId: string) => { + return root.child({ pluginId }); + }; + }, +}); diff --git a/packages/backend-app-api/src/services/implementations/urlReaderService.ts b/packages/backend-app-api/src/services/implementations/urlReaderService.ts new file mode 100644 index 0000000000..afbc80d9ac --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/urlReaderService.ts @@ -0,0 +1,41 @@ +/* + * 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 { UrlReaders } from '@backstage/backend-common'; +import { + configServiceRef, + createServiceFactory, + loggerServiceRef, + urlReaderServiceRef, +} from '@backstage/backend-plugin-api'; +import { loggerToWinstonLogger } from './loggerService'; + +export const urlReaderFactory = createServiceFactory({ + service: urlReaderServiceRef, + deps: { + loggerFactory: loggerServiceRef, + configFactory: configServiceRef, + }, + factory: async ({ loggerFactory, configFactory }) => { + return async (pluginId: string) => { + const logger = await loggerFactory(pluginId); + return UrlReaders.default({ + logger: loggerToWinstonLogger(logger), + config: await configFactory(pluginId), + }); + }; + }, +}); diff --git a/packages/backend-app-api/src/wiring/BackendInitializer.ts b/packages/backend-app-api/src/wiring/BackendInitializer.ts index 8c2f2714c4..a613e88bb1 100644 --- a/packages/backend-app-api/src/wiring/BackendInitializer.ts +++ b/packages/backend-app-api/src/wiring/BackendInitializer.ts @@ -15,7 +15,7 @@ */ import { BackendRegistrable, ServiceRef } from '@backstage/backend-plugin-api'; -import { BackendRegisterInit, ApiHolder } from './types'; +import { ApiHolder, BackendRegisterInit } from './types'; export class BackendInitializer { #started = false; @@ -133,7 +133,6 @@ export class BackendInitializer { for (const registerInit of registerInitsToOrder) { const unInitializedDependents = Array.from( registerInit.provides, - // eslint-disable-next-line no-loop-func ).filter(r => registerInitsToOrder.some( init => init !== registerInit && init.consumes.has(r), diff --git a/packages/backend-app-api/src/wiring/BackstageBackend.ts b/packages/backend-app-api/src/wiring/BackstageBackend.ts index ae27c4ee05..69df1fdf82 100644 --- a/packages/backend-app-api/src/wiring/BackstageBackend.ts +++ b/packages/backend-app-api/src/wiring/BackstageBackend.ts @@ -13,9 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import { - BackendRegistrable, AnyServiceFactory, + BackendRegistrable, } from '@backstage/backend-plugin-api'; import { BackendInitializer } from './BackendInitializer'; import { CoreApiRegistry } from './CoreApiRegistry'; diff --git a/packages/backend-app-api/src/wiring/CoreApiRegistry.ts b/packages/backend-app-api/src/wiring/CoreApiRegistry.ts index a144fc448d..4b1e0f5ce0 100644 --- a/packages/backend-app-api/src/wiring/CoreApiRegistry.ts +++ b/packages/backend-app-api/src/wiring/CoreApiRegistry.ts @@ -15,8 +15,8 @@ */ import { AnyServiceFactory, - ServiceRef, FactoryFunc, + ServiceRef, } from '@backstage/backend-plugin-api'; export class CoreApiRegistry { diff --git a/packages/backend-app-api/src/wiring/types.ts b/packages/backend-app-api/src/wiring/types.ts index 44640cbc07..5c2e7a9e96 100644 --- a/packages/backend-app-api/src/wiring/types.ts +++ b/packages/backend-app-api/src/wiring/types.ts @@ -15,11 +15,12 @@ */ import { - BackendRegistrable, AnyServiceFactory, - ServiceRef, + BackendRegistrable, FactoryFunc, + ServiceRef, } from '@backstage/backend-plugin-api'; +import { loggerFactory } from '../services/implementations/loggerService'; import { BackstageBackend } from './BackstageBackend'; export interface Backend { @@ -44,5 +45,7 @@ export type ApiHolder = { }; export function createBackend(options?: CreateBackendOptions): Backend { - return new BackstageBackend(options?.apis ?? []); + // TODO: merge with provided APIs + const defaultApis = [loggerFactory]; + return new BackstageBackend([...defaultApis, ...(options?.apis ?? [])]); } diff --git a/packages/backend-plugin-api/package.json b/packages/backend-plugin-api/package.json index ccda227018..6d75af03a7 100644 --- a/packages/backend-plugin-api/package.json +++ b/packages/backend-plugin-api/package.json @@ -34,7 +34,8 @@ "start": "backstage-cli package start" }, "dependencies": { - "@backstage/config": "^1.0.1" + "@backstage/config": "^1.0.1", + "@backstage/backend-common": "^0.14.0" }, "devDependencies": { "@backstage/cli": "^0.17.2-next.0" diff --git a/packages/backend-plugin-api/src/services/definitions/cacheManagerServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/cacheManagerServiceRef.ts new file mode 100644 index 0000000000..9dab256704 --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/cacheManagerServiceRef.ts @@ -0,0 +1,22 @@ +/* + * 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 { createServiceRef } from '../system/types'; +import { CacheManager } from '@backstage/backend-common'; + +export const cacheManagerServiceRef = createServiceRef({ + id: 'core.cacheManager', +}); diff --git a/packages/backend-plugin-api/src/services/definitions/cacheServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/cacheServiceRef.ts new file mode 100644 index 0000000000..439a2b645d --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/cacheServiceRef.ts @@ -0,0 +1,22 @@ +/* + * 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 { createServiceRef } from '../system/types'; +import { PluginCacheManager } from '@backstage/backend-common'; + +export const cacheServiceRef = createServiceRef({ + id: 'core.cache', +}); diff --git a/packages/backend-plugin-api/src/services/definitions/configApiRef.ts b/packages/backend-plugin-api/src/services/definitions/configServiceRef.ts similarity index 92% rename from packages/backend-plugin-api/src/services/definitions/configApiRef.ts rename to packages/backend-plugin-api/src/services/definitions/configServiceRef.ts index cca1445079..9a4199967d 100644 --- a/packages/backend-plugin-api/src/services/definitions/configApiRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/configServiceRef.ts @@ -17,6 +17,6 @@ import { Config } from '@backstage/config'; import { createServiceRef } from '../system/types'; -export const configApiRef = createServiceRef({ +export const configServiceRef = createServiceRef({ id: 'core.config', }); diff --git a/packages/backend-plugin-api/src/services/definitions/databaseServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/databaseServiceRef.ts new file mode 100644 index 0000000000..62f7f8a157 --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/databaseServiceRef.ts @@ -0,0 +1,22 @@ +/* + * 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 { PluginDatabaseManager } from '@backstage/backend-common'; +import { createServiceRef } from '../system/types'; + +export const databaseServiceRef = createServiceRef({ + id: 'core.database', +}); diff --git a/packages/backend-plugin-api/src/services/definitions/discoveryServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/discoveryServiceRef.ts new file mode 100644 index 0000000000..3dfd5fa62b --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/discoveryServiceRef.ts @@ -0,0 +1,22 @@ +/* + * 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 { createServiceRef } from '../system/types'; +import { PluginEndpointDiscovery } from '@backstage/backend-common'; + +export const discoveryServiceRef = createServiceRef({ + id: 'core.discovery', +}); diff --git a/packages/backend-plugin-api/src/services/definitions/httpRouterApiRef.ts b/packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts similarity index 91% rename from packages/backend-plugin-api/src/services/definitions/httpRouterApiRef.ts rename to packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts index d54ebd36bf..9cf1089119 100644 --- a/packages/backend-plugin-api/src/services/definitions/httpRouterApiRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts @@ -20,6 +20,6 @@ export interface HttpRouterApi { get(path: string): void; } -export const httpRouterApiRef = createServiceRef({ +export const httpRouterServiceRef = createServiceRef({ id: 'core.httpRouter', }); diff --git a/packages/backend-plugin-api/src/services/definitions/index.ts b/packages/backend-plugin-api/src/services/definitions/index.ts index b11b2f8c4e..8b78d2d0ee 100644 --- a/packages/backend-plugin-api/src/services/definitions/index.ts +++ b/packages/backend-plugin-api/src/services/definitions/index.ts @@ -26,8 +26,13 @@ // scheduler: PluginTaskScheduler; // }; -export { configApiRef } from './configApiRef'; -export { httpRouterApiRef } from './httpRouterApiRef'; -export type { HttpRouterApi } from './httpRouterApiRef'; -export { loggerApiRef } from './loggerApiRef'; -export type { Logger } from './loggerApiRef'; +export { configServiceRef } from './configServiceRef'; +export { httpRouterServiceRef } from './httpRouterServiceRef'; +export type { HttpRouterApi } from './httpRouterServiceRef'; +export { loggerServiceRef } from './loggerServiceRef'; +export type { Logger } from './loggerServiceRef'; +export { urlReaderServiceRef } from './urlReaderServiceRef'; +export { cacheServiceRef } from './cacheServiceRef'; +export { databaseServiceRef } from './databaseServiceRef'; +export { discoveryServiceRef } from './discoveryServiceRef'; +export { tokenManagerServiceRef } from './tokenManagerServiceRef'; diff --git a/packages/backend-plugin-api/src/services/definitions/loggerApiRef.ts b/packages/backend-plugin-api/src/services/definitions/loggerServiceRef.ts similarity index 89% rename from packages/backend-plugin-api/src/services/definitions/loggerApiRef.ts rename to packages/backend-plugin-api/src/services/definitions/loggerServiceRef.ts index 348ffc9cf4..564ce15ded 100644 --- a/packages/backend-plugin-api/src/services/definitions/loggerApiRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/loggerServiceRef.ts @@ -17,10 +17,10 @@ import { createServiceRef } from '../system/types'; export interface Logger { - log(message: string): void; + info(message: string): void; child(fields: { [name: string]: string }): Logger; } -export const loggerApiRef = createServiceRef({ +export const loggerServiceRef = createServiceRef({ id: 'core.logger', }); diff --git a/packages/backend-plugin-api/src/services/definitions/tokenManagerServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/tokenManagerServiceRef.ts new file mode 100644 index 0000000000..c62d4bb634 --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/tokenManagerServiceRef.ts @@ -0,0 +1,22 @@ +/* + * 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 { createServiceRef } from '../system/types'; +import { TokenManager } from '@backstage/backend-common'; + +export const tokenManagerServiceRef = createServiceRef({ + id: 'core.tokenManager', +}); diff --git a/packages/backend-plugin-api/src/services/definitions/urlReaderServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/urlReaderServiceRef.ts new file mode 100644 index 0000000000..25a9666882 --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/urlReaderServiceRef.ts @@ -0,0 +1,22 @@ +/* + * 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 { createServiceRef } from '../system/types'; +import { UrlReader } from '@backstage/backend-common'; + +export const urlReaderServiceRef = createServiceRef({ + id: 'core.urlReader', +}); diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index 86ad8d42f0..2f59e65320 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -68,3 +68,11 @@ export function createServiceRef(options: { id: string }): ServiceRef { $$ref: 'service', // TODO: declare }; } + +export function createServiceFactory< + Api, + Impl extends Api, + Deps extends { [name in string]: unknown }, +>(factory: ServiceFactory): ServiceFactory { + return factory; +} diff --git a/packages/backend/src/next/index.ts b/packages/backend/src/next/index.ts index 221332f6ba..45cf414e01 100644 --- a/packages/backend/src/next/index.ts +++ b/packages/backend/src/next/index.ts @@ -19,6 +19,7 @@ import { createBackendModule, createBackendPlugin, createServiceRef, + loggerServiceRef, } from '@backstage/backend-plugin-api'; // import { catalogPlugin } from '@backstage/plugin-catalog-backend'; // import { scaffolderPlugin } from '@backstage/plugin-scaffolder-backend'; @@ -60,12 +61,12 @@ export const catalogPlugin = createBackendPlugin({ env.registerInit({ deps: { - // logger: loggerApiRef, + logger: loggerServiceRef, }, - async init() { + async init({ logger }) { + // const builder = await CatalogBuilder.create(env); + logger.log('boppp'); console.log('I HAZ', processingExtensions.processors[0].process()); - console.log('I AM le CATALOG!'); - // logger.log('HELLO!'); }, }); }, @@ -94,6 +95,16 @@ const backend = createBackend({ apis: [], }); +// logger: Logger; +// cache: PluginCacheManager; +// database: PluginDatabaseManager; +// config: Config; +// reader: UrlReader; +// discovery: PluginEndpointDiscovery; +// tokenManager: TokenManager; +// permissions: PermissionEvaluator | PermissionAuthorizer; +// scheduler: PluginTaskScheduler; + // backend.add(scaffolderPlugin()); backend.add(catalogPlugin({})); backend.add(scaffolderCatalogExtension({})); From d85f7dc1791381f323d3f195a3191e7c7796e1f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Wed, 15 Jun 2022 17:03:14 +0200 Subject: [PATCH 05/31] Even more MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Patrik Oldsberg Co-authored-by: blam Co-authored-by: Johan Haals Signed-off-by: Fredrik Adelöw --- packages/backend-app-api/package.json | 2 + .../services/implementations/cacheService.ts | 37 ++++++++++++ .../services/implementations/configService.ts | 11 ++-- .../implementations/databaseService.ts | 36 +++++++++++ .../implementations/discoveryService.ts | 36 +++++++++++ .../src/services/implementations/index.ts | 37 ++++++++++++ .../services/implementations/loggerService.ts | 1 - .../implementations/permissionsService.ts | 45 ++++++++++++++ .../implementations/schedulerService.ts | 36 +++++++++++ .../implementations/tokenManagerService.ts | 60 +++++++++++++++++++ .../implementations/urlReaderService.ts | 4 +- .../src/wiring/BackendInitializer.ts | 21 ++++--- packages/backend-app-api/src/wiring/types.ts | 8 ++- packages/backend-plugin-api/package.json | 4 +- .../src/services/definitions/index.ts | 2 + .../definitions/permissionsServiceRef.ts | 27 +++++++++ ...erServiceRef.ts => schedulerServiceRef.ts} | 6 +- .../definitions/scheldulerServiceRef.ts | 27 +++++++++ packages/backend/src/next/index.ts | 2 +- 19 files changed, 379 insertions(+), 23 deletions(-) create mode 100644 packages/backend-app-api/src/services/implementations/cacheService.ts create mode 100644 packages/backend-app-api/src/services/implementations/databaseService.ts create mode 100644 packages/backend-app-api/src/services/implementations/discoveryService.ts create mode 100644 packages/backend-app-api/src/services/implementations/index.ts create mode 100644 packages/backend-app-api/src/services/implementations/permissionsService.ts create mode 100644 packages/backend-app-api/src/services/implementations/schedulerService.ts create mode 100644 packages/backend-app-api/src/services/implementations/tokenManagerService.ts create mode 100644 packages/backend-plugin-api/src/services/definitions/permissionsServiceRef.ts rename packages/backend-plugin-api/src/services/definitions/{cacheManagerServiceRef.ts => schedulerServiceRef.ts} (80%) create mode 100644 packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts diff --git a/packages/backend-app-api/package.json b/packages/backend-app-api/package.json index 02c5450021..b0bbb8cc8b 100644 --- a/packages/backend-app-api/package.json +++ b/packages/backend-app-api/package.json @@ -36,6 +36,8 @@ "dependencies": { "@backstage/backend-plugin-api": "^0.0.0", "@backstage/backend-common": "^0.14.0", + "@backstage/backend-tasks": "^0.3.2", + "@backstage/plugin-permission-node": "^0.6.2", "winston": "^3.2.1" }, "devDependencies": { diff --git a/packages/backend-app-api/src/services/implementations/cacheService.ts b/packages/backend-app-api/src/services/implementations/cacheService.ts new file mode 100644 index 0000000000..034f85f917 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/cacheService.ts @@ -0,0 +1,37 @@ +/* + * 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 { CacheManager } from '@backstage/backend-common'; +import { + configServiceRef, + createServiceFactory, + cacheServiceRef, +} from '@backstage/backend-plugin-api'; + +// TODO: Work out some naming and implementation patterns for these +export const cacheFactory = createServiceFactory({ + service: cacheServiceRef, + deps: { + configFactory: configServiceRef, + }, + factory: async ({ configFactory }) => { + const config = await configFactory('root'); + const cacheManager = CacheManager.fromConfig(config); + return async (pluginId: string) => { + return cacheManager.forPlugin(pluginId); + }; + }, +}); diff --git a/packages/backend-app-api/src/services/implementations/configService.ts b/packages/backend-app-api/src/services/implementations/configService.ts index b066989069..e9a81ae100 100644 --- a/packages/backend-app-api/src/services/implementations/configService.ts +++ b/packages/backend-app-api/src/services/implementations/configService.ts @@ -22,16 +22,19 @@ import { } from '@backstage/backend-plugin-api'; import { loggerToWinstonLogger } from './loggerService'; -export const configService = createServiceFactory({ +export const configFactory = createServiceFactory({ service: configServiceRef, - deps: { loggerFactory: loggerServiceRef }, + deps: { + loggerFactory: loggerServiceRef, + }, factory: async ({ loggerFactory }) => { const logger = await loggerFactory('root'); const config = await loadBackendConfig({ argv: process.argv, logger: loggerToWinstonLogger(logger), }); - - return async () => config; + return async () => { + return config; + }; }, }); diff --git a/packages/backend-app-api/src/services/implementations/databaseService.ts b/packages/backend-app-api/src/services/implementations/databaseService.ts new file mode 100644 index 0000000000..a52da1e444 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/databaseService.ts @@ -0,0 +1,36 @@ +/* + * 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 { DatabaseManager } from '@backstage/backend-common'; +import { + configServiceRef, + createServiceFactory, + databaseServiceRef, +} from '@backstage/backend-plugin-api'; + +export const databaseFactory = createServiceFactory({ + service: databaseServiceRef, + deps: { + configFactory: configServiceRef, + }, + factory: async ({ configFactory }) => { + const config = await configFactory('root'); + const databaseManager = DatabaseManager.fromConfig(config); + return async (pluginId: string) => { + return databaseManager.forPlugin(pluginId); + }; + }, +}); diff --git a/packages/backend-app-api/src/services/implementations/discoveryService.ts b/packages/backend-app-api/src/services/implementations/discoveryService.ts new file mode 100644 index 0000000000..23af1924a0 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/discoveryService.ts @@ -0,0 +1,36 @@ +/* + * 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 { SingleHostDiscovery } from '@backstage/backend-common'; +import { + configServiceRef, + createServiceFactory, + discoveryServiceRef, +} from '@backstage/backend-plugin-api'; + +export const discoveryFactory = createServiceFactory({ + service: discoveryServiceRef, + deps: { + configFactory: configServiceRef, + }, + factory: async ({ configFactory }) => { + const config = await configFactory('root'); + const discovery = SingleHostDiscovery.fromConfig(config); + return async () => { + return discovery; + }; + }, +}); diff --git a/packages/backend-app-api/src/services/implementations/index.ts b/packages/backend-app-api/src/services/implementations/index.ts new file mode 100644 index 0000000000..f520b43cd4 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/index.ts @@ -0,0 +1,37 @@ +/* + * 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 { cacheFactory } from './cacheService'; +import { configFactory } from './configService'; +import { databaseFactory } from './databaseService'; +import { discoveryFactory } from './discoveryService'; +import { loggerFactory } from './loggerService'; +import { permissionsFactory } from './permissionsService'; +import { schedulerFactory } from './schedulerService'; +import { tokenManagerFactory } from './tokenManagerService'; +import { urlReaderFactory } from './urlReaderService'; + +export const defaultServiceFactories = [ + cacheFactory, + configFactory, + databaseFactory, + discoveryFactory, + loggerFactory, + permissionsFactory, + schedulerFactory, + tokenManagerFactory, + urlReaderFactory, +]; diff --git a/packages/backend-app-api/src/services/implementations/loggerService.ts b/packages/backend-app-api/src/services/implementations/loggerService.ts index 4b6eadfa1f..ff6af4b643 100644 --- a/packages/backend-app-api/src/services/implementations/loggerService.ts +++ b/packages/backend-app-api/src/services/implementations/loggerService.ts @@ -55,7 +55,6 @@ export const loggerFactory = createServiceFactory({ const root = new BackstageLogger({ winston: createRootLogger(), }); - return async (pluginId: string) => { return root.child({ pluginId }); }; diff --git a/packages/backend-app-api/src/services/implementations/permissionsService.ts b/packages/backend-app-api/src/services/implementations/permissionsService.ts new file mode 100644 index 0000000000..97f0330917 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/permissionsService.ts @@ -0,0 +1,45 @@ +/* + * 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 { + configServiceRef, + createServiceFactory, + discoveryServiceRef, + permissionsServiceRef, + tokenManagerServiceRef, +} from '@backstage/backend-plugin-api'; +import { ServerPermissionClient } from '@backstage/plugin-permission-node'; + +export const permissionsFactory = createServiceFactory({ + service: permissionsServiceRef, + deps: { + configFactory: configServiceRef, + discoveryFactory: discoveryServiceRef, + tokenManagerFactory: tokenManagerServiceRef, + }, + factory: async ({ configFactory, discoveryFactory, tokenManagerFactory }) => { + const config = await configFactory('root'); + const discovery = await discoveryFactory('root'); + const tokenManager = await tokenManagerFactory('root'); + const permissions = ServerPermissionClient.fromConfig(config, { + discovery, + tokenManager, + }); + return async (_pluginId: string) => { + return permissions; + }; + }, +}); diff --git a/packages/backend-app-api/src/services/implementations/schedulerService.ts b/packages/backend-app-api/src/services/implementations/schedulerService.ts new file mode 100644 index 0000000000..130ccb3f5f --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/schedulerService.ts @@ -0,0 +1,36 @@ +/* + * 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 { + configServiceRef, + createServiceFactory, + schedulerServiceRef, +} from '@backstage/backend-plugin-api'; +import { TaskScheduler } from '@backstage/backend-tasks'; + +export const schedulerFactory = createServiceFactory({ + service: schedulerServiceRef, + deps: { + configFactory: configServiceRef, + }, + factory: async ({ configFactory }) => { + const config = await configFactory('root'); + const taskScheduler = TaskScheduler.fromConfig(config); + return async (pluginId: string) => { + return taskScheduler.forPlugin(pluginId); + }; + }, +}); diff --git a/packages/backend-app-api/src/services/implementations/tokenManagerService.ts b/packages/backend-app-api/src/services/implementations/tokenManagerService.ts new file mode 100644 index 0000000000..7f0423e845 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/tokenManagerService.ts @@ -0,0 +1,60 @@ +/* + * 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 { + configServiceRef, + loggerServiceRef, + createServiceFactory, + tokenManagerServiceRef, +} from '@backstage/backend-plugin-api'; +import { ServerTokenManager } from '@backstage/backend-common'; +import { loggerToWinstonLogger } from './loggerService'; + +export const tokenManagerFactory = createServiceFactory({ + service: tokenManagerServiceRef, + deps: { + configFactory: configServiceRef, + loggerFactory: loggerServiceRef, + }, + factory: async ({ configFactory, loggerFactory }) => { + const logger = await loggerFactory('root'); + const config = await configFactory('root'); + return async (_pluginId: string) => { + // doesn't the logger want to be inferred from the plugin tho here? + // maybe ... also why do we recreate it every time otherwise + // we should memoize on a per plugin right? so I think it's should be fine to re-use the plugin one + // we shouldn't recreate on a per plugin basis. + // hm - on the other hand, is this really ever called more than once? + // not this function right. should only be called when the plugin requests this serviceRef + // yeah so no need to worry about memo probably + // but we still want to scope the logger to the ServrTokenmanagfer>? + // mm sure maybe + // maybe in this case it doesn't provide so much value b + // oh hang on - isn't it up to THE MANAGER to make a child internally if it wants to do that + // so that it becomes a property intrinsic to that class, no matter how it's constructed + // or is that too much responsibility for it - making the constructor complex so to speak, making it harder to tweak that behavior + // this is not ultra efficient :) + + // I think the naming here is wrong to be gonest + // this isn't like the cache manager or the database manager + // the manager name is confusuion i think + // aye perhaps + return ServerTokenManager.fromConfig(config, { + logger: loggerToWinstonLogger(logger), + }); + }; + }, +}); diff --git a/packages/backend-app-api/src/services/implementations/urlReaderService.ts b/packages/backend-app-api/src/services/implementations/urlReaderService.ts index afbc80d9ac..7df6ca33a9 100644 --- a/packages/backend-app-api/src/services/implementations/urlReaderService.ts +++ b/packages/backend-app-api/src/services/implementations/urlReaderService.ts @@ -26,10 +26,10 @@ import { loggerToWinstonLogger } from './loggerService'; export const urlReaderFactory = createServiceFactory({ service: urlReaderServiceRef, deps: { - loggerFactory: loggerServiceRef, configFactory: configServiceRef, + loggerFactory: loggerServiceRef, }, - factory: async ({ loggerFactory, configFactory }) => { + factory: async ({ configFactory, loggerFactory }) => { return async (pluginId: string) => { const logger = await loggerFactory(pluginId); return UrlReaders.default({ diff --git a/packages/backend-app-api/src/wiring/BackendInitializer.ts b/packages/backend-app-api/src/wiring/BackendInitializer.ts index a613e88bb1..6338b60bfb 100644 --- a/packages/backend-app-api/src/wiring/BackendInitializer.ts +++ b/packages/backend-app-api/src/wiring/BackendInitializer.ts @@ -15,7 +15,7 @@ */ import { BackendRegistrable, ServiceRef } from '@backstage/backend-plugin-api'; -import { ApiHolder, BackendRegisterInit } from './types'; +import { BackendRegisterInit, ApiHolder } from './types'; export class BackendInitializer { #started = false; @@ -131,15 +131,20 @@ export class BackendInitializer { const toRemove = new Set(); for (const registerInit of registerInitsToOrder) { - const unInitializedDependents = Array.from( - registerInit.provides, - ).filter(r => - registerInitsToOrder.some( - init => init !== registerInit && init.consumes.has(r), - ), - ); + const unInitializedDependents = []; + + for (const api of registerInit.provides) { + if ( + registerInitsToOrder.some( + init => init !== registerInit && init.consumes.has(api), + ) + ) { + unInitializedDependents.push(api); + } + } if (unInitializedDependents.length === 0) { + console.log(`DEBUG: pushed ${registerInit.id} to results`); orderedRegisterInits.push(registerInit); toRemove.add(registerInit); } diff --git a/packages/backend-app-api/src/wiring/types.ts b/packages/backend-app-api/src/wiring/types.ts index 5c2e7a9e96..c1c43a8ba8 100644 --- a/packages/backend-app-api/src/wiring/types.ts +++ b/packages/backend-app-api/src/wiring/types.ts @@ -20,7 +20,7 @@ import { FactoryFunc, ServiceRef, } from '@backstage/backend-plugin-api'; -import { loggerFactory } from '../services/implementations/loggerService'; +import { defaultServiceFactories } from '../services/implementations'; import { BackstageBackend } from './BackstageBackend'; export interface Backend { @@ -46,6 +46,8 @@ export type ApiHolder = { export function createBackend(options?: CreateBackendOptions): Backend { // TODO: merge with provided APIs - const defaultApis = [loggerFactory]; - return new BackstageBackend([...defaultApis, ...(options?.apis ?? [])]); + return new BackstageBackend([ + ...defaultServiceFactories, + ...(options?.apis ?? []), + ]); } diff --git a/packages/backend-plugin-api/package.json b/packages/backend-plugin-api/package.json index 6d75af03a7..b5c724f764 100644 --- a/packages/backend-plugin-api/package.json +++ b/packages/backend-plugin-api/package.json @@ -35,7 +35,9 @@ }, "dependencies": { "@backstage/config": "^1.0.1", - "@backstage/backend-common": "^0.14.0" + "@backstage/backend-common": "^0.14.0", + "@backstage/plugin-permission-common": "^0.6.2", + "@backstage/backend-tasks": "^0.3.2" }, "devDependencies": { "@backstage/cli": "^0.17.2-next.0" diff --git a/packages/backend-plugin-api/src/services/definitions/index.ts b/packages/backend-plugin-api/src/services/definitions/index.ts index 8b78d2d0ee..30f2e009c5 100644 --- a/packages/backend-plugin-api/src/services/definitions/index.ts +++ b/packages/backend-plugin-api/src/services/definitions/index.ts @@ -36,3 +36,5 @@ export { cacheServiceRef } from './cacheServiceRef'; export { databaseServiceRef } from './databaseServiceRef'; export { discoveryServiceRef } from './discoveryServiceRef'; export { tokenManagerServiceRef } from './tokenManagerServiceRef'; +export { permissionsServiceRef } from './permissionsServiceRef'; +export { schedulerServiceRef } from './schedulerServiceRef'; diff --git a/packages/backend-plugin-api/src/services/definitions/permissionsServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/permissionsServiceRef.ts new file mode 100644 index 0000000000..4355286611 --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/permissionsServiceRef.ts @@ -0,0 +1,27 @@ +/* + * 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 { createServiceRef } from '../system/types'; +import { + PermissionAuthorizer, + PermissionEvaluator, +} from '@backstage/plugin-permission-common'; + +export const permissionsServiceRef = createServiceRef< + PermissionEvaluator | PermissionAuthorizer +>({ + id: 'core.permissions', +}); diff --git a/packages/backend-plugin-api/src/services/definitions/cacheManagerServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/schedulerServiceRef.ts similarity index 80% rename from packages/backend-plugin-api/src/services/definitions/cacheManagerServiceRef.ts rename to packages/backend-plugin-api/src/services/definitions/schedulerServiceRef.ts index 9dab256704..3a3abfcc10 100644 --- a/packages/backend-plugin-api/src/services/definitions/cacheManagerServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/schedulerServiceRef.ts @@ -15,8 +15,8 @@ */ import { createServiceRef } from '../system/types'; -import { CacheManager } from '@backstage/backend-common'; +import { PluginTaskScheduler } from '@backstage/backend-tasks'; -export const cacheManagerServiceRef = createServiceRef({ - id: 'core.cacheManager', +export const schedulerServiceRef = createServiceRef({ + id: 'core.scheduler', }); diff --git a/packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts new file mode 100644 index 0000000000..4355286611 --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts @@ -0,0 +1,27 @@ +/* + * 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 { createServiceRef } from '../system/types'; +import { + PermissionAuthorizer, + PermissionEvaluator, +} from '@backstage/plugin-permission-common'; + +export const permissionsServiceRef = createServiceRef< + PermissionEvaluator | PermissionAuthorizer +>({ + id: 'core.permissions', +}); diff --git a/packages/backend/src/next/index.ts b/packages/backend/src/next/index.ts index 45cf414e01..83ec4b044a 100644 --- a/packages/backend/src/next/index.ts +++ b/packages/backend/src/next/index.ts @@ -65,7 +65,7 @@ export const catalogPlugin = createBackendPlugin({ }, async init({ logger }) { // const builder = await CatalogBuilder.create(env); - logger.log('boppp'); + logger.info('boppp'); console.log('I HAZ', processingExtensions.processors[0].process()); }, }); From 696ba03bc5d96df83077f23d4ebc573a77b363a1 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Mon, 27 Jun 2022 13:41:15 +0200 Subject: [PATCH 06/31] Wire up catalog backend Signed-off-by: Johan Haals --- packages/backend-app-api/src/index.ts | 1 + packages/backend/src/next/index.ts | 61 +++++++++++++++------------ 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/packages/backend-app-api/src/index.ts b/packages/backend-app-api/src/index.ts index 33aca2b291..d75f5c0941 100644 --- a/packages/backend-app-api/src/index.ts +++ b/packages/backend-app-api/src/index.ts @@ -21,3 +21,4 @@ */ export { createBackend } from './wiring/types'; +export { loggerToWinstonLogger } from './services/implementations/loggerService'; diff --git a/packages/backend/src/next/index.ts b/packages/backend/src/next/index.ts index 83ec4b044a..034ef5e5d2 100644 --- a/packages/backend/src/next/index.ts +++ b/packages/backend/src/next/index.ts @@ -14,19 +14,25 @@ * limitations under the License. */ -import { createBackend } from '@backstage/backend-app-api'; import { + createBackend, + loggerToWinstonLogger, +} from '@backstage/backend-app-api'; +import { + configServiceRef, createBackendModule, createBackendPlugin, createServiceRef, + databaseServiceRef, loggerServiceRef, + permissionsServiceRef, + urlReaderServiceRef, } from '@backstage/backend-plugin-api'; -// import { catalogPlugin } from '@backstage/plugin-catalog-backend'; -// import { scaffolderPlugin } from '@backstage/plugin-scaffolder-backend'; - -interface CatalogProcessor { - process(): void; -} +import { + CatalogBuilder, + CatalogProcessor, +} from '@backstage/plugin-catalog-backend'; +import { ScaffolderEntitiesProcessor } from '@backstage/plugin-scaffolder-backend'; interface CatalogProcessingInitApi { addProcessor(processor: CatalogProcessor): void; @@ -62,18 +68,30 @@ export const catalogPlugin = createBackendPlugin({ env.registerInit({ deps: { logger: loggerServiceRef, + config: configServiceRef, + reader: urlReaderServiceRef, + permissions: permissionsServiceRef, + database: databaseServiceRef, }, - async init({ logger }) { - // const builder = await CatalogBuilder.create(env); - logger.info('boppp'); - console.log('I HAZ', processingExtensions.processors[0].process()); + async init({ logger, config, reader, database, permissions }) { + const winstonLogger = loggerToWinstonLogger(logger); + const builder = await CatalogBuilder.create({ + config, + reader, + permissions, + database, + logger: winstonLogger, + }); + builder.addProcessor(processingExtensions.processors); + const { processingEngine } = await builder.build(); + await processingEngine.start(); }, }); }, }); export const scaffolderCatalogExtension = createBackendModule({ - moduleId: 'boop', + moduleId: 'scaffolder.extention', pluginId: 'catalog', register(env) { env.registerInit({ @@ -81,11 +99,9 @@ export const scaffolderCatalogExtension = createBackendModule({ catalogProcessingInitApi: catalogProcessingInitApiRef, }, async init({ catalogProcessingInitApi }) { - catalogProcessingInitApi.addProcessor({ - process() { - console.log('Running scaffolder processor'); - }, - }); + catalogProcessingInitApi.addProcessor( + new ScaffolderEntitiesProcessor(), + ); }, }); }, @@ -95,17 +111,6 @@ const backend = createBackend({ apis: [], }); -// logger: Logger; -// cache: PluginCacheManager; -// database: PluginDatabaseManager; -// config: Config; -// reader: UrlReader; -// discovery: PluginEndpointDiscovery; -// tokenManager: TokenManager; -// permissions: PermissionEvaluator | PermissionAuthorizer; -// scheduler: PluginTaskScheduler; - -// backend.add(scaffolderPlugin()); backend.add(catalogPlugin({})); backend.add(scaffolderCatalogExtension({})); backend.start(); From 118ce976f173f10b405a181c1742b6243feb78e1 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 16 Feb 2022 12:05:48 +0100 Subject: [PATCH 07/31] experiment 1 Signed-off-by: Patrik Oldsberg --- packages/backend/src/experiment1.ts | 350 ++++++++++++++++++++++++++++ 1 file changed, 350 insertions(+) create mode 100644 packages/backend/src/experiment1.ts diff --git a/packages/backend/src/experiment1.ts b/packages/backend/src/experiment1.ts new file mode 100644 index 0000000000..10a752927f --- /dev/null +++ b/packages/backend/src/experiment1.ts @@ -0,0 +1,350 @@ +/* + * 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 express, { NextFunction, Request, Response } from 'express'; +import Router from 'express-promise-router'; +/* +Things to consider: + + - Make the router accessible? Allow integrators to add their own middleware + - The backend entrypoint should make it clear what routes are available + - Modules should not be required to have a router + - Internal modules that extend the functionality of plugins? + - Exposed on under the plugin route, access to same resources? + +*/ + +const routes = { + '/': [/* something */], +} as const; + +interface BackendModule { + routes?: () => [string, (request: Request, response: Response, next: NextFunction) => void]; + start?: () => Promise; + stop?: () => Promise; +} + +const theBackend: BackendModule = { + const modules: Module[]; + async start() { + modules.forEach(it => it.start()) + } + routes() { + return modules.reduce((acc, it) => ({...acc, [it.name]: it.routes()}), {}) + } +} + +async function main_version1() { + const backend: BackendModule = createBackend({ + + }) + + // -> modules install routes and other stuff too? + backend.installModule(catalogPlugin.modules.entitiesCatalog({ startEngine: false })) + backend.installModule(catalogPlugin.modules.locationService({...config})) + + const app = express() + + // Probably problematic to have the installation be too deep + app.use('/catalog/entities', catalogPlugin.modules.entitiesCatalog({ startEngine: false })) + + app.use('/catalog', catalogPlugin.modules.entitiesCatalog({ startEngine: false })) + + const entitiesCatalogModule = catalogPlugin.modules.entitiesCatalog({ startEngine: false }) + for (const route of entitiesCatalogModule.routes) { + app.use(route.path, route.handler) + } + + const entitiesCatalogController = createController(catalogPlugin.modules.entitiesCatalog({ startEngine: false })) + app.use('/catalog/entities', entitiesCatalogController.entities) + + app.use('/catalog', createPluginRouter(entitiesCatalogModule.routes())); + // Get router from startup? + // Hiding creation of router might be bad + + Object.entries(backend.routes()).forEach(([module, routes]) => console.log(`Registering route ${route.join(', ')} for module ${module}`))) + + const { router } = await backend.start() + + // Or access the router directly? + const router = backend.getRouter(); + + + + + app.use(router); +} + + +async function main_version2() { + const backend = createBackend({ + modules: [ + catalogPlugin.modules.entitiesCatalog(), + catalogPlugin.modules.locationService(), + ], + }) + + await backend.start() +} + + +async function main_version3() { + const backend = createBackend({ + modules: { + '/catalog': [ + catalogPlugin.modules.entitiesCatalog(), + catalogPlugin.modules.locationService(), + ], + }, + }) + + await backend.start() +} + + +async function main_version4() { + const backend = createBackend({ + modules: { + '/catalog/entities': catalogPlugin.modules.entitiesCatalog(), + '/catalog/locations': catalogPlugin.modules.locationService(), + '/catalog/extensions': roadieCatalogPlugin.modules.catalogExtensions(), + }, + }) + + await backend.start() +} + + +async function main_version5() { + const backend = createBackend({ + plugins: [ + catalogPlugin(), + ], + }) + + await backend.start() +} + + +async function main_version5b() { + const backend = createBackend({ + plugins: [ + catalogPlugin.modules.entitiesCatalog(), + catalogPlugin.modules.locationService(), + scaffolderPlugin, + ], + }) + + await backend.start() +} + + +async function main_version5c() { + const backend = createBackend({ + plugins: [ + scaffolderPlugin, + ], + modules: [ + catalogPlugin.modules.entitiesCatalog(), + catalogPlugin.modules.locationService(), + ] + }) + + await backend.start() +} + + +async function main_version6() { + const backend = createBackend({ + plugins: [ + catalogPlugin({ extensions }), + ], + }) + + await backend.start() +} + +async function main_version7() { + type CatalogExtension = ({ database, orchestrator, }: any) => () => Promise<{ router: Router }>; + + const extensions: CatalogExtension[] = [ + ({ database, orchestrator }) => async () => { + const router = Router(); + router.post('/stop-orchestrator', () => orchestrator.stop()) + + return { router }; + } + ]; + + // /extensions/stop-orch + + + const backend = createBackend({ + plugins: [ + catalogPlugin({ extensions }), + ], + }) + + await backend.start() +} + +async function main_version7b() { + type CatalogExtension = ({ container }: any) => () => Promise; + + const extensions: CatalogExtension[] = [ + (container) => async () => { + const pluginRouter = container.get('pluginRouter') + const orchestrator = container.get('orchestrator') + + const router = Router(); + router.post('/stop-orchestrator', () => orchestrator.stop()) + + pluginRouter.use(router) + } + ]; + + // /extensions/stop-orch + + + const backend = createBackend({ + plugins: [ + catalogPlugin({ extensions }), + ], + }) + + await backend.start() +} + + + + + + + + + + + + + +// Catalog Plugin implementation + +const myModule = createCatalogExtension({ + entityProviders: [MyProviderFactory], + entityProcessors: [], +}); + +class MyProviderFactory { + constructor(registry: ProviderRegistry /* from the catalog */, things: Thing[] /* from the adopter */) { + registry.add(...things.map(t => t.create(...))) + } +} + +class MyProviderFactory { + constructor(ctx: Context, registry: ProviderRegistry, things: Thing[]) { + registry.add(...things.map(thing => ctx.get('catalog-provider-api').withTag(thing))) + } +} + + +const myEntityProvider = createBackendModule({ + factory: (container) => { + const api = container.get('catalog-provider-api') + ///... + return // ??? + } +}) + +const myEntityProvider = createCatalogEntityProviderExtension({ + ..., +}); + +const catalogPlugin = createBackendPlugin({ + modules: [ + myModule + ] +}) + +// How do you install additional features into the catalog? + + +import { catalogPlugin } from '@backstage/plugin-catalog-backend'; +import { githubEntityProvider } from '@backstage/plugin-catalog-backend-module-github'; + +async function main_version10() { + const backend = createBackend({ + modules: [ + catalogPlugin.modules.entitiesCatalog({ + providers: [githubEntityProvider], + }), + catalogPlugin.modules.locationService(), + ] + }) + + await backend.start() +} + + +async function main_version10a() { + const backend = createBackend({ + modules: [ + catalogPlugin.modules.entitiesCatalog(), + catalogPlugin.modules.locationService(), + githubEntityProvider, + ] + }) + + await backend.start() +} + + +async function main_version10b() { + const backend = createBackend({ + modules: [ + catalogPlugin.modules.entitiesCatalog(), + catalogPlugin.modules.locationService(), + githubEntityProvider, + ] + }) + + await backend.start() +} + +async function main_version10c() { + const backend = createBackend({ + modules: [ + catalogPlugin(), + searchPlugin({ + collators: new CatalogCollator(), // :( + }), + ] + }) + + await backend.start() +} + + + +// possible DI way to do that @johan, nice maaan! +container.add(someCollator); +container.add(otherCOllator); +... +container.getAll(); + +// types are build time ;) :shakefist: +container.add(SearchCollator, someCollator); +container.add(SearchCollator, otherCOllator); +... +container.getAll(SearchCollator); From 7e48611ba6a84690cb9c0a661e0ac7e3b34c76a3 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 5 Jul 2022 15:14:21 +0200 Subject: [PATCH 08/31] =?UTF-8?q?chore:=20wiring=20up=20the=20catalog=20?= =?UTF-8?q?=F0=9F=93=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: Johan Haals Co-authored-by: Patrik Oldsberg Signed-off-by: blam --- packages/backend-app-api/package.json | 2 + .../implementations/httpRouterService.ts | 44 +++++++++++++++++++ .../src/services/implementations/index.ts | 2 + packages/backend-plugin-api/package.json | 6 ++- .../definitions/httpRouterServiceRef.ts | 25 +++++++++-- .../src/services/definitions/index.ts | 2 +- packages/backend/src/next/index.ts | 18 ++++++-- 7 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 packages/backend-app-api/src/services/implementations/httpRouterService.ts diff --git a/packages/backend-app-api/package.json b/packages/backend-app-api/package.json index b0bbb8cc8b..6ad48f86d5 100644 --- a/packages/backend-app-api/package.json +++ b/packages/backend-app-api/package.json @@ -38,6 +38,8 @@ "@backstage/backend-common": "^0.14.0", "@backstage/backend-tasks": "^0.3.2", "@backstage/plugin-permission-node": "^0.6.2", + "express": "^4.17.1", + "express-promise-router": "^4.1.0", "winston": "^3.2.1" }, "devDependencies": { diff --git a/packages/backend-app-api/src/services/implementations/httpRouterService.ts b/packages/backend-app-api/src/services/implementations/httpRouterService.ts new file mode 100644 index 0000000000..82dfb13218 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/httpRouterService.ts @@ -0,0 +1,44 @@ +/* + * 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 { + createServiceFactory, + httpRouterServiceRef, +} from '@backstage/backend-plugin-api'; +import Router from 'express-promise-router'; +import express, { Handler } from 'express'; + +export const httpRouterFactory = createServiceFactory({ + service: httpRouterServiceRef, + deps: {}, + factory: async () => { + const app = express(); + const rootRouter = Router(); + + app.use(rootRouter); + app.listen(8123); + + return async (pluginId?: string) => { + if (!pluginId) { + return rootRouter; + } + return { + use(handler: Handler) { + rootRouter.use(`/api/${pluginId}`, handler); + }, + }; + }; + }, +}); diff --git a/packages/backend-app-api/src/services/implementations/index.ts b/packages/backend-app-api/src/services/implementations/index.ts index f520b43cd4..e6b6604069 100644 --- a/packages/backend-app-api/src/services/implementations/index.ts +++ b/packages/backend-app-api/src/services/implementations/index.ts @@ -23,6 +23,7 @@ import { permissionsFactory } from './permissionsService'; import { schedulerFactory } from './schedulerService'; import { tokenManagerFactory } from './tokenManagerService'; import { urlReaderFactory } from './urlReaderService'; +import { httpRouterFactory } from './httpRouterService'; export const defaultServiceFactories = [ cacheFactory, @@ -34,4 +35,5 @@ export const defaultServiceFactories = [ schedulerFactory, tokenManagerFactory, urlReaderFactory, + httpRouterFactory, ]; diff --git a/packages/backend-plugin-api/package.json b/packages/backend-plugin-api/package.json index b5c724f764..dc52366c65 100644 --- a/packages/backend-plugin-api/package.json +++ b/packages/backend-plugin-api/package.json @@ -37,10 +37,12 @@ "@backstage/config": "^1.0.1", "@backstage/backend-common": "^0.14.0", "@backstage/plugin-permission-common": "^0.6.2", - "@backstage/backend-tasks": "^0.3.2" + "@backstage/backend-tasks": "^0.3.2", + "express": "^4.17.1" }, "devDependencies": { - "@backstage/cli": "^0.17.2-next.0" + "@backstage/cli": "^0.17.2-next.0", + "@types/express": "^4.17.6" }, "files": [ "dist", diff --git a/packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts index 9cf1089119..65a01fde3a 100644 --- a/packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts @@ -15,11 +15,30 @@ */ import { createServiceRef } from '../system/types'; +import { Handler } from 'express'; -export interface HttpRouterApi { - get(path: string): void; +// const apiRouter = Router(); +// apiRouter.use('/catalog', await catalog(catalogEnv)); +// const service = createServiceBuilder(module) +// .loadConfig(config) +// .addRouter('', await healthcheck(healthcheckEnv)) +// .addRouter('', metricsHandler()) +// .addRouter('/api', apiRouter) +// .addRouter('', await app(appEnv)); + +// interface BackstageRequest extends Request { +// identity?: BackstageIdentity; +// context?: Context; +// } + +// interface RequestIdentityService { +// getRequestIdentity(req: Request): BackstageIdentity | undefined; +// } + +export interface HttpRouterService { + use(handler: Handler): void; } -export const httpRouterServiceRef = createServiceRef({ +export const httpRouterServiceRef = createServiceRef({ id: 'core.httpRouter', }); diff --git a/packages/backend-plugin-api/src/services/definitions/index.ts b/packages/backend-plugin-api/src/services/definitions/index.ts index 30f2e009c5..556aa88c72 100644 --- a/packages/backend-plugin-api/src/services/definitions/index.ts +++ b/packages/backend-plugin-api/src/services/definitions/index.ts @@ -28,7 +28,7 @@ export { configServiceRef } from './configServiceRef'; export { httpRouterServiceRef } from './httpRouterServiceRef'; -export type { HttpRouterApi } from './httpRouterServiceRef'; +export type { HttpRouterService } from './httpRouterServiceRef'; export { loggerServiceRef } from './loggerServiceRef'; export type { Logger } from './loggerServiceRef'; export { urlReaderServiceRef } from './urlReaderServiceRef'; diff --git a/packages/backend/src/next/index.ts b/packages/backend/src/next/index.ts index 034ef5e5d2..de40592ea5 100644 --- a/packages/backend/src/next/index.ts +++ b/packages/backend/src/next/index.ts @@ -27,6 +27,7 @@ import { loggerServiceRef, permissionsServiceRef, urlReaderServiceRef, + httpRouterServiceRef, } from '@backstage/backend-plugin-api'; import { CatalogBuilder, @@ -72,8 +73,16 @@ export const catalogPlugin = createBackendPlugin({ reader: urlReaderServiceRef, permissions: permissionsServiceRef, database: databaseServiceRef, + httpRouter: httpRouterServiceRef, }, - async init({ logger, config, reader, database, permissions }) { + async init({ + logger, + config, + reader, + database, + permissions, + httpRouter, + }) { const winstonLogger = loggerToWinstonLogger(logger); const builder = await CatalogBuilder.create({ config, @@ -82,9 +91,12 @@ export const catalogPlugin = createBackendPlugin({ database, logger: winstonLogger, }); - builder.addProcessor(processingExtensions.processors); - const { processingEngine } = await builder.build(); + builder.addProcessor(...processingExtensions.processors); + const { processingEngine, router } = await builder.build(); + await processingEngine.start(); + + httpRouter.use(router); }, }); }, From 0d71831aae42f02c48cfc33ebe771c738f5862fe Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Wed, 6 Jul 2022 10:41:41 +0200 Subject: [PATCH 09/31] chore: delete experiements Signed-off-by: Johan Haals --- packages/backend/src/experiment1.ts | 350 ---------------------------- 1 file changed, 350 deletions(-) delete mode 100644 packages/backend/src/experiment1.ts diff --git a/packages/backend/src/experiment1.ts b/packages/backend/src/experiment1.ts deleted file mode 100644 index 10a752927f..0000000000 --- a/packages/backend/src/experiment1.ts +++ /dev/null @@ -1,350 +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 express, { NextFunction, Request, Response } from 'express'; -import Router from 'express-promise-router'; -/* -Things to consider: - - - Make the router accessible? Allow integrators to add their own middleware - - The backend entrypoint should make it clear what routes are available - - Modules should not be required to have a router - - Internal modules that extend the functionality of plugins? - - Exposed on under the plugin route, access to same resources? - -*/ - -const routes = { - '/': [/* something */], -} as const; - -interface BackendModule { - routes?: () => [string, (request: Request, response: Response, next: NextFunction) => void]; - start?: () => Promise; - stop?: () => Promise; -} - -const theBackend: BackendModule = { - const modules: Module[]; - async start() { - modules.forEach(it => it.start()) - } - routes() { - return modules.reduce((acc, it) => ({...acc, [it.name]: it.routes()}), {}) - } -} - -async function main_version1() { - const backend: BackendModule = createBackend({ - - }) - - // -> modules install routes and other stuff too? - backend.installModule(catalogPlugin.modules.entitiesCatalog({ startEngine: false })) - backend.installModule(catalogPlugin.modules.locationService({...config})) - - const app = express() - - // Probably problematic to have the installation be too deep - app.use('/catalog/entities', catalogPlugin.modules.entitiesCatalog({ startEngine: false })) - - app.use('/catalog', catalogPlugin.modules.entitiesCatalog({ startEngine: false })) - - const entitiesCatalogModule = catalogPlugin.modules.entitiesCatalog({ startEngine: false }) - for (const route of entitiesCatalogModule.routes) { - app.use(route.path, route.handler) - } - - const entitiesCatalogController = createController(catalogPlugin.modules.entitiesCatalog({ startEngine: false })) - app.use('/catalog/entities', entitiesCatalogController.entities) - - app.use('/catalog', createPluginRouter(entitiesCatalogModule.routes())); - // Get router from startup? - // Hiding creation of router might be bad - - Object.entries(backend.routes()).forEach(([module, routes]) => console.log(`Registering route ${route.join(', ')} for module ${module}`))) - - const { router } = await backend.start() - - // Or access the router directly? - const router = backend.getRouter(); - - - - - app.use(router); -} - - -async function main_version2() { - const backend = createBackend({ - modules: [ - catalogPlugin.modules.entitiesCatalog(), - catalogPlugin.modules.locationService(), - ], - }) - - await backend.start() -} - - -async function main_version3() { - const backend = createBackend({ - modules: { - '/catalog': [ - catalogPlugin.modules.entitiesCatalog(), - catalogPlugin.modules.locationService(), - ], - }, - }) - - await backend.start() -} - - -async function main_version4() { - const backend = createBackend({ - modules: { - '/catalog/entities': catalogPlugin.modules.entitiesCatalog(), - '/catalog/locations': catalogPlugin.modules.locationService(), - '/catalog/extensions': roadieCatalogPlugin.modules.catalogExtensions(), - }, - }) - - await backend.start() -} - - -async function main_version5() { - const backend = createBackend({ - plugins: [ - catalogPlugin(), - ], - }) - - await backend.start() -} - - -async function main_version5b() { - const backend = createBackend({ - plugins: [ - catalogPlugin.modules.entitiesCatalog(), - catalogPlugin.modules.locationService(), - scaffolderPlugin, - ], - }) - - await backend.start() -} - - -async function main_version5c() { - const backend = createBackend({ - plugins: [ - scaffolderPlugin, - ], - modules: [ - catalogPlugin.modules.entitiesCatalog(), - catalogPlugin.modules.locationService(), - ] - }) - - await backend.start() -} - - -async function main_version6() { - const backend = createBackend({ - plugins: [ - catalogPlugin({ extensions }), - ], - }) - - await backend.start() -} - -async function main_version7() { - type CatalogExtension = ({ database, orchestrator, }: any) => () => Promise<{ router: Router }>; - - const extensions: CatalogExtension[] = [ - ({ database, orchestrator }) => async () => { - const router = Router(); - router.post('/stop-orchestrator', () => orchestrator.stop()) - - return { router }; - } - ]; - - // /extensions/stop-orch - - - const backend = createBackend({ - plugins: [ - catalogPlugin({ extensions }), - ], - }) - - await backend.start() -} - -async function main_version7b() { - type CatalogExtension = ({ container }: any) => () => Promise; - - const extensions: CatalogExtension[] = [ - (container) => async () => { - const pluginRouter = container.get('pluginRouter') - const orchestrator = container.get('orchestrator') - - const router = Router(); - router.post('/stop-orchestrator', () => orchestrator.stop()) - - pluginRouter.use(router) - } - ]; - - // /extensions/stop-orch - - - const backend = createBackend({ - plugins: [ - catalogPlugin({ extensions }), - ], - }) - - await backend.start() -} - - - - - - - - - - - - - -// Catalog Plugin implementation - -const myModule = createCatalogExtension({ - entityProviders: [MyProviderFactory], - entityProcessors: [], -}); - -class MyProviderFactory { - constructor(registry: ProviderRegistry /* from the catalog */, things: Thing[] /* from the adopter */) { - registry.add(...things.map(t => t.create(...))) - } -} - -class MyProviderFactory { - constructor(ctx: Context, registry: ProviderRegistry, things: Thing[]) { - registry.add(...things.map(thing => ctx.get('catalog-provider-api').withTag(thing))) - } -} - - -const myEntityProvider = createBackendModule({ - factory: (container) => { - const api = container.get('catalog-provider-api') - ///... - return // ??? - } -}) - -const myEntityProvider = createCatalogEntityProviderExtension({ - ..., -}); - -const catalogPlugin = createBackendPlugin({ - modules: [ - myModule - ] -}) - -// How do you install additional features into the catalog? - - -import { catalogPlugin } from '@backstage/plugin-catalog-backend'; -import { githubEntityProvider } from '@backstage/plugin-catalog-backend-module-github'; - -async function main_version10() { - const backend = createBackend({ - modules: [ - catalogPlugin.modules.entitiesCatalog({ - providers: [githubEntityProvider], - }), - catalogPlugin.modules.locationService(), - ] - }) - - await backend.start() -} - - -async function main_version10a() { - const backend = createBackend({ - modules: [ - catalogPlugin.modules.entitiesCatalog(), - catalogPlugin.modules.locationService(), - githubEntityProvider, - ] - }) - - await backend.start() -} - - -async function main_version10b() { - const backend = createBackend({ - modules: [ - catalogPlugin.modules.entitiesCatalog(), - catalogPlugin.modules.locationService(), - githubEntityProvider, - ] - }) - - await backend.start() -} - -async function main_version10c() { - const backend = createBackend({ - modules: [ - catalogPlugin(), - searchPlugin({ - collators: new CatalogCollator(), // :( - }), - ] - }) - - await backend.start() -} - - - -// possible DI way to do that @johan, nice maaan! -container.add(someCollator); -container.add(otherCOllator); -... -container.getAll(); - -// types are build time ;) :shakefist: -container.add(SearchCollator, someCollator); -container.add(SearchCollator, otherCOllator); -... -container.getAll(SearchCollator); From 699881cdd9d661211a56990ce6a1bdec34f46f1d Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Wed, 6 Jul 2022 11:06:17 +0200 Subject: [PATCH 10/31] chore: Move catalogPlugin to catalog-backend Signed-off-by: Johan Haals --- packages/backend/src/next/index.ts | 88 +-------------- plugins/catalog-backend/api-report.md | 14 +++ plugins/catalog-backend/package.json | 2 + .../src/service/CatalogPlugin.ts | 105 ++++++++++++++++++ plugins/catalog-backend/src/service/index.ts | 3 + 5 files changed, 128 insertions(+), 84 deletions(-) create mode 100644 plugins/catalog-backend/src/service/CatalogPlugin.ts diff --git a/packages/backend/src/next/index.ts b/packages/backend/src/next/index.ts index de40592ea5..f0f246343d 100644 --- a/packages/backend/src/next/index.ts +++ b/packages/backend/src/next/index.ts @@ -14,94 +14,14 @@ * limitations under the License. */ +import { createBackend } from '@backstage/backend-app-api'; +import { createBackendModule } from '@backstage/backend-plugin-api'; import { - createBackend, - loggerToWinstonLogger, -} from '@backstage/backend-app-api'; -import { - configServiceRef, - createBackendModule, - createBackendPlugin, - createServiceRef, - databaseServiceRef, - loggerServiceRef, - permissionsServiceRef, - urlReaderServiceRef, - httpRouterServiceRef, -} from '@backstage/backend-plugin-api'; -import { - CatalogBuilder, - CatalogProcessor, + catalogPlugin, + catalogProcessingInitApiRef, } from '@backstage/plugin-catalog-backend'; import { ScaffolderEntitiesProcessor } from '@backstage/plugin-scaffolder-backend'; -interface CatalogProcessingInitApi { - addProcessor(processor: CatalogProcessor): void; -} - -export const catalogProcessingInitApiRef = - createServiceRef({ - id: 'catalog.processing', - }); - -class CatalogExtensionPointImpl implements CatalogProcessingInitApi { - #processors = new Array(); - - addProcessor(processor: CatalogProcessor): void { - this.#processors.push(processor); - } - - get processors() { - return this.#processors; - } -} - -export const catalogPlugin = createBackendPlugin({ - id: 'catalog', - register(env) { - const processingExtensions = new CatalogExtensionPointImpl(); - // plugins depending on this API will be initialized before this plugins init method is executed. - env.registerExtensionPoint( - catalogProcessingInitApiRef, - processingExtensions, - ); - - env.registerInit({ - deps: { - logger: loggerServiceRef, - config: configServiceRef, - reader: urlReaderServiceRef, - permissions: permissionsServiceRef, - database: databaseServiceRef, - httpRouter: httpRouterServiceRef, - }, - async init({ - logger, - config, - reader, - database, - permissions, - httpRouter, - }) { - const winstonLogger = loggerToWinstonLogger(logger); - const builder = await CatalogBuilder.create({ - config, - reader, - permissions, - database, - logger: winstonLogger, - }); - builder.addProcessor(...processingExtensions.processors); - const { processingEngine, router } = await builder.build(); - - await processingEngine.start(); - - httpRouter.use(router); - }, - }); - }, -}); - export const scaffolderCatalogExtension = createBackendModule({ moduleId: 'scaffolder.extention', pluginId: 'catalog', diff --git a/plugins/catalog-backend/api-report.md b/plugins/catalog-backend/api-report.md index 3bd9504bea..b7e63ae66d 100644 --- a/plugins/catalog-backend/api-report.md +++ b/plugins/catalog-backend/api-report.md @@ -5,6 +5,7 @@ ```ts /// +import { BackendRegistrable } from '@backstage/backend-plugin-api'; import { CatalogApi } from '@backstage/catalog-client'; import { CatalogEntityDocument } from '@backstage/plugin-catalog-common'; import { CatalogProcessor } from '@backstage/plugin-catalog-node'; @@ -46,6 +47,7 @@ import { Readable } from 'stream'; import { ResourcePermission } from '@backstage/plugin-permission-common'; import { Router } from 'express'; import { ScmIntegrationRegistry } from '@backstage/integration'; +import { ServiceRef } from '@backstage/backend-plugin-api'; import { TokenManager } from '@backstage/backend-common'; import { UrlReader } from '@backstage/backend-common'; import { Validators } from '@backstage/catalog-model'; @@ -215,6 +217,9 @@ export type CatalogEnvironment = { export type CatalogPermissionRule = PermissionRule; +// @alpha +export const catalogPlugin: (option: unknown) => BackendRegistrable; + // @public (undocumented) export interface CatalogProcessingEngine { // (undocumented) @@ -223,6 +228,15 @@ export interface CatalogProcessingEngine { stop(): Promise; } +// @alpha (undocumented) +export interface CatalogProcessingInitApi { + // (undocumented) + addProcessor(processor: CatalogProcessor): void; +} + +// @alpha (undocumented) +export const catalogProcessingInitApiRef: ServiceRef; + export { CatalogProcessor }; export { CatalogProcessorCache }; diff --git a/plugins/catalog-backend/package.json b/plugins/catalog-backend/package.json index 3177782289..c36e28deb8 100644 --- a/plugins/catalog-backend/package.json +++ b/plugins/catalog-backend/package.json @@ -34,6 +34,8 @@ "clean": "backstage-cli package clean" }, "dependencies": { + "@backstage/backend-app-api": "^0.0.0", + "@backstage/backend-plugin-api": "^0.0.0", "@backstage/plugin-catalog-node": "^0.0.0", "@backstage/backend-common": "^0.14.1-next.2", "@backstage/catalog-client": "^1.0.4-next.1", diff --git a/plugins/catalog-backend/src/service/CatalogPlugin.ts b/plugins/catalog-backend/src/service/CatalogPlugin.ts new file mode 100644 index 0000000000..6742757294 --- /dev/null +++ b/plugins/catalog-backend/src/service/CatalogPlugin.ts @@ -0,0 +1,105 @@ +/* + * 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 { loggerToWinstonLogger } from '@backstage/backend-app-api'; +import { + configServiceRef, + createBackendPlugin, + createServiceRef, + databaseServiceRef, + loggerServiceRef, + permissionsServiceRef, + urlReaderServiceRef, + httpRouterServiceRef, +} from '@backstage/backend-plugin-api'; +import { CatalogProcessor } from '../api/processor'; +import { CatalogBuilder } from './CatalogBuilder'; + +/** + * @alpha + */ +export interface CatalogProcessingInitApi { + addProcessor(processor: CatalogProcessor): void; +} + +/** + * @alpha + */ +export const catalogProcessingInitApiRef = + createServiceRef({ + id: 'catalog.processing', + }); + +class CatalogExtensionPointImpl implements CatalogProcessingInitApi { + #processors = new Array(); + + addProcessor(processor: CatalogProcessor): void { + this.#processors.push(processor); + } + + get processors() { + return this.#processors; + } +} + +/** + * Catalog plugin + * @alpha + */ +export const catalogPlugin = createBackendPlugin({ + id: 'catalog', + register(env) { + const processingExtensions = new CatalogExtensionPointImpl(); + // plugins depending on this API will be initialized before this plugins init method is executed. + env.registerExtensionPoint( + catalogProcessingInitApiRef, + processingExtensions, + ); + + env.registerInit({ + deps: { + logger: loggerServiceRef, + config: configServiceRef, + reader: urlReaderServiceRef, + permissions: permissionsServiceRef, + database: databaseServiceRef, + httpRouter: httpRouterServiceRef, + }, + async init({ + logger, + config, + reader, + database, + permissions, + httpRouter, + }) { + const winstonLogger = loggerToWinstonLogger(logger); + const builder = await CatalogBuilder.create({ + config, + reader, + permissions, + database, + logger: winstonLogger, + }); + builder.addProcessor(...processingExtensions.processors); + const { processingEngine, router } = await builder.build(); + + await processingEngine.start(); + + httpRouter.use(router); + }, + }); + }, +}); diff --git a/plugins/catalog-backend/src/service/index.ts b/plugins/catalog-backend/src/service/index.ts index b1e91e4382..ed24780c5a 100644 --- a/plugins/catalog-backend/src/service/index.ts +++ b/plugins/catalog-backend/src/service/index.ts @@ -16,3 +16,6 @@ export type { CatalogEnvironment } from './CatalogBuilder'; export { CatalogBuilder } from './CatalogBuilder'; + +export type { CatalogProcessingInitApi } from './CatalogPlugin'; +export { catalogPlugin, catalogProcessingInitApiRef } from './CatalogPlugin'; From 4ba38d1f27246b96b035479ba3378c02e8487160 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Wed, 6 Jul 2022 16:08:25 +0200 Subject: [PATCH 11/31] Move shared types into catalog-node package Signed-off-by: Johan Haals --- packages/backend-app-api/package.json | 8 +- packages/backend-plugin-api/package.json | 8 +- packages/backend/src/next/index.ts | 40 ++--- .../src/service/CatalogPlugin.ts | 26 +--- plugins/catalog-backend/src/service/index.ts | 4 +- plugins/catalog-node/api-report.md | 10 ++ plugins/catalog-node/package.json | 1 + plugins/catalog-node/src/extensions.ts | 32 ++++ plugins/catalog-node/src/index.ts | 2 + yarn.lock | 146 +----------------- 10 files changed, 82 insertions(+), 195 deletions(-) create mode 100644 plugins/catalog-node/src/extensions.ts diff --git a/packages/backend-app-api/package.json b/packages/backend-app-api/package.json index 6ad48f86d5..ae6f7eb03b 100644 --- a/packages/backend-app-api/package.json +++ b/packages/backend-app-api/package.json @@ -35,15 +35,15 @@ }, "dependencies": { "@backstage/backend-plugin-api": "^0.0.0", - "@backstage/backend-common": "^0.14.0", - "@backstage/backend-tasks": "^0.3.2", - "@backstage/plugin-permission-node": "^0.6.2", + "@backstage/backend-common": "^0.14.1-next.2", + "@backstage/backend-tasks": "^0.3.3-next.2", + "@backstage/plugin-permission-node": "^0.6.3-next.1", "express": "^4.17.1", "express-promise-router": "^4.1.0", "winston": "^3.2.1" }, "devDependencies": { - "@backstage/cli": "^0.17.2-next.0" + "@backstage/cli": "^0.18.0-next.2" }, "files": [ "dist", diff --git a/packages/backend-plugin-api/package.json b/packages/backend-plugin-api/package.json index dc52366c65..24d2012374 100644 --- a/packages/backend-plugin-api/package.json +++ b/packages/backend-plugin-api/package.json @@ -35,13 +35,13 @@ }, "dependencies": { "@backstage/config": "^1.0.1", - "@backstage/backend-common": "^0.14.0", - "@backstage/plugin-permission-common": "^0.6.2", - "@backstage/backend-tasks": "^0.3.2", + "@backstage/backend-common": "^0.14.1-next.2", + "@backstage/plugin-permission-common": "^0.6.3-next.0", + "@backstage/backend-tasks": "^0.3.3-next.2", "express": "^4.17.1" }, "devDependencies": { - "@backstage/cli": "^0.17.2-next.0", + "@backstage/cli": "^0.18.0-next.2", "@types/express": "^4.17.6" }, "files": [ diff --git a/packages/backend/src/next/index.ts b/packages/backend/src/next/index.ts index f0f246343d..0a16160a23 100644 --- a/packages/backend/src/next/index.ts +++ b/packages/backend/src/next/index.ts @@ -15,34 +15,34 @@ */ import { createBackend } from '@backstage/backend-app-api'; -import { createBackendModule } from '@backstage/backend-plugin-api'; +// import { createBackendModule } from '@backstage/backend-plugin-api'; import { catalogPlugin, - catalogProcessingInitApiRef, + // catalogProcessingInitApiRef, } from '@backstage/plugin-catalog-backend'; -import { ScaffolderEntitiesProcessor } from '@backstage/plugin-scaffolder-backend'; +// import { ScaffolderEntitiesProcessor } from '@backstage/plugin-scaffolder-backend'; -export const scaffolderCatalogExtension = createBackendModule({ - moduleId: 'scaffolder.extention', - pluginId: 'catalog', - register(env) { - env.registerInit({ - deps: { - catalogProcessingInitApi: catalogProcessingInitApiRef, - }, - async init({ catalogProcessingInitApi }) { - catalogProcessingInitApi.addProcessor( - new ScaffolderEntitiesProcessor(), - ); - }, - }); - }, -}); +// export const scaffolderCatalogExtension = createBackendModule({ +// moduleId: 'scaffolder.extention', +// pluginId: 'catalog', +// register(env) { +// env.registerInit({ +// deps: { +// catalogProcessingInitApi: catalogProcessingInitApiRef, +// }, +// async init({ catalogProcessingInitApi }) { +// catalogProcessingInitApi.addProcessor( +// new ScaffolderEntitiesProcessor(), +// ); +// }, +// }); +// }, +// }); const backend = createBackend({ apis: [], }); backend.add(catalogPlugin({})); -backend.add(scaffolderCatalogExtension({})); +// backend.add(scaffolderCatalogExtension({})); backend.start(); diff --git a/plugins/catalog-backend/src/service/CatalogPlugin.ts b/plugins/catalog-backend/src/service/CatalogPlugin.ts index 6742757294..d6dc4d11da 100644 --- a/plugins/catalog-backend/src/service/CatalogPlugin.ts +++ b/plugins/catalog-backend/src/service/CatalogPlugin.ts @@ -17,32 +17,20 @@ import { loggerToWinstonLogger } from '@backstage/backend-app-api'; import { configServiceRef, createBackendPlugin, - createServiceRef, databaseServiceRef, loggerServiceRef, permissionsServiceRef, urlReaderServiceRef, httpRouterServiceRef, } from '@backstage/backend-plugin-api'; -import { CatalogProcessor } from '../api/processor'; +import { CatalogProcessor } from '@backstage/plugin-catalog-node'; import { CatalogBuilder } from './CatalogBuilder'; +import { + CatalogProcessingExtensionPoint, + catalogProcessingExtentionPoint, +} from '@backstage/plugin-catalog-node'; -/** - * @alpha - */ -export interface CatalogProcessingInitApi { - addProcessor(processor: CatalogProcessor): void; -} - -/** - * @alpha - */ -export const catalogProcessingInitApiRef = - createServiceRef({ - id: 'catalog.processing', - }); - -class CatalogExtensionPointImpl implements CatalogProcessingInitApi { +class CatalogExtensionPointImpl implements CatalogProcessingExtensionPoint { #processors = new Array(); addProcessor(processor: CatalogProcessor): void { @@ -64,7 +52,7 @@ export const catalogPlugin = createBackendPlugin({ const processingExtensions = new CatalogExtensionPointImpl(); // plugins depending on this API will be initialized before this plugins init method is executed. env.registerExtensionPoint( - catalogProcessingInitApiRef, + catalogProcessingExtentionPoint, processingExtensions, ); diff --git a/plugins/catalog-backend/src/service/index.ts b/plugins/catalog-backend/src/service/index.ts index ed24780c5a..7e0c6025c3 100644 --- a/plugins/catalog-backend/src/service/index.ts +++ b/plugins/catalog-backend/src/service/index.ts @@ -16,6 +16,4 @@ export type { CatalogEnvironment } from './CatalogBuilder'; export { CatalogBuilder } from './CatalogBuilder'; - -export type { CatalogProcessingInitApi } from './CatalogPlugin'; -export { catalogPlugin, catalogProcessingInitApiRef } from './CatalogPlugin'; +export { catalogPlugin } from './CatalogPlugin'; diff --git a/plugins/catalog-node/api-report.md b/plugins/catalog-node/api-report.md index d3460e4e27..cf81f494c5 100644 --- a/plugins/catalog-node/api-report.md +++ b/plugins/catalog-node/api-report.md @@ -8,6 +8,16 @@ import { CompoundEntityRef } from '@backstage/catalog-model'; import { Entity } from '@backstage/catalog-model'; import { JsonValue } from '@backstage/types'; +import { ServiceRef } from '@backstage/backend-plugin-api'; + +// @alpha (undocumented) +export interface CatalogProcessingExtensionPoint { + // (undocumented) + addProcessor(processor: CatalogProcessor): void; +} + +// @alpha (undocumented) +export const catalogProcessingExtentionPoint: ServiceRef; // @public (undocumented) export type CatalogProcessor = { diff --git a/plugins/catalog-node/package.json b/plugins/catalog-node/package.json index a1c96990a0..d6228c717f 100644 --- a/plugins/catalog-node/package.json +++ b/plugins/catalog-node/package.json @@ -25,6 +25,7 @@ "postpack": "backstage-cli package postpack" }, "dependencies": { + "@backstage/backend-plugin-api": "^0.0.0", "@backstage/catalog-model": "^1.1.0-next.2", "@backstage/errors": "1.1.0-next.0", "@backstage/types": "^1.0.0" diff --git a/plugins/catalog-node/src/extensions.ts b/plugins/catalog-node/src/extensions.ts new file mode 100644 index 0000000000..61605d3b26 --- /dev/null +++ b/plugins/catalog-node/src/extensions.ts @@ -0,0 +1,32 @@ +/* + * 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 { createServiceRef } from '@backstage/backend-plugin-api'; +import { CatalogProcessor } from './api/processor'; + +/** + * @alpha + */ +export interface CatalogProcessingExtensionPoint { + addProcessor(processor: CatalogProcessor): void; +} + +/** + * @alpha + */ +export const catalogProcessingExtentionPoint = + createServiceRef({ + id: 'catalog.processing', + }); diff --git a/plugins/catalog-node/src/index.ts b/plugins/catalog-node/src/index.ts index 6a1accfcbd..fc7a1b1b67 100644 --- a/plugins/catalog-node/src/index.ts +++ b/plugins/catalog-node/src/index.ts @@ -20,5 +20,7 @@ * @packageDocumentation */ +export type { CatalogProcessingExtensionPoint } from './extensions'; +export { catalogProcessingExtentionPoint } from './extensions'; export * from './api'; export * from './processing'; diff --git a/yarn.lock b/yarn.lock index fe5ff8cb76..e35aee85fb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1557,126 +1557,6 @@ lodash "^4.17.21" uuid "^8.0.0" -"@backstage/cli@^0.17.2-next.0": - version "0.17.2" - resolved "https://registry.npmjs.org/@backstage/cli/-/cli-0.17.2.tgz#2387b8d24d8af4828b84baaa62e6b444ec4330e6" - integrity sha512-stRJWmokD7SXnclZ1dsVfA1stUP4PQPkbi3GkwY1zM84y4M20dU+YHde7MrTILMPnhMY+16WdTdfbeqLfXOJrw== - dependencies: - "@backstage/cli-common" "^0.1.9" - "@backstage/config" "^1.0.1" - "@backstage/config-loader" "^1.1.2" - "@backstage/errors" "^1.0.0" - "@backstage/release-manifests" "^0.0.4" - "@backstage/types" "^1.0.0" - "@hot-loader/react-dom-v16" "npm:@hot-loader/react-dom@^16.0.2" - "@hot-loader/react-dom-v17" "npm:@hot-loader/react-dom@^17.0.2" - "@manypkg/get-packages" "^1.1.3" - "@octokit/request" "^5.4.12" - "@rollup/plugin-commonjs" "^22.0.0" - "@rollup/plugin-json" "^4.1.0" - "@rollup/plugin-node-resolve" "^13.0.0" - "@rollup/plugin-yaml" "^3.1.0" - "@spotify/eslint-config-base" "^13.0.0" - "@spotify/eslint-config-react" "^13.0.0" - "@spotify/eslint-config-typescript" "^13.0.0" - "@sucrase/jest-plugin" "^2.1.1" - "@sucrase/webpack-loader" "^2.0.0" - "@svgr/plugin-jsx" "6.2.x" - "@svgr/plugin-svgo" "6.2.x" - "@svgr/rollup" "6.2.x" - "@svgr/webpack" "6.2.x" - "@types/webpack-env" "^1.15.2" - "@typescript-eslint/eslint-plugin" "^5.9.0" - "@typescript-eslint/parser" "^5.9.0" - "@yarnpkg/lockfile" "^1.1.0" - "@yarnpkg/parsers" "^3.0.0-rc.4" - bfj "^7.0.2" - buffer "^6.0.3" - chalk "^4.0.0" - chokidar "^3.3.1" - commander "^9.1.0" - css-loader "^6.5.1" - diff "^5.0.0" - esbuild "^0.14.10" - esbuild-loader "^2.18.0" - eslint "^8.6.0" - eslint-config-prettier "^8.3.0" - eslint-formatter-friendly "^7.0.0" - eslint-plugin-deprecation "^1.3.2" - eslint-plugin-import "^2.25.4" - eslint-plugin-jest "^26.1.2" - eslint-plugin-jsx-a11y "^6.5.1" - eslint-plugin-monorepo "^0.3.2" - eslint-plugin-react "^7.28.0" - eslint-plugin-react-hooks "^4.3.0" - eslint-webpack-plugin "^3.1.1" - express "^4.17.1" - fork-ts-checker-webpack-plugin "^7.0.0-alpha.8" - fs-extra "10.1.0" - glob "^7.1.7" - global-agent "^3.0.0" - handlebars "^4.7.3" - html-webpack-plugin "^5.3.1" - inquirer "^8.2.0" - jest "^27.5.1" - jest-css-modules "^2.1.0" - jest-runtime "^27.5.1" - jest-transform-yaml "^1.0.0" - json-schema "^0.4.0" - lodash "^4.17.21" - mini-css-extract-plugin "^2.4.2" - minimatch "5.1.0" - node-fetch "^2.6.7" - node-libs-browser "^2.2.1" - npm-packlist "^5.0.0" - ora "^5.3.0" - postcss "^8.1.0" - process "^0.11.10" - react-dev-utils "^12.0.0-next.60" - react-hot-loader "^4.13.0" - recursive-readdir "^2.2.2" - replace-in-file "^6.0.0" - rollup "^2.60.2" - rollup-plugin-dts "^4.0.1" - rollup-plugin-esbuild "^4.7.2" - rollup-plugin-postcss "^4.0.0" - rollup-pluginutils "^2.8.2" - run-script-webpack-plugin "^0.0.14" - semver "^7.3.2" - style-loader "^3.3.1" - sucrase "^3.20.2" - tar "^6.1.2" - terser-webpack-plugin "^5.1.3" - util "^0.12.3" - webpack "^5.66.0" - webpack-dev-server "^4.7.3" - webpack-node-externals "^3.0.0" - yaml "^1.10.0" - yml-loader "^2.1.0" - yn "^4.0.0" - zod "^3.11.6" - -"@backstage/config-loader@^1.1.2": - version "1.1.2" - resolved "https://registry.npmjs.org/@backstage/config-loader/-/config-loader-1.1.2.tgz#72cb0d7b2647f5a646bb279360bc34732e06521f" - integrity sha512-c5ZO7xDJn609DBIsYAWGE5kgh+7SPYUmG2ADtVX9SbXaql3VCafGlhc2hAZQa/O12W04qi3GgwGg0bqSFmx5uw== - dependencies: - "@backstage/cli-common" "^0.1.9" - "@backstage/config" "^1.0.1" - "@backstage/errors" "^1.0.0" - "@backstage/types" "^1.0.0" - "@types/json-schema" "^7.0.6" - ajv "^8.10.0" - chokidar "^3.5.2" - fs-extra "10.1.0" - json-schema "^0.4.0" - json-schema-merge-allof "^0.8.1" - json-schema-traverse "^1.0.0" - node-fetch "^2.6.7" - typescript-json-schema "^0.53.0" - yaml "^1.9.2" - yup "^0.32.9" - "@backstage/core-components@^0.9.0", "@backstage/core-components@^0.9.5": version "0.9.5" resolved "https://registry.npmjs.org/@backstage/core-components/-/core-components-0.9.5.tgz#5a0b34867aaee0549bfa67b39a69c09588fa3c7a" @@ -5675,7 +5555,7 @@ dependencies: "@rollup/pluginutils" "^3.0.8" -"@rollup/plugin-node-resolve@^13.0.0", "@rollup/plugin-node-resolve@^13.0.6": +"@rollup/plugin-node-resolve@^13.0.6": version "13.3.0" resolved "https://registry.npmjs.org/@rollup/plugin-node-resolve/-/plugin-node-resolve-13.3.0.tgz#da1c5c5ce8316cef96a2f823d111c1e4e498801c" integrity sha512-Lus8rbUo1eEcnS4yTFKLZrVumLPY+YayBdWXgFSHYhTT2iJbMhoaaBL3xl5NCdeRytErGr8tZ0L71BMRmnlwSw== @@ -20624,11 +20504,6 @@ path-case@^3.0.4: dot-case "^3.0.4" tslib "^2.0.3" -path-equal@1.1.2: - version "1.1.2" - resolved "https://registry.npmjs.org/path-equal/-/path-equal-1.1.2.tgz#260e7c449c4c2022f68cc5fa6e617e892858250d" - integrity sha512-p5kxPPwCdbf5AdXzT1bUBJomhgBlEjRBavYNr1XUpMFIE4Hnf2roueCMXudZK5tnaAu1tTmp3GPzqwJK45IHEA== - path-equal@^1.1.2: version "1.2.2" resolved "https://registry.npmjs.org/path-equal/-/path-equal-1.2.2.tgz#fa2997f0a829de22ec8f5f86461ca5590d49b832" @@ -23153,11 +23028,6 @@ run-parallel@^1.1.9: resolved "https://registry.npmjs.org/run-parallel/-/run-parallel-1.1.9.tgz#c9dd3a7cf9f4b2c4b6244e173a6ed866e61dd679" integrity sha512-DEqnSRTDw/Tc3FXf49zedI638Z9onwUotBMiUFKmrO2sdFKIbXamXGQ3Axd4qgphxKB4kw/qP1w5kTxnfU1B9Q== -run-script-webpack-plugin@^0.0.14: - version "0.0.14" - resolved "https://registry.npmjs.org/run-script-webpack-plugin/-/run-script-webpack-plugin-0.0.14.tgz#fe2362b32c1dab7a8af7a6f1246fc043690cedd7" - integrity sha512-DXe6lzzEVXjBr/74zd4m4yOfmz5P6GMjzhQxDDsViOmwG7cap8UCE6RgD5rT7zf4wM83a+ToHnpB3v4efUv5IA== - run-script-webpack-plugin@^0.1.0: version "0.1.1" resolved "https://registry.npmjs.org/run-script-webpack-plugin/-/run-script-webpack-plugin-0.1.1.tgz#dad3114be32eb864d2160306e4d9c52a2c1cfd59" @@ -25441,20 +25311,6 @@ typedarray@^0.0.6: resolved "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= -typescript-json-schema@^0.53.0: - version "0.53.1" - resolved "https://registry.npmjs.org/typescript-json-schema/-/typescript-json-schema-0.53.1.tgz#9204547f3e145169b40928998366ff6d28b81d32" - integrity sha512-Hg+RnOKUd38MOzC0rDft03a8xvwO+gCcj1F77smw2tCoZYQpFoLtrXWBGdvCX+REliko5WYel2kux17HPFqjLQ== - dependencies: - "@types/json-schema" "^7.0.9" - "@types/node" "^16.9.2" - glob "^7.1.7" - path-equal "1.1.2" - safe-stable-stringify "^2.2.0" - ts-node "^10.2.1" - typescript "~4.6.0" - yargs "^17.1.1" - typescript-json-schema@^0.54.0: version "0.54.0" resolved "https://registry.npmjs.org/typescript-json-schema/-/typescript-json-schema-0.54.0.tgz#b3fc42ad90df6a0f6ab57571ebc8b4d41125df4f" From 9d9b7f77091f976ab79b8ec3d25d355af72773cf Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Wed, 6 Jul 2022 16:11:38 +0200 Subject: [PATCH 12/31] add missing docstrings Signed-off-by: Johan Haals --- .../services/definitions/cacheServiceRef.ts | 3 +++ .../services/definitions/configServiceRef.ts | 3 +++ .../definitions/databaseServiceRef.ts | 3 +++ .../definitions/discoveryServiceRef.ts | 3 +++ .../definitions/httpRouterServiceRef.ts | 24 +++++-------------- .../src/services/definitions/index.ts | 12 ---------- .../services/definitions/loggerServiceRef.ts | 6 +++++ .../definitions/permissionsServiceRef.ts | 3 +++ .../definitions/schedulerServiceRef.ts | 3 +++ .../definitions/scheldulerServiceRef.ts | 3 +++ .../definitions/tokenManagerServiceRef.ts | 3 +++ .../definitions/urlReaderServiceRef.ts | 3 +++ .../src/services/system/types.ts | 6 +++++ 13 files changed, 45 insertions(+), 30 deletions(-) diff --git a/packages/backend-plugin-api/src/services/definitions/cacheServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/cacheServiceRef.ts index 439a2b645d..0b572f1712 100644 --- a/packages/backend-plugin-api/src/services/definitions/cacheServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/cacheServiceRef.ts @@ -17,6 +17,9 @@ import { createServiceRef } from '../system/types'; import { PluginCacheManager } from '@backstage/backend-common'; +/** + * @public + */ export const cacheServiceRef = createServiceRef({ id: 'core.cache', }); diff --git a/packages/backend-plugin-api/src/services/definitions/configServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/configServiceRef.ts index 9a4199967d..ba5dcacdc8 100644 --- a/packages/backend-plugin-api/src/services/definitions/configServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/configServiceRef.ts @@ -17,6 +17,9 @@ import { Config } from '@backstage/config'; import { createServiceRef } from '../system/types'; +/** + * @public + */ export const configServiceRef = createServiceRef({ id: 'core.config', }); diff --git a/packages/backend-plugin-api/src/services/definitions/databaseServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/databaseServiceRef.ts index 62f7f8a157..b41159dc9e 100644 --- a/packages/backend-plugin-api/src/services/definitions/databaseServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/databaseServiceRef.ts @@ -17,6 +17,9 @@ import { PluginDatabaseManager } from '@backstage/backend-common'; import { createServiceRef } from '../system/types'; +/** + * @public + */ export const databaseServiceRef = createServiceRef({ id: 'core.database', }); diff --git a/packages/backend-plugin-api/src/services/definitions/discoveryServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/discoveryServiceRef.ts index 3dfd5fa62b..675ac206a2 100644 --- a/packages/backend-plugin-api/src/services/definitions/discoveryServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/discoveryServiceRef.ts @@ -17,6 +17,9 @@ import { createServiceRef } from '../system/types'; import { PluginEndpointDiscovery } from '@backstage/backend-common'; +/** + * @public + */ export const discoveryServiceRef = createServiceRef({ id: 'core.discovery', }); diff --git a/packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts index 65a01fde3a..fbc5a27e87 100644 --- a/packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/httpRouterServiceRef.ts @@ -17,28 +17,16 @@ import { createServiceRef } from '../system/types'; import { Handler } from 'express'; -// const apiRouter = Router(); -// apiRouter.use('/catalog', await catalog(catalogEnv)); -// const service = createServiceBuilder(module) -// .loadConfig(config) -// .addRouter('', await healthcheck(healthcheckEnv)) -// .addRouter('', metricsHandler()) -// .addRouter('/api', apiRouter) -// .addRouter('', await app(appEnv)); - -// interface BackstageRequest extends Request { -// identity?: BackstageIdentity; -// context?: Context; -// } - -// interface RequestIdentityService { -// getRequestIdentity(req: Request): BackstageIdentity | undefined; -// } - +/** + * @public + */ export interface HttpRouterService { use(handler: Handler): void; } +/** + * @public + */ export const httpRouterServiceRef = createServiceRef({ id: 'core.httpRouter', }); diff --git a/packages/backend-plugin-api/src/services/definitions/index.ts b/packages/backend-plugin-api/src/services/definitions/index.ts index 556aa88c72..797cfb5a76 100644 --- a/packages/backend-plugin-api/src/services/definitions/index.ts +++ b/packages/backend-plugin-api/src/services/definitions/index.ts @@ -14,18 +14,6 @@ * limitations under the License. */ -// export type PluginEnvironment = { -// logger: Logger; -// cache: PluginCacheManager; -// database: PluginDatabaseManager; -// config: Config; -// reader: UrlReader; -// discovery: PluginEndpointDiscovery; -// tokenManager: TokenManager; -// permissions: PermissionEvaluator | PermissionAuthorizer; -// scheduler: PluginTaskScheduler; -// }; - export { configServiceRef } from './configServiceRef'; export { httpRouterServiceRef } from './httpRouterServiceRef'; export type { HttpRouterService } from './httpRouterServiceRef'; diff --git a/packages/backend-plugin-api/src/services/definitions/loggerServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/loggerServiceRef.ts index 564ce15ded..e99bc4e7d4 100644 --- a/packages/backend-plugin-api/src/services/definitions/loggerServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/loggerServiceRef.ts @@ -16,11 +16,17 @@ import { createServiceRef } from '../system/types'; +/** + * @public + */ export interface Logger { info(message: string): void; child(fields: { [name: string]: string }): Logger; } +/** + * @public + */ export const loggerServiceRef = createServiceRef({ id: 'core.logger', }); diff --git a/packages/backend-plugin-api/src/services/definitions/permissionsServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/permissionsServiceRef.ts index 4355286611..b13ca9e240 100644 --- a/packages/backend-plugin-api/src/services/definitions/permissionsServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/permissionsServiceRef.ts @@ -20,6 +20,9 @@ import { PermissionEvaluator, } from '@backstage/plugin-permission-common'; +/** + * @public + */ export const permissionsServiceRef = createServiceRef< PermissionEvaluator | PermissionAuthorizer >({ diff --git a/packages/backend-plugin-api/src/services/definitions/schedulerServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/schedulerServiceRef.ts index 3a3abfcc10..ca442dd34d 100644 --- a/packages/backend-plugin-api/src/services/definitions/schedulerServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/schedulerServiceRef.ts @@ -17,6 +17,9 @@ import { createServiceRef } from '../system/types'; import { PluginTaskScheduler } from '@backstage/backend-tasks'; +/** + * @public + */ export const schedulerServiceRef = createServiceRef({ id: 'core.scheduler', }); diff --git a/packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts index 4355286611..b13ca9e240 100644 --- a/packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts @@ -20,6 +20,9 @@ import { PermissionEvaluator, } from '@backstage/plugin-permission-common'; +/** + * @public + */ export const permissionsServiceRef = createServiceRef< PermissionEvaluator | PermissionAuthorizer >({ diff --git a/packages/backend-plugin-api/src/services/definitions/tokenManagerServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/tokenManagerServiceRef.ts index c62d4bb634..6995033e81 100644 --- a/packages/backend-plugin-api/src/services/definitions/tokenManagerServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/tokenManagerServiceRef.ts @@ -17,6 +17,9 @@ import { createServiceRef } from '../system/types'; import { TokenManager } from '@backstage/backend-common'; +/** + * @public + */ export const tokenManagerServiceRef = createServiceRef({ id: 'core.tokenManager', }); diff --git a/packages/backend-plugin-api/src/services/definitions/urlReaderServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/urlReaderServiceRef.ts index 25a9666882..ebb32f1aed 100644 --- a/packages/backend-plugin-api/src/services/definitions/urlReaderServiceRef.ts +++ b/packages/backend-plugin-api/src/services/definitions/urlReaderServiceRef.ts @@ -17,6 +17,9 @@ import { createServiceRef } from '../system/types'; import { UrlReader } from '@backstage/backend-common'; +/** + * @public + */ export const urlReaderServiceRef = createServiceRef({ id: 'core.urlReader', }); diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index 2f59e65320..30d8d94c11 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -56,6 +56,9 @@ export type AnyServiceFactory = ServiceFactory< { [key in string]: unknown } >; +/** + * @public + */ export function createServiceRef(options: { id: string }): ServiceRef { return { id: options.id, @@ -69,6 +72,9 @@ export function createServiceRef(options: { id: string }): ServiceRef { }; } +/** + * @public + */ export function createServiceFactory< Api, Impl extends Api, From 5da7205bad3b2d70663b8d728e79b44258f4df87 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 5 Jul 2022 15:27:48 +0200 Subject: [PATCH 13/31] =?UTF-8?q?chore:=20wire=20up=20the=20service=20bygg?= =?UTF-8?q?are=20=F0=9F=93=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: Johan Haals Co-authored-by: Patrik Oldsberg Signed-off-by: blam --- .../implementations/httpRouterService.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/backend-app-api/src/services/implementations/httpRouterService.ts b/packages/backend-app-api/src/services/implementations/httpRouterService.ts index 82dfb13218..505906d13b 100644 --- a/packages/backend-app-api/src/services/implementations/httpRouterService.ts +++ b/packages/backend-app-api/src/services/implementations/httpRouterService.ts @@ -13,22 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import { createServiceFactory, httpRouterServiceRef, + configServiceRef, } from '@backstage/backend-plugin-api'; import Router from 'express-promise-router'; -import express, { Handler } from 'express'; +import { Handler } from 'express'; +import { createServiceBuilder } from '@backstage/backend-common'; export const httpRouterFactory = createServiceFactory({ service: httpRouterServiceRef, - deps: {}, - factory: async () => { - const app = express(); + deps: { + configFactory: configServiceRef, + }, + factory: async ({ configFactory }) => { const rootRouter = Router(); - - app.use(rootRouter); - app.listen(8123); + const service = createServiceBuilder(module) + .loadConfig(await configFactory('root')) + .addRouter('', rootRouter); + await service.start(); return async (pluginId?: string) => { if (!pluginId) { From f6404275c796b10bca90df145552d786dc1d8ec9 Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 6 Jul 2022 16:41:49 +0200 Subject: [PATCH 14/31] chore: added in the router things: Co-authored-by: Johan Haals Signed-off-by: blam --- .../implementations/httpRouterService.ts | 8 +++---- .../src/services/system/types.ts | 2 +- packages/backend/src/next/index.ts | 24 +++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/backend-app-api/src/services/implementations/httpRouterService.ts b/packages/backend-app-api/src/services/implementations/httpRouterService.ts index 505906d13b..1f72378643 100644 --- a/packages/backend-app-api/src/services/implementations/httpRouterService.ts +++ b/packages/backend-app-api/src/services/implementations/httpRouterService.ts @@ -30,18 +30,18 @@ export const httpRouterFactory = createServiceFactory({ }, factory: async ({ configFactory }) => { const rootRouter = Router(); + const service = createServiceBuilder(module) .loadConfig(await configFactory('root')) .addRouter('', rootRouter); + await service.start(); return async (pluginId?: string) => { - if (!pluginId) { - return rootRouter; - } + const path = pluginId ? `/api/${pluginId}` : ''; return { use(handler: Handler) { - rootRouter.use(`/api/${pluginId}`, handler); + rootRouter.use(path, handler); }, }; }; diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index 30d8d94c11..40534fcbcb 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -79,6 +79,6 @@ export function createServiceFactory< Api, Impl extends Api, Deps extends { [name in string]: unknown }, ->(factory: ServiceFactory): ServiceFactory { +>(factory: ServiceFactory): ServiceFactory { return factory; } diff --git a/packages/backend/src/next/index.ts b/packages/backend/src/next/index.ts index 0a16160a23..96b36e3e03 100644 --- a/packages/backend/src/next/index.ts +++ b/packages/backend/src/next/index.ts @@ -39,6 +39,30 @@ import { // }, // }); +// export const appPlugin = createBackendPlugin({ +// id: 'app', +// register(env) { +// env.registerInit({ +// deps: { +// httpRouter: httpRouterServiceRef, +// database: databaseServiceRef, +// logger: loggerServiceRef, +// config: configServiceRef, +// }, +// async init({ httpRouter, config, logger, database }) { +// console.log('App plugin init'); + +// const appBackendRoute = await createAppBackendRouter({ +// appPackageName: 'example-app', +// config, +// logger: loggerToWinstonLogger(logger), +// database, +// }); +// }, +// }); +// }, +// }); + const backend = createBackend({ apis: [], }); From 634b191757cac7aef316f62ff1b33800fc996e2d Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 6 Jul 2022 16:45:22 +0200 Subject: [PATCH 15/31] chore: fix imports in the catalog plugin ja! Signed-off-by: blam Signed-off-by: Patrik Oldsberg --- plugins/catalog-backend/src/service/CatalogPlugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/catalog-backend/src/service/CatalogPlugin.ts b/plugins/catalog-backend/src/service/CatalogPlugin.ts index d6dc4d11da..a2fcfe3f4b 100644 --- a/plugins/catalog-backend/src/service/CatalogPlugin.ts +++ b/plugins/catalog-backend/src/service/CatalogPlugin.ts @@ -23,9 +23,9 @@ import { urlReaderServiceRef, httpRouterServiceRef, } from '@backstage/backend-plugin-api'; -import { CatalogProcessor } from '@backstage/plugin-catalog-node'; import { CatalogBuilder } from './CatalogBuilder'; import { + CatalogProcessor, CatalogProcessingExtensionPoint, catalogProcessingExtentionPoint, } from '@backstage/plugin-catalog-node'; From 506ddb6ac883943c08c01c111b9bca639b46cfe6 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Thu, 7 Jul 2022 14:00:12 +0200 Subject: [PATCH 16/31] Rename catalog-node plugin, add scaffolder extension Signed-off-by: Johan Haals --- packages/backend/src/next/index.ts | 27 ++----------- plugins/scaffolder-backend/api-report.md | 6 +++ plugins/scaffolder-backend/package.json | 8 +++- .../extension/ScaffolderCatalogExtension.ts | 39 +++++++++++++++++++ .../scaffolder-backend/src/extension/index.ts | 16 ++++++++ plugins/scaffolder-backend/src/index.ts | 1 + 6 files changed, 71 insertions(+), 26 deletions(-) create mode 100644 plugins/scaffolder-backend/src/extension/ScaffolderCatalogExtension.ts create mode 100644 plugins/scaffolder-backend/src/extension/index.ts diff --git a/packages/backend/src/next/index.ts b/packages/backend/src/next/index.ts index 96b36e3e03..9deab2feeb 100644 --- a/packages/backend/src/next/index.ts +++ b/packages/backend/src/next/index.ts @@ -15,29 +15,8 @@ */ import { createBackend } from '@backstage/backend-app-api'; -// import { createBackendModule } from '@backstage/backend-plugin-api'; -import { - catalogPlugin, - // catalogProcessingInitApiRef, -} from '@backstage/plugin-catalog-backend'; -// import { ScaffolderEntitiesProcessor } from '@backstage/plugin-scaffolder-backend'; - -// export const scaffolderCatalogExtension = createBackendModule({ -// moduleId: 'scaffolder.extention', -// pluginId: 'catalog', -// register(env) { -// env.registerInit({ -// deps: { -// catalogProcessingInitApi: catalogProcessingInitApiRef, -// }, -// async init({ catalogProcessingInitApi }) { -// catalogProcessingInitApi.addProcessor( -// new ScaffolderEntitiesProcessor(), -// ); -// }, -// }); -// }, -// }); +import { catalogPlugin } from '@backstage/plugin-catalog-backend'; +import { scaffolderCatalogExtension } from '@backstage/plugin-scaffolder-backend'; // export const appPlugin = createBackendPlugin({ // id: 'app', @@ -68,5 +47,5 @@ const backend = createBackend({ }); backend.add(catalogPlugin({})); -// backend.add(scaffolderCatalogExtension({})); +backend.add(scaffolderCatalogExtension({})); backend.start(); diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index 2053f01e39..672b853f3b 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -5,6 +5,7 @@ ```ts /// +import { BackendRegistrable } from '@backstage/backend-plugin-api'; import { CatalogApi } from '@backstage/catalog-client'; import { CatalogProcessor } from '@backstage/plugin-catalog-backend'; import { CatalogProcessorEmit } from '@backstage/plugin-catalog-backend'; @@ -543,6 +544,11 @@ export type RunCommandOptions = { logStream?: Writable; }; +// @alpha +export const scaffolderCatalogExtension: ( + option: unknown, +) => BackendRegistrable; + // @public (undocumented) export class ScaffolderEntitiesProcessor implements CatalogProcessor { // (undocumented) diff --git a/plugins/scaffolder-backend/package.json b/plugins/scaffolder-backend/package.json index a1bb552435..f2fabad69d 100644 --- a/plugins/scaffolder-backend/package.json +++ b/plugins/scaffolder-backend/package.json @@ -9,7 +9,8 @@ "publishConfig": { "access": "public", "main": "dist/index.cjs.js", - "types": "dist/index.d.ts" + "types": "dist/index.d.ts", + "alphaTypes": "dist/index.alpha.d.ts" }, "backstage": { "role": "backend-plugin" @@ -25,7 +26,7 @@ ], "scripts": { "start": "backstage-cli package start", - "build": "backstage-cli package build", + "build": "backstage-cli package build --experimental-type-build", "lint": "backstage-cli package lint", "test": "backstage-cli package test", "prepack": "backstage-cli package prepack", @@ -42,6 +43,8 @@ "@backstage/integration": "^1.2.2-next.2", "@backstage/plugin-catalog-backend": "^1.2.1-next.2", "@backstage/plugin-scaffolder-common": "^1.1.2-next.0", + "@backstage/backend-plugin-api": "^0.0.0", + "@backstage/plugin-catalog-node": "^0.0.0", "@backstage/types": "^1.0.0", "@gitbeaker/core": "^35.6.0", "@gitbeaker/node": "^35.1.0", @@ -93,6 +96,7 @@ "yaml": "^1.10.0" }, "files": [ + "alpha", "dist", "migrations", "config.d.ts", diff --git a/plugins/scaffolder-backend/src/extension/ScaffolderCatalogExtension.ts b/plugins/scaffolder-backend/src/extension/ScaffolderCatalogExtension.ts new file mode 100644 index 0000000000..9ca140a952 --- /dev/null +++ b/plugins/scaffolder-backend/src/extension/ScaffolderCatalogExtension.ts @@ -0,0 +1,39 @@ +/* + * 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 { createBackendModule } from '@backstage/backend-plugin-api'; +import { catalogProcessingExtentionPoint } from '@backstage/plugin-catalog-node'; +import { ScaffolderEntitiesProcessor } from '../processor'; + +/** + * @alpha + * Registers the ScaffolderEntitiesProcessor with the catalog processing extension point. + */ +export const scaffolderCatalogExtension = createBackendModule({ + moduleId: 'scaffolder.extention', + pluginId: 'catalog', + register(env) { + env.registerInit({ + deps: { + catalogProcessingExtensionPoint: catalogProcessingExtentionPoint, + }, + async init({ catalogProcessingExtensionPoint }) { + catalogProcessingExtensionPoint.addProcessor( + new ScaffolderEntitiesProcessor(), + ); + }, + }); + }, +}); diff --git a/plugins/scaffolder-backend/src/extension/index.ts b/plugins/scaffolder-backend/src/extension/index.ts new file mode 100644 index 0000000000..672e29e2ee --- /dev/null +++ b/plugins/scaffolder-backend/src/extension/index.ts @@ -0,0 +1,16 @@ +/* + * 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. + */ +export { scaffolderCatalogExtension } from './ScaffolderCatalogExtension'; diff --git a/plugins/scaffolder-backend/src/index.ts b/plugins/scaffolder-backend/src/index.ts index 5978b6389b..a1032449d5 100644 --- a/plugins/scaffolder-backend/src/index.ts +++ b/plugins/scaffolder-backend/src/index.ts @@ -24,3 +24,4 @@ export * from './scaffolder'; export * from './service/router'; export * from './lib'; export * from './processor'; +export * from './extension'; From aedb038a8b3bfe19b91c33022db2fbe17f5dc784 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 10:43:49 +0200 Subject: [PATCH 17/31] backend-plugin-api: use type instead of interface to work around api-extractor bug Signed-off-by: Patrik Oldsberg --- packages/backend-plugin-api/src/services/system/types.ts | 4 ++-- packages/backend-plugin-api/src/wiring/types.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index 40534fcbcb..66b3e89e3c 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -19,7 +19,7 @@ * * @public */ -export interface ServiceRef { +export type ServiceRef = { id: string; /** @@ -31,7 +31,7 @@ export interface ServiceRef { toString(): string; $$ref: 'service'; -} +}; type TypesToServiceRef = { [key in keyof T]: ServiceRef }; type DepsToDepFactories = { diff --git a/packages/backend-plugin-api/src/wiring/types.ts b/packages/backend-plugin-api/src/wiring/types.ts index 746d47b355..be4bf949e7 100644 --- a/packages/backend-plugin-api/src/wiring/types.ts +++ b/packages/backend-plugin-api/src/wiring/types.ts @@ -21,7 +21,7 @@ import { ServiceRef } from '../services/system/types'; * * @public */ -export interface ExtensionPoint { +export type ExtensionPoint = { id: string; /** @@ -34,7 +34,7 @@ export interface ExtensionPoint { toString(): string; $$ref: 'extension-point'; -} +}; export function createExtensionPoint(options: { id: string; From 922f53f5926702a53a1c18900151d40889fe770e Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 10:45:17 +0200 Subject: [PATCH 18/31] backend-plugin-api: add API report Signed-off-by: Patrik Oldsberg --- packages/backend-plugin-api/api-report.md | 206 ++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 packages/backend-plugin-api/api-report.md diff --git a/packages/backend-plugin-api/api-report.md b/packages/backend-plugin-api/api-report.md new file mode 100644 index 0000000000..875f589d17 --- /dev/null +++ b/packages/backend-plugin-api/api-report.md @@ -0,0 +1,206 @@ +## API Report File for "@backstage/backend-plugin-api" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts +import { Config } from '@backstage/config'; +import { Handler } from 'express'; +import { PermissionAuthorizer } from '@backstage/plugin-permission-common'; +import { PermissionEvaluator } from '@backstage/plugin-permission-common'; +import { PluginCacheManager } from '@backstage/backend-common'; +import { PluginDatabaseManager } from '@backstage/backend-common'; +import { PluginEndpointDiscovery } from '@backstage/backend-common'; +import { PluginTaskScheduler } from '@backstage/backend-tasks'; +import { TokenManager } from '@backstage/backend-common'; +import { UrlReader } from '@backstage/backend-common'; + +// Warning: (ae-missing-release-tag) "AnyServiceFactory" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type AnyServiceFactory = ServiceFactory< + unknown, + unknown, + { + [key in string]: unknown; + } +>; + +// Warning: (ae-missing-release-tag) "BackendInitRegistry" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export interface BackendInitRegistry { + // (undocumented) + registerExtensionPoint( + ref: ServiceRef, + impl: TExtensionPoint, + ): void; + // (undocumented) + registerInit< + Deps extends { + [name in string]: unknown; + }, + >(options: { + deps: { + [name in keyof Deps]: ServiceRef; + }; + init: (deps: Deps) => Promise; + }): void; +} + +// Warning: (ae-missing-release-tag) "BackendModuleConfig" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export interface BackendModuleConfig { + // (undocumented) + moduleId: string; + // (undocumented) + pluginId: string; + // (undocumented) + register( + reg: Omit, + options: TOptions, + ): void; +} + +// Warning: (ae-missing-release-tag) "BackendPluginConfig" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export interface BackendPluginConfig { + // (undocumented) + id: string; + // (undocumented) + register(reg: BackendInitRegistry, options: TOptions): void; +} + +// Warning: (ae-missing-release-tag) "BackendRegistrable" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export interface BackendRegistrable { + // (undocumented) + id: string; + // (undocumented) + register(reg: BackendInitRegistry): void; +} + +// @public (undocumented) +export const cacheServiceRef: ServiceRef; + +// @public (undocumented) +export const configServiceRef: ServiceRef; + +// Warning: (ae-missing-release-tag) "createBackendModule" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export function createBackendModule( + config: BackendModuleConfig, +): (option: TOptions) => BackendRegistrable; + +// Warning: (ae-missing-release-tag) "createBackendPlugin" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export function createBackendPlugin( + config: BackendPluginConfig, +): (option: TOptions) => BackendRegistrable; + +// Warning: (ae-missing-release-tag) "createExtensionPoint" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export function createExtensionPoint(options: { + id: string; +}): ExtensionPoint; + +// @public (undocumented) +export function createServiceFactory< + Api, + Impl extends Api, + Deps extends { + [name in string]: unknown; + }, +>(factory: ServiceFactory): ServiceFactory; + +// @public (undocumented) +export function createServiceRef(options: { id: string }): ServiceRef; + +// @public (undocumented) +export const databaseServiceRef: ServiceRef; + +// @public (undocumented) +export const discoveryServiceRef: ServiceRef; + +// @public +export type ExtensionPoint = { + id: string; + T: T; + toString(): string; + $$ref: 'extension-point'; +}; + +// Warning: (ae-missing-release-tag) "FactoryFunc" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type FactoryFunc = (pluginId: string) => Promise; + +// @public (undocumented) +export interface HttpRouterService { + // (undocumented) + use(handler: Handler): void; +} + +// @public (undocumented) +export const httpRouterServiceRef: ServiceRef; + +// @public (undocumented) +export interface Logger { + // (undocumented) + child(fields: { [name: string]: string }): Logger; + // (undocumented) + info(message: string): void; +} + +// @public (undocumented) +export const loggerServiceRef: ServiceRef; + +// @public (undocumented) +export const permissionsServiceRef: ServiceRef< + PermissionAuthorizer | PermissionEvaluator +>; + +// @public (undocumented) +export const schedulerServiceRef: ServiceRef; + +// Warning: (ae-missing-release-tag) "ServiceFactory" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type ServiceFactory< + TApi, + TImpl extends TApi, + TDeps extends { + [name in string]: unknown; + }, +> = { + service: ServiceRef; + deps: TypesToServiceRef; + factory(deps: DepsToDepFactories): Promise>; +}; + +// @public +export type ServiceRef = { + id: string; + T: T; + toString(): string; + $$ref: 'service'; +}; + +// @public (undocumented) +export const tokenManagerServiceRef: ServiceRef; + +// @public (undocumented) +export const urlReaderServiceRef: ServiceRef; + +// Warnings were encountered during analysis: +// +// src/services/system/types.d.ts:27:5 - (ae-forgotten-export) The symbol "TypesToServiceRef" needs to be exported by the entry point index.d.ts +// src/services/system/types.d.ts:28:5 - (ae-forgotten-export) The symbol "DepsToDepFactories" needs to be exported by the entry point index.d.ts +// src/wiring/types.d.ts:10:67 - (tsdoc-code-span-missing-delimiter) The code span is missing its closing backtick +// src/wiring/types.d.ts:11:24 - (tsdoc-code-span-missing-delimiter) The code span is missing its closing backtick +``` From a3cdfcd07821025c38fa1695bf7334783d55bb7a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 10:56:46 +0200 Subject: [PATCH 19/31] backend-plugin-api: fix API report warnings Signed-off-by: Patrik Oldsberg --- packages/backend-plugin-api/api-report.md | 37 +++++-------------- .../src/services/system/types.ts | 10 ++++- .../backend-plugin-api/src/wiring/types.ts | 12 ++++-- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/packages/backend-plugin-api/api-report.md b/packages/backend-plugin-api/api-report.md index 875f589d17..a4d45a2fb7 100644 --- a/packages/backend-plugin-api/api-report.md +++ b/packages/backend-plugin-api/api-report.md @@ -14,8 +14,6 @@ import { PluginTaskScheduler } from '@backstage/backend-tasks'; import { TokenManager } from '@backstage/backend-common'; import { UrlReader } from '@backstage/backend-common'; -// Warning: (ae-missing-release-tag) "AnyServiceFactory" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export type AnyServiceFactory = ServiceFactory< unknown, @@ -25,8 +23,6 @@ export type AnyServiceFactory = ServiceFactory< } >; -// Warning: (ae-missing-release-tag) "BackendInitRegistry" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export interface BackendInitRegistry { // (undocumented) @@ -47,8 +43,6 @@ export interface BackendInitRegistry { }): void; } -// Warning: (ae-missing-release-tag) "BackendModuleConfig" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export interface BackendModuleConfig { // (undocumented) @@ -62,8 +56,6 @@ export interface BackendModuleConfig { ): void; } -// Warning: (ae-missing-release-tag) "BackendPluginConfig" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export interface BackendPluginConfig { // (undocumented) @@ -72,8 +64,6 @@ export interface BackendPluginConfig { register(reg: BackendInitRegistry, options: TOptions): void; } -// Warning: (ae-missing-release-tag) "BackendRegistrable" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export interface BackendRegistrable { // (undocumented) @@ -88,22 +78,16 @@ export const cacheServiceRef: ServiceRef; // @public (undocumented) export const configServiceRef: ServiceRef; -// Warning: (ae-missing-release-tag) "createBackendModule" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export function createBackendModule( config: BackendModuleConfig, ): (option: TOptions) => BackendRegistrable; -// Warning: (ae-missing-release-tag) "createBackendPlugin" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export function createBackendPlugin( config: BackendPluginConfig, ): (option: TOptions) => BackendRegistrable; -// Warning: (ae-missing-release-tag) "createExtensionPoint" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export function createExtensionPoint(options: { id: string; @@ -124,6 +108,11 @@ export function createServiceRef(options: { id: string }): ServiceRef; // @public (undocumented) export const databaseServiceRef: ServiceRef; +// @public (undocumented) +export type DepsToDepFactories = { + [key in keyof T]: (pluginId: string) => Promise; +}; + // @public (undocumented) export const discoveryServiceRef: ServiceRef; @@ -135,8 +124,6 @@ export type ExtensionPoint = { $$ref: 'extension-point'; }; -// Warning: (ae-missing-release-tag) "FactoryFunc" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export type FactoryFunc = (pluginId: string) => Promise; @@ -168,8 +155,6 @@ export const permissionsServiceRef: ServiceRef< // @public (undocumented) export const schedulerServiceRef: ServiceRef; -// Warning: (ae-missing-release-tag) "ServiceFactory" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export type ServiceFactory< TApi, @@ -195,12 +180,10 @@ export type ServiceRef = { export const tokenManagerServiceRef: ServiceRef; // @public (undocumented) -export const urlReaderServiceRef: ServiceRef; +export type TypesToServiceRef = { + [key in keyof T]: ServiceRef; +}; -// Warnings were encountered during analysis: -// -// src/services/system/types.d.ts:27:5 - (ae-forgotten-export) The symbol "TypesToServiceRef" needs to be exported by the entry point index.d.ts -// src/services/system/types.d.ts:28:5 - (ae-forgotten-export) The symbol "DepsToDepFactories" needs to be exported by the entry point index.d.ts -// src/wiring/types.d.ts:10:67 - (tsdoc-code-span-missing-delimiter) The code span is missing its closing backtick -// src/wiring/types.d.ts:11:24 - (tsdoc-code-span-missing-delimiter) The code span is missing its closing backtick +// @public (undocumented) +export const urlReaderServiceRef: ServiceRef; ``` diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index 66b3e89e3c..d578434170 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -33,13 +33,18 @@ export type ServiceRef = { $$ref: 'service'; }; -type TypesToServiceRef = { [key in keyof T]: ServiceRef }; -type DepsToDepFactories = { +/** @public */ +export type TypesToServiceRef = { [key in keyof T]: ServiceRef }; + +/** @public */ +export type DepsToDepFactories = { [key in keyof T]: (pluginId: string) => Promise; }; +/** @public */ export type FactoryFunc = (pluginId: string) => Promise; +/** @public */ export type ServiceFactory< TApi, TImpl extends TApi, @@ -50,6 +55,7 @@ export type ServiceFactory< factory(deps: DepsToDepFactories): Promise>; }; +/** @public */ export type AnyServiceFactory = ServiceFactory< unknown, unknown, diff --git a/packages/backend-plugin-api/src/wiring/types.ts b/packages/backend-plugin-api/src/wiring/types.ts index be4bf949e7..2b4aacb40a 100644 --- a/packages/backend-plugin-api/src/wiring/types.ts +++ b/packages/backend-plugin-api/src/wiring/types.ts @@ -25,9 +25,8 @@ export type ExtensionPoint = { id: string; /** - * Utility for getting the type of the extension point, using `typeof - * extensionPoint.T`. Attempting to actually read this value will result in an - * exception. + * Utility for getting the type of the extension point, using `typeof extensionPoint.T`. + * Attempting to actually read this value will result in an exception. */ T: T; @@ -36,6 +35,7 @@ export type ExtensionPoint = { $$ref: 'extension-point'; }; +/** @public */ export function createExtensionPoint(options: { id: string; }): ExtensionPoint { @@ -51,6 +51,7 @@ export function createExtensionPoint(options: { }; } +/** @public */ export interface BackendInitRegistry { registerExtensionPoint( ref: ServiceRef, @@ -62,16 +63,19 @@ export interface BackendInitRegistry { }): void; } +/** @public */ export interface BackendRegistrable { id: string; register(reg: BackendInitRegistry): void; } +/** @public */ export interface BackendPluginConfig { id: string; register(reg: BackendInitRegistry, options: TOptions): void; } +/** @public */ export function createBackendPlugin( config: BackendPluginConfig, ): (option: TOptions) => BackendRegistrable { @@ -83,6 +87,7 @@ export function createBackendPlugin( }); } +/** @public */ export interface BackendModuleConfig { pluginId: string; moduleId: string; @@ -92,6 +97,7 @@ export interface BackendModuleConfig { ): void; } +/** @public */ export function createBackendModule( config: BackendModuleConfig, ): (option: TOptions) => BackendRegistrable { From e360c28f226e6beec820f0c96221ec5b615203a4 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 8 Jul 2022 11:18:50 +0200 Subject: [PATCH 20/31] Rename scaffolderCatalogModule Signed-off-by: Johan Haals --- packages/backend/src/next/index.ts | 4 +- .../extension/ScaffolderCatalogModule.test.ts | 42 +++++++++++++++++++ ...xtension.ts => ScaffolderCatalogModule.ts} | 4 +- .../scaffolder-backend/src/extension/index.ts | 2 +- 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 plugins/scaffolder-backend/src/extension/ScaffolderCatalogModule.test.ts rename plugins/scaffolder-backend/src/extension/{ScaffolderCatalogExtension.ts => ScaffolderCatalogModule.ts} (92%) diff --git a/packages/backend/src/next/index.ts b/packages/backend/src/next/index.ts index 9deab2feeb..cda7ae4007 100644 --- a/packages/backend/src/next/index.ts +++ b/packages/backend/src/next/index.ts @@ -16,7 +16,7 @@ import { createBackend } from '@backstage/backend-app-api'; import { catalogPlugin } from '@backstage/plugin-catalog-backend'; -import { scaffolderCatalogExtension } from '@backstage/plugin-scaffolder-backend'; +import { scaffolderCatalogModule } from '@backstage/plugin-scaffolder-backend'; // export const appPlugin = createBackendPlugin({ // id: 'app', @@ -47,5 +47,5 @@ const backend = createBackend({ }); backend.add(catalogPlugin({})); -backend.add(scaffolderCatalogExtension({})); +backend.add(scaffolderCatalogModule({})); backend.start(); diff --git a/plugins/scaffolder-backend/src/extension/ScaffolderCatalogModule.test.ts b/plugins/scaffolder-backend/src/extension/ScaffolderCatalogModule.test.ts new file mode 100644 index 0000000000..f07eb0f7ae --- /dev/null +++ b/plugins/scaffolder-backend/src/extension/ScaffolderCatalogModule.test.ts @@ -0,0 +1,42 @@ +/* + * 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 { BackendInitRegistry } from '@backstage/backend-plugin-api'; +import { ScaffolderEntitiesProcessor } from '../processor'; +import { scaffolderCatalogModule } from './ScaffolderCatalogModule'; + +describe('ScaffolderCatalogModule', () => { + it('should register the extension point', () => { + // TODO(jhaals): clean this up and add test helpers for backend system. + const ext = scaffolderCatalogModule({}); + expect(ext.id).toBe('catalog.scaffolder.module'); + const registry: jest.Mocked = { + registerInit: jest.fn(), + } as any; + ext.register(registry); + + const extensionPoint = { + addProcessor: jest.fn(), + }; + + registry.registerInit.mock.calls[0][0].init({ + catalogProcessingExtensionPoint: extensionPoint, + }); + expect(extensionPoint.addProcessor).toHaveBeenCalledWith( + new ScaffolderEntitiesProcessor(), + ); + }); +}); diff --git a/plugins/scaffolder-backend/src/extension/ScaffolderCatalogExtension.ts b/plugins/scaffolder-backend/src/extension/ScaffolderCatalogModule.ts similarity index 92% rename from plugins/scaffolder-backend/src/extension/ScaffolderCatalogExtension.ts rename to plugins/scaffolder-backend/src/extension/ScaffolderCatalogModule.ts index 9ca140a952..bdce46a31a 100644 --- a/plugins/scaffolder-backend/src/extension/ScaffolderCatalogExtension.ts +++ b/plugins/scaffolder-backend/src/extension/ScaffolderCatalogModule.ts @@ -21,8 +21,8 @@ import { ScaffolderEntitiesProcessor } from '../processor'; * @alpha * Registers the ScaffolderEntitiesProcessor with the catalog processing extension point. */ -export const scaffolderCatalogExtension = createBackendModule({ - moduleId: 'scaffolder.extention', +export const scaffolderCatalogModule = createBackendModule({ + moduleId: 'scaffolder.module', pluginId: 'catalog', register(env) { env.registerInit({ diff --git a/plugins/scaffolder-backend/src/extension/index.ts b/plugins/scaffolder-backend/src/extension/index.ts index 672e29e2ee..c827d9cafd 100644 --- a/plugins/scaffolder-backend/src/extension/index.ts +++ b/plugins/scaffolder-backend/src/extension/index.ts @@ -13,4 +13,4 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -export { scaffolderCatalogExtension } from './ScaffolderCatalogExtension'; +export { scaffolderCatalogModule } from './ScaffolderCatalogModule'; From 9ce1bc564f07c4c48ec8d90a0ed172299889bec7 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 8 Jul 2022 11:19:21 +0200 Subject: [PATCH 21/31] Add addEntityProvider method to catalog extension Signed-off-by: Johan Haals --- plugins/catalog-backend/src/service/CatalogPlugin.ts | 11 +++++++++++ plugins/catalog-node/src/extensions.ts | 2 ++ 2 files changed, 13 insertions(+) diff --git a/plugins/catalog-backend/src/service/CatalogPlugin.ts b/plugins/catalog-backend/src/service/CatalogPlugin.ts index a2fcfe3f4b..fd3397612f 100644 --- a/plugins/catalog-backend/src/service/CatalogPlugin.ts +++ b/plugins/catalog-backend/src/service/CatalogPlugin.ts @@ -28,18 +28,28 @@ import { CatalogProcessor, CatalogProcessingExtensionPoint, catalogProcessingExtentionPoint, + EntityProvider, } from '@backstage/plugin-catalog-node'; class CatalogExtensionPointImpl implements CatalogProcessingExtensionPoint { #processors = new Array(); + #entityProviders = new Array(); addProcessor(processor: CatalogProcessor): void { this.#processors.push(processor); } + addEntityProvider(provider: EntityProvider): void { + this.#entityProviders.push(provider); + } + get processors() { return this.#processors; } + + get entityProviders() { + return this.#entityProviders; + } } /** @@ -82,6 +92,7 @@ export const catalogPlugin = createBackendPlugin({ logger: winstonLogger, }); builder.addProcessor(...processingExtensions.processors); + builder.addEntityProvider(...processingExtensions.entityProviders); const { processingEngine, router } = await builder.build(); await processingEngine.start(); diff --git a/plugins/catalog-node/src/extensions.ts b/plugins/catalog-node/src/extensions.ts index 61605d3b26..560d9d4db1 100644 --- a/plugins/catalog-node/src/extensions.ts +++ b/plugins/catalog-node/src/extensions.ts @@ -14,6 +14,7 @@ * limitations under the License. */ import { createServiceRef } from '@backstage/backend-plugin-api'; +import { EntityProvider } from './api'; import { CatalogProcessor } from './api/processor'; /** @@ -21,6 +22,7 @@ import { CatalogProcessor } from './api/processor'; */ export interface CatalogProcessingExtensionPoint { addProcessor(processor: CatalogProcessor): void; + addEntityProvider(provider: EntityProvider): void; } /** From 5f7c8c45f4d84d4763fbcda0516c79e1eab56a0a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 14:15:15 +0200 Subject: [PATCH 22/31] backend: move additions to new backend-next package Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- packages/backend-next/.eslintrc.js | 1 + packages/backend-next/README.md | 5 +++ packages/backend-next/package.json | 38 +++++++++++++++++++ packages/backend-next/src/index.test.ts | 24 ++++++++++++ .../src/next => backend-next/src}/index.ts | 24 ------------ packages/backend/package.json | 2 - 6 files changed, 68 insertions(+), 26 deletions(-) create mode 100644 packages/backend-next/.eslintrc.js create mode 100644 packages/backend-next/README.md create mode 100644 packages/backend-next/package.json create mode 100644 packages/backend-next/src/index.test.ts rename packages/{backend/src/next => backend-next/src}/index.ts (58%) diff --git a/packages/backend-next/.eslintrc.js b/packages/backend-next/.eslintrc.js new file mode 100644 index 0000000000..e2a53a6ad2 --- /dev/null +++ b/packages/backend-next/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require('@backstage/cli/config/eslint-factory')(__dirname); diff --git a/packages/backend-next/README.md b/packages/backend-next/README.md new file mode 100644 index 0000000000..94fc0767c4 --- /dev/null +++ b/packages/backend-next/README.md @@ -0,0 +1,5 @@ +# example-backend-next + +This is an example backend for the new **EXPERIMENTAL** Backstage backend system. + +Do not use this in your own projects. diff --git a/packages/backend-next/package.json b/packages/backend-next/package.json new file mode 100644 index 0000000000..6feaf5ad97 --- /dev/null +++ b/packages/backend-next/package.json @@ -0,0 +1,38 @@ +{ + "name": "example-backend-next", + "version": "0.2.73-next.2", + "main": "dist/index.cjs.js", + "types": "src/index.ts", + "license": "Apache-2.0", + "private": true, + "backstage": { + "role": "backend" + }, + "homepage": "https://backstage.io", + "repository": { + "type": "git", + "url": "https://github.com/backstage/backstage", + "directory": "packages/backend-next" + }, + "keywords": [ + "backstage" + ], + "scripts": { + "start": "backstage-cli package start", + "build": "backstage-cli package build", + "lint": "backstage-cli package lint", + "test": "backstage-cli package test", + "clean": "backstage-cli package clean" + }, + "dependencies": { + "@backstage/backend-app-api": "^0.0.0", + "@backstage/plugin-catalog-backend": "^1.2.1-next.2", + "@backstage/plugin-scaffolder-backend": "^1.4.0-next.2" + }, + "devDependencies": { + "@backstage/cli": "^0.18.0-next.2" + }, + "files": [ + "dist" + ] +} diff --git a/packages/backend-next/src/index.test.ts b/packages/backend-next/src/index.test.ts new file mode 100644 index 0000000000..8b41455b27 --- /dev/null +++ b/packages/backend-next/src/index.test.ts @@ -0,0 +1,24 @@ +/* + * 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 { PluginEnvironment } from './types'; + +describe('test', () => { + it('unbreaks the test runner', () => { + const unbreaker = {} as PluginEnvironment; + expect(unbreaker).toBeTruthy(); + }); +}); diff --git a/packages/backend/src/next/index.ts b/packages/backend-next/src/index.ts similarity index 58% rename from packages/backend/src/next/index.ts rename to packages/backend-next/src/index.ts index cda7ae4007..6463dc7ed5 100644 --- a/packages/backend/src/next/index.ts +++ b/packages/backend-next/src/index.ts @@ -18,30 +18,6 @@ import { createBackend } from '@backstage/backend-app-api'; import { catalogPlugin } from '@backstage/plugin-catalog-backend'; import { scaffolderCatalogModule } from '@backstage/plugin-scaffolder-backend'; -// export const appPlugin = createBackendPlugin({ -// id: 'app', -// register(env) { -// env.registerInit({ -// deps: { -// httpRouter: httpRouterServiceRef, -// database: databaseServiceRef, -// logger: loggerServiceRef, -// config: configServiceRef, -// }, -// async init({ httpRouter, config, logger, database }) { -// console.log('App plugin init'); - -// const appBackendRoute = await createAppBackendRouter({ -// appPackageName: 'example-app', -// config, -// logger: loggerToWinstonLogger(logger), -// database, -// }); -// }, -// }); -// }, -// }); - const backend = createBackend({ apis: [], }); diff --git a/packages/backend/package.json b/packages/backend/package.json index 7604f1e609..df6fe5ec3b 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -26,8 +26,6 @@ "build-image": "docker build ../.. -f Dockerfile --tag example-backend" }, "dependencies": { - "@backstage/backend-plugin-api": "^0.0.0", - "@backstage/backend-app-api": "^0.0.0", "@backstage/backend-common": "^0.14.1-next.2", "@backstage/backend-tasks": "^0.3.3-next.2", "@backstage/catalog-client": "^1.0.4-next.1", From f147543044b3008b6e990f2db8a16f99931f1661 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 8 Jul 2022 14:20:21 +0200 Subject: [PATCH 23/31] Update packages/backend-app-api/src/wiring/BackendInitializer.ts Co-authored-by: Phil Kuang Signed-off-by: Patrik Oldsberg --- packages/backend-app-api/src/wiring/BackendInitializer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend-app-api/src/wiring/BackendInitializer.ts b/packages/backend-app-api/src/wiring/BackendInitializer.ts index 6338b60bfb..d33427d54d 100644 --- a/packages/backend-app-api/src/wiring/BackendInitializer.ts +++ b/packages/backend-app-api/src/wiring/BackendInitializer.ts @@ -69,7 +69,7 @@ export class BackendInitializer { extension.register({ registerExtensionPoint: (api, impl) => { if (registerInit) { - throw new Error('registerInitApi called after registerInit'); + throw new Error('registerExtensionPoint called after registerInit'); } if (this.#apis.has(api)) { throw new Error(`API ${api.id} already registered`); From 7f5a1f9b04d42f3edd5a6240f2b58d959ef4e9a2 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 14:48:08 +0200 Subject: [PATCH 24/31] backend-plugin-api: loggerToWinstonLogger refactor Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- packages/backend-app-api/src/index.ts | 1 - .../services/implementations/configService.ts | 2 +- .../services/implementations/loggerService.ts | 28 ++++-------- .../implementations/tokenManagerService.ts | 2 +- .../implementations/urlReaderService.ts | 2 +- packages/backend-plugin-api/package.json | 4 +- packages/backend-plugin-api/src/index.ts | 3 +- .../src/services/helpers/index.ts} | 11 +---- .../services/helpers/loggerToWinstonLogger.ts | 43 +++++++++++++++++++ .../backend-plugin-api/src/services/index.ts | 19 ++++++++ .../src/services/system/index.ts | 25 +++++++++++ plugins/catalog-backend/package.json | 1 - .../src/service/CatalogPlugin.ts | 2 +- 13 files changed, 105 insertions(+), 38 deletions(-) rename packages/{backend-next/src/index.test.ts => backend-plugin-api/src/services/helpers/index.ts} (69%) create mode 100644 packages/backend-plugin-api/src/services/helpers/loggerToWinstonLogger.ts create mode 100644 packages/backend-plugin-api/src/services/index.ts create mode 100644 packages/backend-plugin-api/src/services/system/index.ts diff --git a/packages/backend-app-api/src/index.ts b/packages/backend-app-api/src/index.ts index d75f5c0941..33aca2b291 100644 --- a/packages/backend-app-api/src/index.ts +++ b/packages/backend-app-api/src/index.ts @@ -21,4 +21,3 @@ */ export { createBackend } from './wiring/types'; -export { loggerToWinstonLogger } from './services/implementations/loggerService'; diff --git a/packages/backend-app-api/src/services/implementations/configService.ts b/packages/backend-app-api/src/services/implementations/configService.ts index e9a81ae100..94a59baf34 100644 --- a/packages/backend-app-api/src/services/implementations/configService.ts +++ b/packages/backend-app-api/src/services/implementations/configService.ts @@ -18,9 +18,9 @@ import { loadBackendConfig } from '@backstage/backend-common'; import { configServiceRef, createServiceFactory, + loggerToWinstonLogger, loggerServiceRef, } from '@backstage/backend-plugin-api'; -import { loggerToWinstonLogger } from './loggerService'; export const configFactory = createServiceFactory({ service: configServiceRef, diff --git a/packages/backend-app-api/src/services/implementations/loggerService.ts b/packages/backend-app-api/src/services/implementations/loggerService.ts index ff6af4b643..fbeb4b9ade 100644 --- a/packages/backend-app-api/src/services/implementations/loggerService.ts +++ b/packages/backend-app-api/src/services/implementations/loggerService.ts @@ -23,38 +23,26 @@ import { import { Logger as WinstonLogger } from 'winston'; class BackstageLogger implements Logger { - constructor( - private readonly options: { - winston: WinstonLogger; - }, - ) {} + static fromWinston(logger: WinstonLogger): BackstageLogger { + return new BackstageLogger(logger); + } + + private constructor(private readonly winston: WinstonLogger) {} info(message: string, ...meta: any[]): void { - this.options.winston.info(message, ...meta); + this.winston.info(message, ...meta); } child(fields: { [name: string]: string }): Logger { - return new BackstageLogger({ - winston: this.options.winston.child(fields), - }); + return new BackstageLogger(this.winston.child(fields)); } - - toWinston() { - return this.options.winston; - } -} - -export function loggerToWinstonLogger(logger: Logger): WinstonLogger { - return (logger as BackstageLogger).toWinston(); } export const loggerFactory = createServiceFactory({ service: loggerServiceRef, deps: {}, factory: async () => { - const root = new BackstageLogger({ - winston: createRootLogger(), - }); + const root = BackstageLogger.fromWinston(createRootLogger()); return async (pluginId: string) => { return root.child({ pluginId }); }; diff --git a/packages/backend-app-api/src/services/implementations/tokenManagerService.ts b/packages/backend-app-api/src/services/implementations/tokenManagerService.ts index 7f0423e845..2e82fce9d9 100644 --- a/packages/backend-app-api/src/services/implementations/tokenManagerService.ts +++ b/packages/backend-app-api/src/services/implementations/tokenManagerService.ts @@ -19,9 +19,9 @@ import { loggerServiceRef, createServiceFactory, tokenManagerServiceRef, + loggerToWinstonLogger, } from '@backstage/backend-plugin-api'; import { ServerTokenManager } from '@backstage/backend-common'; -import { loggerToWinstonLogger } from './loggerService'; export const tokenManagerFactory = createServiceFactory({ service: tokenManagerServiceRef, diff --git a/packages/backend-app-api/src/services/implementations/urlReaderService.ts b/packages/backend-app-api/src/services/implementations/urlReaderService.ts index 7df6ca33a9..74431dfa0d 100644 --- a/packages/backend-app-api/src/services/implementations/urlReaderService.ts +++ b/packages/backend-app-api/src/services/implementations/urlReaderService.ts @@ -19,9 +19,9 @@ import { configServiceRef, createServiceFactory, loggerServiceRef, + loggerToWinstonLogger, urlReaderServiceRef, } from '@backstage/backend-plugin-api'; -import { loggerToWinstonLogger } from './loggerService'; export const urlReaderFactory = createServiceFactory({ service: urlReaderServiceRef, diff --git a/packages/backend-plugin-api/package.json b/packages/backend-plugin-api/package.json index 24d2012374..dcf37826df 100644 --- a/packages/backend-plugin-api/package.json +++ b/packages/backend-plugin-api/package.json @@ -38,7 +38,9 @@ "@backstage/backend-common": "^0.14.1-next.2", "@backstage/plugin-permission-common": "^0.6.3-next.0", "@backstage/backend-tasks": "^0.3.3-next.2", - "express": "^4.17.1" + "express": "^4.17.1", + "winston": "^3.2.1", + "winston-transport": "^4.5.0" }, "devDependencies": { "@backstage/cli": "^0.18.0-next.2", diff --git a/packages/backend-plugin-api/src/index.ts b/packages/backend-plugin-api/src/index.ts index e82218ca69..452fa046f9 100644 --- a/packages/backend-plugin-api/src/index.ts +++ b/packages/backend-plugin-api/src/index.ts @@ -20,6 +20,5 @@ * @packageDocumentation */ +export * from './services'; export * from './wiring'; -export * from './services/system/types'; -export * from './services/definitions'; diff --git a/packages/backend-next/src/index.test.ts b/packages/backend-plugin-api/src/services/helpers/index.ts similarity index 69% rename from packages/backend-next/src/index.test.ts rename to packages/backend-plugin-api/src/services/helpers/index.ts index 8b41455b27..24336c1af2 100644 --- a/packages/backend-next/src/index.test.ts +++ b/packages/backend-plugin-api/src/services/helpers/index.ts @@ -1,5 +1,5 @@ /* - * Copyright 2020 The Backstage Authors + * 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. @@ -14,11 +14,4 @@ * limitations under the License. */ -import { PluginEnvironment } from './types'; - -describe('test', () => { - it('unbreaks the test runner', () => { - const unbreaker = {} as PluginEnvironment; - expect(unbreaker).toBeTruthy(); - }); -}); +export { loggerToWinstonLogger } from './loggerToWinstonLogger'; diff --git a/packages/backend-plugin-api/src/services/helpers/loggerToWinstonLogger.ts b/packages/backend-plugin-api/src/services/helpers/loggerToWinstonLogger.ts new file mode 100644 index 0000000000..65f97db745 --- /dev/null +++ b/packages/backend-plugin-api/src/services/helpers/loggerToWinstonLogger.ts @@ -0,0 +1,43 @@ +/* + * 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 { Logger as BackstageLogger } from '../definitions'; +import { Logger as WinstonLogger, createLogger } from 'winston'; +import Transport, { TransportStreamOptions } from 'winston-transport'; + +class BackstageLoggerTransport extends Transport { + constructor( + private readonly backstageLogger: BackstageLogger, + opts?: TransportStreamOptions, + ) { + super(opts); + } + + log(info: { message: string }, callback: VoidFunction) { + // TODO: add support for levels and fields + this.backstageLogger.info(info.message); + callback(); + } +} + +export function loggerToWinstonLogger( + logger: BackstageLogger, + opts?: TransportStreamOptions, +): WinstonLogger { + return createLogger({ + transports: [new BackstageLoggerTransport(logger, opts)], + }); +} diff --git a/packages/backend-plugin-api/src/services/index.ts b/packages/backend-plugin-api/src/services/index.ts new file mode 100644 index 0000000000..96bd3d320a --- /dev/null +++ b/packages/backend-plugin-api/src/services/index.ts @@ -0,0 +1,19 @@ +/* + * 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. + */ + +export * from './definitions'; +export * from './helpers'; +export * from './system'; diff --git a/packages/backend-plugin-api/src/services/system/index.ts b/packages/backend-plugin-api/src/services/system/index.ts new file mode 100644 index 0000000000..eead2297a5 --- /dev/null +++ b/packages/backend-plugin-api/src/services/system/index.ts @@ -0,0 +1,25 @@ +/* + * 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. + */ + +export type { + ServiceRef, + TypesToServiceRef, + DepsToDepFactories, + FactoryFunc, + ServiceFactory, + AnyServiceFactory, +} from './types'; +export { createServiceRef, createServiceFactory } from './types'; diff --git a/plugins/catalog-backend/package.json b/plugins/catalog-backend/package.json index c36e28deb8..6ebcae936b 100644 --- a/plugins/catalog-backend/package.json +++ b/plugins/catalog-backend/package.json @@ -34,7 +34,6 @@ "clean": "backstage-cli package clean" }, "dependencies": { - "@backstage/backend-app-api": "^0.0.0", "@backstage/backend-plugin-api": "^0.0.0", "@backstage/plugin-catalog-node": "^0.0.0", "@backstage/backend-common": "^0.14.1-next.2", diff --git a/plugins/catalog-backend/src/service/CatalogPlugin.ts b/plugins/catalog-backend/src/service/CatalogPlugin.ts index fd3397612f..b82797ec8b 100644 --- a/plugins/catalog-backend/src/service/CatalogPlugin.ts +++ b/plugins/catalog-backend/src/service/CatalogPlugin.ts @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { loggerToWinstonLogger } from '@backstage/backend-app-api'; import { configServiceRef, createBackendPlugin, databaseServiceRef, loggerServiceRef, + loggerToWinstonLogger, permissionsServiceRef, urlReaderServiceRef, httpRouterServiceRef, From 825d769650a2ccb3e6e1288c3571cac2c05bfed0 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 15:25:38 +0200 Subject: [PATCH 25/31] update API report for backend system changes Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- packages/backend-plugin-api/api-report.md | 8 ++++++++ .../src/services/helpers/loggerToWinstonLogger.ts | 1 + plugins/catalog-backend/api-report.md | 10 ---------- plugins/catalog-node/api-report.md | 2 ++ plugins/scaffolder-backend/api-report.md | 4 +--- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/backend-plugin-api/api-report.md b/packages/backend-plugin-api/api-report.md index a4d45a2fb7..df8474d670 100644 --- a/packages/backend-plugin-api/api-report.md +++ b/packages/backend-plugin-api/api-report.md @@ -5,6 +5,7 @@ ```ts import { Config } from '@backstage/config'; import { Handler } from 'express'; +import { Logger as Logger_2 } from 'winston'; import { PermissionAuthorizer } from '@backstage/plugin-permission-common'; import { PermissionEvaluator } from '@backstage/plugin-permission-common'; import { PluginCacheManager } from '@backstage/backend-common'; @@ -12,6 +13,7 @@ import { PluginDatabaseManager } from '@backstage/backend-common'; import { PluginEndpointDiscovery } from '@backstage/backend-common'; import { PluginTaskScheduler } from '@backstage/backend-tasks'; import { TokenManager } from '@backstage/backend-common'; +import { TransportStreamOptions } from 'winston-transport'; import { UrlReader } from '@backstage/backend-common'; // @public (undocumented) @@ -147,6 +149,12 @@ export interface Logger { // @public (undocumented) export const loggerServiceRef: ServiceRef; +// @public (undocumented) +export function loggerToWinstonLogger( + logger: Logger, + opts?: TransportStreamOptions, +): Logger_2; + // @public (undocumented) export const permissionsServiceRef: ServiceRef< PermissionAuthorizer | PermissionEvaluator diff --git a/packages/backend-plugin-api/src/services/helpers/loggerToWinstonLogger.ts b/packages/backend-plugin-api/src/services/helpers/loggerToWinstonLogger.ts index 65f97db745..b9ccd70d3f 100644 --- a/packages/backend-plugin-api/src/services/helpers/loggerToWinstonLogger.ts +++ b/packages/backend-plugin-api/src/services/helpers/loggerToWinstonLogger.ts @@ -33,6 +33,7 @@ class BackstageLoggerTransport extends Transport { } } +/** @public */ export function loggerToWinstonLogger( logger: BackstageLogger, opts?: TransportStreamOptions, diff --git a/plugins/catalog-backend/api-report.md b/plugins/catalog-backend/api-report.md index b7e63ae66d..b5263172cb 100644 --- a/plugins/catalog-backend/api-report.md +++ b/plugins/catalog-backend/api-report.md @@ -47,7 +47,6 @@ import { Readable } from 'stream'; import { ResourcePermission } from '@backstage/plugin-permission-common'; import { Router } from 'express'; import { ScmIntegrationRegistry } from '@backstage/integration'; -import { ServiceRef } from '@backstage/backend-plugin-api'; import { TokenManager } from '@backstage/backend-common'; import { UrlReader } from '@backstage/backend-common'; import { Validators } from '@backstage/catalog-model'; @@ -228,15 +227,6 @@ export interface CatalogProcessingEngine { stop(): Promise; } -// @alpha (undocumented) -export interface CatalogProcessingInitApi { - // (undocumented) - addProcessor(processor: CatalogProcessor): void; -} - -// @alpha (undocumented) -export const catalogProcessingInitApiRef: ServiceRef; - export { CatalogProcessor }; export { CatalogProcessorCache }; diff --git a/plugins/catalog-node/api-report.md b/plugins/catalog-node/api-report.md index cf81f494c5..0449708bc3 100644 --- a/plugins/catalog-node/api-report.md +++ b/plugins/catalog-node/api-report.md @@ -12,6 +12,8 @@ import { ServiceRef } from '@backstage/backend-plugin-api'; // @alpha (undocumented) export interface CatalogProcessingExtensionPoint { + // (undocumented) + addEntityProvider(provider: EntityProvider): void; // (undocumented) addProcessor(processor: CatalogProcessor): void; } diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index 672b853f3b..dfca5994fd 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -545,9 +545,7 @@ export type RunCommandOptions = { }; // @alpha -export const scaffolderCatalogExtension: ( - option: unknown, -) => BackendRegistrable; +export const scaffolderCatalogModule: (option: unknown) => BackendRegistrable; // @public (undocumented) export class ScaffolderEntitiesProcessor implements CatalogProcessor { From fcfea69d83bc3fca4f0169b2f4e412aef9f0e645 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 15:33:26 +0200 Subject: [PATCH 26/31] backend-app-api: fixes, renames, cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Johan Haals Co-authored-by: Fredrik Adelöw Signed-off-by: Patrik Oldsberg --- packages/backend-app-api/README.md | 2 + packages/backend-app-api/api-report.md | 18 +++++-- packages/backend-app-api/src/index.ts | 2 +- .../src/wiring/BackendInitializer.ts | 54 +++++++++---------- .../src/wiring/BackstageBackend.ts | 8 +-- ...{CoreApiRegistry.ts => ServiceRegistry.ts} | 22 ++++---- packages/backend-app-api/src/wiring/index.ts | 18 +++++++ packages/backend-app-api/src/wiring/types.ts | 13 ++++- packages/backend-plugin-api/README.md | 2 + 9 files changed, 88 insertions(+), 51 deletions(-) rename packages/backend-app-api/src/wiring/{CoreApiRegistry.ts => ServiceRegistry.ts} (71%) create mode 100644 packages/backend-app-api/src/wiring/index.ts diff --git a/packages/backend-app-api/README.md b/packages/backend-app-api/README.md index 295a04c7a1..ee608ceb43 100644 --- a/packages/backend-app-api/README.md +++ b/packages/backend-app-api/README.md @@ -1,5 +1,7 @@ # @backstage/backend-app-api +**This package is HIGHLY EXPERIMENTAL, do not use this for production** + This package provides the core API used by Backstage backend apps. ## Installation diff --git a/packages/backend-app-api/api-report.md b/packages/backend-app-api/api-report.md index 8c64858310..a37ec6abc6 100644 --- a/packages/backend-app-api/api-report.md +++ b/packages/backend-app-api/api-report.md @@ -6,10 +6,20 @@ import { AnyServiceFactory } from '@backstage/backend-plugin-api'; import { BackendRegistrable } from '@backstage/backend-plugin-api'; -// Warning: (ae-forgotten-export) The symbol "CreateBackendOptions" needs to be exported by the entry point index.d.ts -// Warning: (ae-forgotten-export) The symbol "Backend" needs to be exported by the entry point index.d.ts -// Warning: (ae-missing-release-tag) "createBackend" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// +// @public (undocumented) +export interface Backend { + // (undocumented) + add(extension: BackendRegistrable): void; + // (undocumented) + start(): Promise; +} + // @public (undocumented) export function createBackend(options?: CreateBackendOptions): Backend; + +// @public (undocumented) +export interface CreateBackendOptions { + // (undocumented) + apis: AnyServiceFactory[]; +} ``` diff --git a/packages/backend-app-api/src/index.ts b/packages/backend-app-api/src/index.ts index 33aca2b291..c58379c3e4 100644 --- a/packages/backend-app-api/src/index.ts +++ b/packages/backend-app-api/src/index.ts @@ -20,4 +20,4 @@ * @packageDocumentation */ -export { createBackend } from './wiring/types'; +export * from './wiring'; diff --git a/packages/backend-app-api/src/wiring/BackendInitializer.ts b/packages/backend-app-api/src/wiring/BackendInitializer.ts index d33427d54d..ab086f4144 100644 --- a/packages/backend-app-api/src/wiring/BackendInitializer.ts +++ b/packages/backend-app-api/src/wiring/BackendInitializer.ts @@ -14,31 +14,38 @@ * limitations under the License. */ -import { BackendRegistrable, ServiceRef } from '@backstage/backend-plugin-api'; -import { BackendRegisterInit, ApiHolder } from './types'; +import { + BackendRegistrable, + ExtensionPoint, + ServiceRef, +} from '@backstage/backend-plugin-api'; +import { BackendRegisterInit, ServiceHolder } from './types'; + +type ServiceOrExtensionPoint = ExtensionPoint | ServiceRef; export class BackendInitializer { #started = false; #extensions = new Map(); - // #stops = []; #registerInits = new Array(); - #apis = new Map, unknown>(); - #apiHolder: ApiHolder; + #extensionPoints = new Map(); + #serviceHolder: ServiceHolder; - constructor(apiHolder: ApiHolder) { - this.#apiHolder = apiHolder; + constructor(serviceHolder: ServiceHolder) { + this.#serviceHolder = serviceHolder; } async #getInitDeps( - deps: { [name: string]: ServiceRef }, + deps: { [name: string]: ServiceOrExtensionPoint }, pluginId: string, ) { return Object.fromEntries( await Promise.all( - Object.entries(deps).map(async ([name, apiRef]) => [ + Object.entries(deps).map(async ([name, ref]) => [ name, - this.#apis.get(apiRef) || - (await this.#apiHolder.get(apiRef)!(pluginId)), + this.#extensionPoints.get(ref) || + (await this.#serviceHolder.get(ref as ServiceRef)!( + pluginId, + )), ]), ), ); @@ -67,15 +74,15 @@ export class BackendInitializer { console.log('Registering', extension.id); extension.register({ - registerExtensionPoint: (api, impl) => { + registerExtensionPoint: (extensionPointRef, impl) => { if (registerInit) { throw new Error('registerExtensionPoint called after registerInit'); } - if (this.#apis.has(api)) { - throw new Error(`API ${api.id} already registered`); + if (this.#extensionPoints.has(extensionPointRef)) { + throw new Error(`API ${extensionPointRef.id} already registered`); } - this.#apis.set(api, impl); - provides.add(api); + this.#extensionPoints.set(extensionPointRef, impl); + provides.add(extensionPointRef); }, registerInit: registerOptions => { if (registerInit) { @@ -105,20 +112,11 @@ export class BackendInitializer { const orderedRegisterResults = this.#resolveInitOrder(this.#registerInits); for (const registerInit of orderedRegisterResults) { - // TODO: DI const deps = await this.#getInitDeps(registerInit.deps, registerInit.id); await registerInit.init(deps); - // Maybe return stop? or lifecycle API - // this.#stops.push(); } } - // async stop(): Promise { - // for (const stop of this.#stops) { - // await stop.stop(); - // } - // } - private validateSetup() {} #resolveInitOrder(registerInits: Array) { @@ -133,13 +131,13 @@ export class BackendInitializer { for (const registerInit of registerInitsToOrder) { const unInitializedDependents = []; - for (const api of registerInit.provides) { + for (const serviceRef of registerInit.provides) { if ( registerInitsToOrder.some( - init => init !== registerInit && init.consumes.has(api), + init => init !== registerInit && init.consumes.has(serviceRef), ) ) { - unInitializedDependents.push(api); + unInitializedDependents.push(serviceRef); } } diff --git a/packages/backend-app-api/src/wiring/BackstageBackend.ts b/packages/backend-app-api/src/wiring/BackstageBackend.ts index 69df1fdf82..104d2b6fc8 100644 --- a/packages/backend-app-api/src/wiring/BackstageBackend.ts +++ b/packages/backend-app-api/src/wiring/BackstageBackend.ts @@ -19,16 +19,16 @@ import { BackendRegistrable, } from '@backstage/backend-plugin-api'; import { BackendInitializer } from './BackendInitializer'; -import { CoreApiRegistry } from './CoreApiRegistry'; +import { ServiceRegistry } from './ServiceRegistry'; import { Backend } from './types'; export class BackstageBackend implements Backend { - #coreApis: CoreApiRegistry; + #services: ServiceRegistry; #initializer: BackendInitializer; constructor(apiFactories: AnyServiceFactory[]) { - this.#coreApis = new CoreApiRegistry(apiFactories); - this.#initializer = new BackendInitializer(this.#coreApis); + this.#services = new ServiceRegistry(apiFactories); + this.#initializer = new BackendInitializer(this.#services); } add(extension: BackendRegistrable): void { diff --git a/packages/backend-app-api/src/wiring/CoreApiRegistry.ts b/packages/backend-app-api/src/wiring/ServiceRegistry.ts similarity index 71% rename from packages/backend-app-api/src/wiring/CoreApiRegistry.ts rename to packages/backend-app-api/src/wiring/ServiceRegistry.ts index 4b1e0f5ce0..bdaecf30c6 100644 --- a/packages/backend-app-api/src/wiring/CoreApiRegistry.ts +++ b/packages/backend-app-api/src/wiring/ServiceRegistry.ts @@ -19,7 +19,7 @@ import { ServiceRef, } from '@backstage/backend-plugin-api'; -export class CoreApiRegistry { +export class ServiceRegistry { readonly #implementations: Map>; readonly #factories: Map; @@ -35,29 +35,27 @@ export class CoreApiRegistry { } return async (pluginId: string): Promise => { - if (this.#implementations.has(ref.id)) { - if (this.#implementations.get(ref.id)!.has(pluginId)) { - return this.#implementations.get(ref.id)!.get(pluginId) as T; + let implementations = this.#implementations.get(ref.id); + if (implementations) { + if (implementations.has(pluginId)) { + return implementations.get(pluginId) as T; } - this.#implementations.set(ref.id, new Map()); } else { - this.#implementations.set(ref.id, new Map()); + implementations = new Map(); + this.#implementations.set(ref.id, implementations); } const factoryDeps = Object.fromEntries( - Object.entries(factory.deps).map(([name, apiRef]) => [ + Object.entries(factory.deps).map(([name, serviceRef]) => [ name, - this.get(apiRef)!, // TODO: throw + this.get(serviceRef)!, // TODO: throw ]), ); const factoryFunc = await factory.factory(factoryDeps); const implementation = await factoryFunc(pluginId); - this.#implementations.set( - ref.id, - this.#implementations.get(ref.id)!.set(pluginId, implementation), - ); + implementations.set(pluginId, implementation); return implementation as T; }; diff --git a/packages/backend-app-api/src/wiring/index.ts b/packages/backend-app-api/src/wiring/index.ts new file mode 100644 index 0000000000..1dd91e30e1 --- /dev/null +++ b/packages/backend-app-api/src/wiring/index.ts @@ -0,0 +1,18 @@ +/* + * 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. + */ + +export type { Backend, CreateBackendOptions } from './types'; +export { createBackend } from './types'; diff --git a/packages/backend-app-api/src/wiring/types.ts b/packages/backend-app-api/src/wiring/types.ts index c1c43a8ba8..112fe06b05 100644 --- a/packages/backend-app-api/src/wiring/types.ts +++ b/packages/backend-app-api/src/wiring/types.ts @@ -23,6 +23,9 @@ import { import { defaultServiceFactories } from '../services/implementations'; import { BackstageBackend } from './BackstageBackend'; +/** + * @public + */ export interface Backend { add(extension: BackendRegistrable): void; start(): Promise; @@ -36,14 +39,20 @@ export interface BackendRegisterInit { init: (deps: { [name: string]: unknown }) => Promise; } -interface CreateBackendOptions { +/** + * @public + */ +export interface CreateBackendOptions { apis: AnyServiceFactory[]; } -export type ApiHolder = { +export type ServiceHolder = { get(api: ServiceRef): FactoryFunc | undefined; }; +/** + * @public + */ export function createBackend(options?: CreateBackendOptions): Backend { // TODO: merge with provided APIs return new BackstageBackend([ diff --git a/packages/backend-plugin-api/README.md b/packages/backend-plugin-api/README.md index 9e28e220a1..364655dd60 100644 --- a/packages/backend-plugin-api/README.md +++ b/packages/backend-plugin-api/README.md @@ -1,5 +1,7 @@ # @backstage/backend-plugin-api +**This package is HIGHLY EXPERIMENTAL, do not use this for production** + This package provides the core API used by Backstage backend plugins. ## Installation From f710231475102bf8bdd18586cfd79c1b1622670d Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 15:41:18 +0200 Subject: [PATCH 27/31] backend-plugin-api: add TODOs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- packages/backend-plugin-api/src/wiring/types.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/backend-plugin-api/src/wiring/types.ts b/packages/backend-plugin-api/src/wiring/types.ts index 2b4aacb40a..2b0bef16f7 100644 --- a/packages/backend-plugin-api/src/wiring/types.ts +++ b/packages/backend-plugin-api/src/wiring/types.ts @@ -75,6 +75,7 @@ export interface BackendPluginConfig { register(reg: BackendInitRegistry, options: TOptions): void; } +// TODO: Make option optional in the returned factory if they are indeed optional /** @public */ export function createBackendPlugin( config: BackendPluginConfig, @@ -97,6 +98,7 @@ export interface BackendModuleConfig { ): void; } +// TODO: Make option optional in the returned factory if they are indeed optional /** @public */ export function createBackendModule( config: BackendModuleConfig, @@ -104,6 +106,7 @@ export function createBackendModule( return options => ({ id: `${config.pluginId}.${config.moduleId}`, register(register) { + // TODO: Hide registerExtensionPoint return config.register(register, options); }, }); From ca32d532354568e384fac6b1170a141bc17176dc Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 15:42:43 +0200 Subject: [PATCH 28/31] backend-next: set version to 0.0.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Johan Haals Co-authored-by: Fredrik Adelöw Signed-off-by: Patrik Oldsberg --- packages/backend-next/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend-next/package.json b/packages/backend-next/package.json index 6feaf5ad97..673116790e 100644 --- a/packages/backend-next/package.json +++ b/packages/backend-next/package.json @@ -1,6 +1,6 @@ { "name": "example-backend-next", - "version": "0.2.73-next.2", + "version": "0.0.0", "main": "dist/index.cjs.js", "types": "src/index.ts", "license": "Apache-2.0", From b4e127d126bb76ec7aee19194f0bacb4fd462bfe Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 15:43:15 +0200 Subject: [PATCH 29/31] backend-plugin-api: no schelduler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: blam Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- .../definitions/scheldulerServiceRef.ts | 30 ------------------- 1 file changed, 30 deletions(-) delete mode 100644 packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts diff --git a/packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts b/packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts deleted file mode 100644 index b13ca9e240..0000000000 --- a/packages/backend-plugin-api/src/services/definitions/scheldulerServiceRef.ts +++ /dev/null @@ -1,30 +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 { createServiceRef } from '../system/types'; -import { - PermissionAuthorizer, - PermissionEvaluator, -} from '@backstage/plugin-permission-common'; - -/** - * @public - */ -export const permissionsServiceRef = createServiceRef< - PermissionEvaluator | PermissionAuthorizer ->({ - id: 'core.permissions', -}); From 91c1d12123b55f09c494f0c192b7f087d450b84d Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 15:46:16 +0200 Subject: [PATCH 30/31] changesets: added changesets for initial experimental backend system Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- .changeset/beige-kiwis-know.md | 6 ++++++ .changeset/lazy-steaks-tell.md | 5 +++++ .changeset/polite-moose-beam.md | 5 +++++ .changeset/short-wolves-applaud.md | 5 +++++ .changeset/swift-plants-fix.md | 7 +++++++ 5 files changed, 28 insertions(+) create mode 100644 .changeset/beige-kiwis-know.md create mode 100644 .changeset/lazy-steaks-tell.md create mode 100644 .changeset/polite-moose-beam.md create mode 100644 .changeset/short-wolves-applaud.md create mode 100644 .changeset/swift-plants-fix.md diff --git a/.changeset/beige-kiwis-know.md b/.changeset/beige-kiwis-know.md new file mode 100644 index 0000000000..696b9e81cb --- /dev/null +++ b/.changeset/beige-kiwis-know.md @@ -0,0 +1,6 @@ +--- +'@backstage/backend-plugin-api': minor +--- + +Introduced new package for creating backend plugins using the new alpha backend plugin framework. +This package is still considered **EXPERIMENTAL** and things will change without warning. Do not use this for production. diff --git a/.changeset/lazy-steaks-tell.md b/.changeset/lazy-steaks-tell.md new file mode 100644 index 0000000000..5e63816ada --- /dev/null +++ b/.changeset/lazy-steaks-tell.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-node': minor +--- + +Added alpha exports for the new experimental backend system. diff --git a/.changeset/polite-moose-beam.md b/.changeset/polite-moose-beam.md new file mode 100644 index 0000000000..5e41018ae6 --- /dev/null +++ b/.changeset/polite-moose-beam.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': minor +--- + +Export experimental `catalogPlugin` for the new backend system. This export is not considered stable and should not be used in production. diff --git a/.changeset/short-wolves-applaud.md b/.changeset/short-wolves-applaud.md new file mode 100644 index 0000000000..fa5cebffc3 --- /dev/null +++ b/.changeset/short-wolves-applaud.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': minor +--- + +Export experimental `scaffolderCatalogExtension` for the new backend system. This export is not considered stable and should not be used in production. diff --git a/.changeset/swift-plants-fix.md b/.changeset/swift-plants-fix.md new file mode 100644 index 0000000000..52873b7985 --- /dev/null +++ b/.changeset/swift-plants-fix.md @@ -0,0 +1,7 @@ +--- +'@backstage/backend-app-api': minor +--- + +Add initial plumbing for creating backends using the experimental backend framework. + +This package is highly **EXPERIMENTAL** and should not be used in production. From 79849709d5e4b91e79300f093a3dfd3b7c87c427 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Jul 2022 16:01:51 +0200 Subject: [PATCH 31/31] backend-plugin-api: fix @types/express dep Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- packages/backend-plugin-api/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/backend-plugin-api/package.json b/packages/backend-plugin-api/package.json index dcf37826df..f9109ae680 100644 --- a/packages/backend-plugin-api/package.json +++ b/packages/backend-plugin-api/package.json @@ -38,13 +38,13 @@ "@backstage/backend-common": "^0.14.1-next.2", "@backstage/plugin-permission-common": "^0.6.3-next.0", "@backstage/backend-tasks": "^0.3.3-next.2", + "@types/express": "^4.17.6", "express": "^4.17.1", "winston": "^3.2.1", "winston-transport": "^4.5.0" }, "devDependencies": { - "@backstage/cli": "^0.18.0-next.2", - "@types/express": "^4.17.6" + "@backstage/cli": "^0.18.0-next.2" }, "files": [ "dist",