app-backend: initial port to new backend system
Co-authored-by: Fredrik Adelöw <freben@gmail.com> Co-authored-by: Johan Haals <johan.haals@gmail.com> Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -21,3 +21,5 @@
|
||||
*/
|
||||
|
||||
export * from './service/router';
|
||||
export { appPlugin } from './service/appPlugin';
|
||||
export type { AppPluginOptions } from './service/appPlugin';
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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 mockFs from 'mock-fs';
|
||||
import { resolve as resolvePath } from 'path';
|
||||
import fetch from 'node-fetch';
|
||||
import { configServiceRef } from '@backstage/backend-plugin-api';
|
||||
import { startTestBackend } from '@backstage/backend-test-utils';
|
||||
import { appPlugin } from './appPlugin';
|
||||
import {
|
||||
databaseFactory,
|
||||
httpRouterFactory,
|
||||
loggerFactory,
|
||||
rootLoggerFactory,
|
||||
} from '@backstage/backend-app-api';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import getPort from 'get-port';
|
||||
|
||||
describe('appPlugin', () => {
|
||||
beforeAll(() => {
|
||||
mockFs({
|
||||
[resolvePath(process.cwd(), 'node_modules/app')]: {
|
||||
'package.json': '{}',
|
||||
dist: {
|
||||
static: {},
|
||||
'index.html': 'winning',
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
mockFs.restore();
|
||||
});
|
||||
|
||||
it('boots', async () => {
|
||||
const port = await getPort();
|
||||
await startTestBackend({
|
||||
services: [
|
||||
[
|
||||
configServiceRef,
|
||||
new ConfigReader({
|
||||
backend: {
|
||||
listen: { port },
|
||||
database: { client: 'better-sqlite3', connection: ':memory:' },
|
||||
},
|
||||
}),
|
||||
],
|
||||
loggerFactory(),
|
||||
rootLoggerFactory(),
|
||||
databaseFactory(),
|
||||
httpRouterFactory(),
|
||||
],
|
||||
features: [
|
||||
appPlugin({
|
||||
appPackageName: 'app',
|
||||
disableStaticFallbackCache: true,
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
await expect(
|
||||
fetch(`http://localhost:${port}/api/app/derp.html`).then(res =>
|
||||
res.text(),
|
||||
),
|
||||
).resolves.toBe('winning');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* 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 from 'express';
|
||||
import {
|
||||
configServiceRef,
|
||||
createBackendPlugin,
|
||||
databaseServiceRef,
|
||||
loggerServiceRef,
|
||||
loggerToWinstonLogger,
|
||||
httpRouterServiceRef,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { createRouter } from './router';
|
||||
|
||||
/** @alpha */
|
||||
export type AppPluginOptions = {
|
||||
/**
|
||||
* The name of the app package (in most Backstage repositories, this is the
|
||||
* "name" field in `packages/app/package.json`) that content should be served
|
||||
* from. The same app package should be added as a dependency to the backend
|
||||
* package in order for it to be accessible at runtime.
|
||||
*
|
||||
* In a typical setup with a single app package, this would be set to 'app'.
|
||||
*/
|
||||
appPackageName: string;
|
||||
|
||||
/**
|
||||
* A request handler to handle requests for static content that are not present in the app bundle.
|
||||
*
|
||||
* This can be used to avoid issues with clients on older deployment versions trying to access lazy
|
||||
* loaded content that is no longer present. Typically the requests would fall back to a long-term
|
||||
* object store where all recently deployed versions of the app are present.
|
||||
*
|
||||
* Another option is to provide a `database` that will take care of storing the static assets instead.
|
||||
*
|
||||
* If both `database` and `staticFallbackHandler` are provided, the `database` will attempt to serve
|
||||
* static assets first, and if they are not found, the `staticFallbackHandler` will be called.
|
||||
*/
|
||||
staticFallbackHandler?: express.Handler;
|
||||
|
||||
/**
|
||||
* Disables the configuration injection. This can be useful if you're running in an environment
|
||||
* with a read-only filesystem, or for some other reason don't want configuration to be injected.
|
||||
*
|
||||
* Note that this will cause the configuration used when building the app bundle to be used, unless
|
||||
* a separate configuration loading strategy is set up.
|
||||
*
|
||||
* This also disables configuration injection though `APP_CONFIG_` environment variables.
|
||||
*/
|
||||
disableConfigInjection?: boolean;
|
||||
|
||||
/**
|
||||
* By default the app backend plugin will cache previously deployed static assets in the database.
|
||||
* If you disable this, it is recommended to set a `staticFallbackHandler` instead.
|
||||
*/
|
||||
disableStaticFallbackCache?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* The App plugin is responsible for serving the frontend app bundle and static assets.
|
||||
* @alpha
|
||||
*/
|
||||
export const appPlugin = createBackendPlugin({
|
||||
id: 'app',
|
||||
register(env, options: AppPluginOptions) {
|
||||
env.registerInit({
|
||||
deps: {
|
||||
logger: loggerServiceRef,
|
||||
config: configServiceRef,
|
||||
database: databaseServiceRef,
|
||||
httpRouter: httpRouterServiceRef,
|
||||
},
|
||||
async init({ logger, config, database, httpRouter }) {
|
||||
const {
|
||||
appPackageName,
|
||||
staticFallbackHandler,
|
||||
disableConfigInjection,
|
||||
disableStaticFallbackCache,
|
||||
} = options;
|
||||
const winstonLogger = loggerToWinstonLogger(logger);
|
||||
|
||||
const router = await createRouter({
|
||||
logger: winstonLogger,
|
||||
config,
|
||||
database: disableStaticFallbackCache ? undefined : database,
|
||||
appPackageName,
|
||||
staticFallbackHandler,
|
||||
disableConfigInjection,
|
||||
});
|
||||
httpRouter.use(router);
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user