Refactored createRouter args

Signed-off-by: Andre Wanlin <andrewanlin@gmail.com>
This commit is contained in:
Andre Wanlin
2023-02-08 17:08:51 -06:00
parent bb1cfedc33
commit d327896ecb
6 changed files with 64 additions and 64 deletions
+1 -10
View File
@@ -28,14 +28,5 @@ export default async function createPlugin(
initialDelay: { seconds: 15 },
};
return createRouter({
logger: env.logger,
config: env.config,
reader: env.reader,
discovery: env.discovery,
database: env.database,
scheduler: env.scheduler,
schedule: schedule,
age: { days: 30 },
});
return createRouter({ schedule: schedule, age: { days: 30 } }, { ...env });
}
+4 -21
View File
@@ -36,15 +36,7 @@ Here's how to get the backend up and running:
initialDelay: { seconds: 90 },
};
return createRouter({
logger: env.logger,
config: env.config,
reader: env.reader,
discovery: env.discovery,
database: env.database,
scheduler: env.scheduler,
schedule: schedule,
});
return createRouter({ schedule: schedule }, { ...env });
}
```
@@ -79,19 +71,10 @@ const schedule: TaskScheduleDefinition = {
};
```
The default setup will only generate the language breakdown for entities with the linguist annotation that have not been generated yet. If you want this process to also refresh the data you can do so by adding the `age` in your `packages/backend/src/plugins/linguist.ts`:
The default setup will only generate the language breakdown for entities with the linguist annotation that have not been generated yet. If you want this process to also refresh the data you can do so by adding the `age` (as a `HumanDuration`) in your `packages/backend/src/plugins/linguist.ts` when you call `createRouter`:
```diff
return createRouter({
logger: env.logger,
config: env.config,
reader: env.reader,
discovery: env.discovery,
database: env.database,
scheduler: env.scheduler,
schedule: schedule,
+ age: { days: 15 },
});
```ts
return createRouter({ schedule: schedule, age: { days: 30 } }, { ...env });
```
With the `age` setup like this if the language breakdown is older than 15 days it will get regenerated. It's recommended that if you choose to use this configuration to set it to a large value - 30, 90, or 180 - as this data generally does not change drastically.
+11 -4
View File
@@ -19,7 +19,10 @@ import { TaskScheduleDefinition } from '@backstage/backend-tasks';
import { UrlReader } from '@backstage/backend-common';
// @public (undocumented)
export function createRouter(options: RouterOptions): Promise<express.Router>;
export function createRouter(
pluginOptions: PluginOptions,
routerOptions: RouterOptions,
): Promise<express.Router>;
// @public (undocumented)
export class LinguistBackendApi {
@@ -65,9 +68,15 @@ export interface LinguistBackendStore {
}
// @public (undocumented)
export interface RouterOptions {
export interface PluginOptions {
// (undocumented)
age?: HumanDuration;
// (undocumented)
schedule?: TaskScheduleDefinition;
}
// @public (undocumented)
export interface RouterOptions {
// (undocumented)
config: Config;
// (undocumented)
@@ -81,8 +90,6 @@ export interface RouterOptions {
// (undocumented)
reader: UrlReader;
// (undocumented)
schedule?: TaskScheduleDefinition;
// (undocumented)
scheduler?: PluginTaskScheduler;
}
@@ -26,6 +26,7 @@ import request from 'supertest';
import { ConfigReader } from '@backstage/config';
import { createRouter } from './router';
import { LinguistBackendApi } from '../api';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
function createDatabase(): PluginDatabaseManager {
return DatabaseManager.fromConfig(
@@ -52,19 +53,28 @@ const mockUrlReader = UrlReaders.default({
config: new ConfigReader({}),
});
const schedule: TaskScheduleDefinition = {
frequency: { minutes: 2 },
timeout: { minutes: 15 },
initialDelay: { seconds: 15 },
};
describe('createRouter', () => {
let linguistBackendApi: jest.Mocked<LinguistBackendApi>;
let app: express.Express;
beforeAll(async () => {
const router = await createRouter({
linguistBackendApi,
config: new ConfigReader({}),
discovery: testDiscovery,
database: createDatabase(),
reader: mockUrlReader,
logger: getVoidLogger(),
});
const router = await createRouter(
{ schedule: schedule, age: { days: 30 } },
{
linguistBackendApi,
config: new ConfigReader({}),
discovery: testDiscovery,
database: createDatabase(),
reader: mockUrlReader,
logger: getVoidLogger(),
},
);
app = express().use(router);
});
+13 -14
View File
@@ -32,6 +32,12 @@ import {
} from '@backstage/backend-tasks';
import { HumanDuration } from '@backstage/types';
/** @public */
export interface PluginOptions {
schedule?: TaskScheduleDefinition;
age?: HumanDuration;
}
/** @public */
export interface RouterOptions {
linguistBackendApi?: LinguistBackendApi;
@@ -41,31 +47,24 @@ export interface RouterOptions {
database: PluginDatabaseManager;
discovery: PluginEndpointDiscovery;
scheduler?: PluginTaskScheduler;
schedule?: TaskScheduleDefinition;
age?: HumanDuration;
}
/** @public */
export async function createRouter(
options: RouterOptions,
pluginOptions: PluginOptions,
routerOptions: RouterOptions,
): Promise<express.Router> {
const {
config,
logger,
reader,
database,
discovery,
scheduler,
schedule,
age,
} = options;
const { schedule, age } = pluginOptions;
const { config, logger, reader, database, discovery, scheduler } =
routerOptions;
const linguistBackendStore = await LinguistBackendDatabase.create(
await database.getClient(),
);
const linguistBackendApi =
options.linguistBackendApi ||
routerOptions.linguistBackendApi ||
new LinguistBackendApi(
config,
logger,
@@ -25,6 +25,7 @@ import { Server } from 'http';
import { Logger } from 'winston';
import { createRouter } from './router';
import knexFactory from 'knex';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
export interface ServerOptions {
port: number;
@@ -51,14 +52,23 @@ export async function startStandaloneServer(
return knex;
});
const schedule: TaskScheduleDefinition = {
frequency: { minutes: 2 },
timeout: { minutes: 15 },
initialDelay: { seconds: 15 },
};
logger.debug('Starting application server...');
const router = await createRouter({
database: { getClient: async () => db },
config,
discovery: SingleHostDiscovery.fromConfig(config),
reader: UrlReaders.default({ logger, config }),
logger,
});
const router = await createRouter(
{ schedule: schedule, age: { days: 30 } },
{
database: { getClient: async () => db },
config,
discovery: SingleHostDiscovery.fromConfig(config),
reader: UrlReaders.default({ logger, config }),
logger,
},
);
let service = createServiceBuilder(module)
.setPort(options.port)