Add more service implementations

Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>
Co-authored-by: blam <ben@blam.sh>
Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-06-15 15:44:10 +02:00
committed by Patrik Oldsberg
parent 39b24c78fe
commit 375a403664
21 changed files with 325 additions and 22 deletions
+3 -1
View File
@@ -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"
@@ -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;
},
});
@@ -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 });
};
},
});
@@ -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),
});
};
},
});
@@ -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),
@@ -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';
@@ -15,8 +15,8 @@
*/
import {
AnyServiceFactory,
ServiceRef,
FactoryFunc,
ServiceRef,
} from '@backstage/backend-plugin-api';
export class CoreApiRegistry {
+6 -3
View File
@@ -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 ?? [])]);
}
+2 -1
View File
@@ -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"
@@ -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<CacheManager>({
id: 'core.cacheManager',
});
@@ -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<PluginCacheManager>({
id: 'core.cache',
});
@@ -17,6 +17,6 @@
import { Config } from '@backstage/config';
import { createServiceRef } from '../system/types';
export const configApiRef = createServiceRef<Config>({
export const configServiceRef = createServiceRef<Config>({
id: 'core.config',
});
@@ -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<PluginDatabaseManager>({
id: 'core.database',
});
@@ -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<PluginEndpointDiscovery>({
id: 'core.discovery',
});
@@ -20,6 +20,6 @@ export interface HttpRouterApi {
get(path: string): void;
}
export const httpRouterApiRef = createServiceRef<HttpRouterApi>({
export const httpRouterServiceRef = createServiceRef<HttpRouterApi>({
id: 'core.httpRouter',
});
@@ -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';
@@ -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<Logger>({
export const loggerServiceRef = createServiceRef<Logger>({
id: 'core.logger',
});
@@ -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<TokenManager>({
id: 'core.tokenManager',
});
@@ -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<UrlReader>({
id: 'core.urlReader',
});
@@ -68,3 +68,11 @@ export function createServiceRef<T>(options: { id: string }): ServiceRef<T> {
$$ref: 'service', // TODO: declare
};
}
export function createServiceFactory<
Api,
Impl extends Api,
Deps extends { [name in string]: unknown },
>(factory: ServiceFactory<Api, Impl, Deps>): ServiceFactory<Api, Impl, Deps> {
return factory;
}
+15 -4
View File
@@ -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({}));