app-backend: some refactoring, renaming, and docs

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-12-29 17:37:53 +01:00
parent fb08e2f285
commit ccd9bcb86e
7 changed files with 31 additions and 12 deletions
@@ -19,7 +19,7 @@ import { Knex } from 'knex';
import { Logger } from 'winston';
import { DateTime } from 'luxon';
import partition from 'lodash/partition';
import { StaticAsset, StaticAssetInput } from './types';
import { StaticAsset, StaticAssetInput, StaticAssetProvider } from './types';
const migrationsDir = resolvePackagePath(
'@backstage/plugin-app-backend',
@@ -43,7 +43,7 @@ export interface StaticAssetsStoreOptions {
*
* @internal
*/
export class StaticAssetsStore {
export class StaticAssetsStore implements StaticAssetProvider {
#db: Knex;
#logger: Logger;
@@ -16,16 +16,16 @@
import express from 'express';
import request from 'supertest';
import { createStaticAssetsStoreMiddleware } from './createStaticAssetsStoreMiddleware';
import { createStaticAssetMiddleware } from './createStaticAssetMiddleware';
import { StaticAssetsStore } from './StaticAssetsStore';
const mockStore = {
getAsset: jest.fn(),
} as unknown as jest.Mocked<StaticAssetsStore>;
describe('createStaticAssetsStoreMiddleware', () => {
describe('createStaticAssetMiddleware', () => {
const app = express();
app.use(createStaticAssetsStoreMiddleware(mockStore));
app.use(createStaticAssetMiddleware(mockStore));
app.use((_req, res) => {
res.status(404).end('Not Found');
});
@@ -16,11 +16,16 @@
import { extname } from 'path';
import { RequestHandler } from 'express';
import { StaticAssetsStore } from './StaticAssetsStore';
import { StaticAssetProvider } from './types';
import { CACHE_CONTROL_MAX_CACHE } from '../headers';
export function createStaticAssetsStoreMiddleware(
store: StaticAssetsStore,
/**
* Creates a middleware that serves static assets from a static asset provider
*
* @internal
*/
export function createStaticAssetMiddleware(
store: StaticAssetProvider,
): RequestHandler {
return (req, res, next) => {
if (req.method !== 'GET' && req.method !== 'HEAD') {
@@ -28,6 +33,7 @@ export function createStaticAssetsStoreMiddleware(
return;
}
// Let's not assume we're in promise-router
Promise.resolve(
(async () => {
// Drop leading slashes from the incoming path
@@ -19,6 +19,11 @@ import globby from 'globby';
import { StaticAssetInput } from './types';
import { resolveSafeChildPath } from '@backstage/backend-common';
/**
* Finds all static assets within a directory
*
* @internal
*/
export async function findStaticAssets(
staticDir: string,
): Promise<StaticAssetInput[]> {
+6 -2
View File
@@ -15,6 +15,10 @@
*/
export { StaticAssetsStore } from './StaticAssetsStore';
export type { StaticAsset, StaticAssetInput } from './types';
export type {
StaticAsset,
StaticAssetInput,
StaticAssetProvider,
} from './types';
export { findStaticAssets } from './findStaticAssets';
export { createStaticAssetsStoreMiddleware } from './createStaticAssetsStoreMiddleware';
export { createStaticAssetMiddleware } from './createStaticAssetMiddleware';
@@ -24,3 +24,7 @@ export interface StaticAsset {
content: Buffer;
lastModifiedAt: Date;
}
export interface StaticAssetProvider {
getAsset(path: string): Promise<StaticAsset | undefined>;
}
+2 -2
View File
@@ -30,7 +30,7 @@ import { injectConfig, readConfigs } from '../lib/config';
import {
StaticAssetsStore,
findStaticAssets,
createStaticAssetsStoreMiddleware,
createStaticAssetMiddleware,
} from '../lib/assets';
import {
CACHE_CONTROL_MAX_CACHE,
@@ -132,7 +132,7 @@ export async function createRouter(
// Remove any assets that are older than 7 days
await store.trimAssets({ maxAgeSeconds: 60 * 60 * 24 * 7 });
staticRouter.use(createStaticAssetsStoreMiddleware(store));
staticRouter.use(createStaticAssetMiddleware(store));
}
if (staticFallbackHandler) {