Skip to content

Commit

Permalink
feat: add support for cdnPrefix for static assets (#1191)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarconr committed Jan 6, 2022
1 parent 2b59a42 commit 26b7da8
Show file tree
Hide file tree
Showing 27 changed files with 147 additions and 121 deletions.
1 change: 1 addition & 0 deletions src/lib/__snapshots__/create-config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Object {
"secureHeaders": false,
"server": Object {
"baseUriPath": "",
"cdnPrefix": undefined,
"enableRequestLogger": false,
"gracefulShutdownEnable": true,
"gracefulShutdownTimeout": 1000,
Expand Down
14 changes: 7 additions & 7 deletions src/lib/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ jest.mock(
},
);

const getApp = require('./app');
const getApp = require('./app').default;

test('should not throw when valid config', () => {
test('should not throw when valid config', async () => {
const config = createTestConfig();
const app = getApp(config, {}, {});
const app = await getApp(config, {}, {});
expect(typeof app.listen).toBe('function');
});

test('should call preHook', () => {
test('should call preHook', async () => {
let called = 0;
const config = createTestConfig({
preHook: () => {
called++;
},
});
getApp(config, {}, {});
await getApp(config, {}, {});
expect(called).toBe(1);
});

test('should call preRouterHook', () => {
test('should call preRouterHook', async () => {
let called = 0;
const config = createTestConfig({
preRouterHook: () => {
called++;
},
});
getApp(config, {}, {});
await getApp(config, {}, {});
expect(called).toBe(1);
});
16 changes: 5 additions & 11 deletions src/lib/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { publicFolder } from 'unleash-frontend';
import fs from 'fs';
import express, { Application, RequestHandler } from 'express';
import cors from 'cors';
import compression from 'compression';
Expand All @@ -22,23 +21,19 @@ import ossAuthentication from './middleware/oss-authentication';
import noAuthentication from './middleware/no-authentication';
import secureHeaders from './middleware/secure-headers';

import { rewriteHTML } from './util/rewriteHTML';
import { loadIndexHTML } from './util/load-index-html';

export default function getApp(
export default async function getApp(
config: IUnleashConfig,
stores: IUnleashStores,
services: IUnleashServices,
unleashSession?: RequestHandler,
): Application {
): Promise<Application> {
const app = express();

const baseUriPath = config.server.baseUriPath || '';

let indexHTML = fs
.readFileSync(path.join(publicFolder, 'index.html'))
.toString();

indexHTML = rewriteHTML(indexHTML, baseUriPath);
let indexHTML = await loadIndexHTML(config, publicFolder);

app.set('trust proxy', true);
app.disable('x-powered-by');
Expand Down Expand Up @@ -68,7 +63,7 @@ export default function getApp(
app.use(secureHeaders(config));
app.use(express.urlencoded({ extended: true }));
app.use(favicon(path.join(publicFolder, 'favicon.ico')));

app.use(baseUriPath, favicon(path.join(publicFolder, 'favicon.ico')));
app.use(baseUriPath, express.static(publicFolder, { index: false }));

if (config.enableOAS) {
Expand Down Expand Up @@ -151,4 +146,3 @@ export default function getApp(
});
return app;
}
module.exports = getApp;
1 change: 1 addition & 0 deletions src/lib/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const defaultServerOption: IServerOption = {
host: process.env.HTTP_HOST,
port: safeNumber(process.env.HTTP_PORT || process.env.PORT, 4242),
baseUriPath: formatBaseUri(process.env.BASE_URI_PATH),
cdnPrefix: process.env.CDN_PREFIX,
unleashUrl: process.env.UNLEASH_URL || 'http://localhost:4242',
serverMetrics: true,
keepAliveTimeout: minutesToMilliseconds(1),
Expand Down
12 changes: 6 additions & 6 deletions src/lib/middleware/oss-authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getApp from '../app';
import User from '../types/user';
import sessionDb from './session-db';

function getSetup(preRouterHook) {
async function getSetup(preRouterHook) {
const base = `/random${Math.round(Math.random() * 1000)}`;
const config = createTestConfig({
server: { baseUriPath: base },
Expand All @@ -23,25 +23,25 @@ function getSetup(preRouterHook) {
const stores = createStores();
const services = createServices(stores, config);
const unleashSession = sessionDb(config, undefined);
const app = getApp(config, stores, services, unleashSession);
const app = await getApp(config, stores, services, unleashSession);

return {
base,
request: supertest(app),
};
}

test('should return 401 when missing user', () => {
test('should return 401 when missing user', async () => {
expect.assertions(0);
const { base, request } = getSetup(() => {});
const { base, request } = await getSetup(() => {});

return request.get(`${base}/api/protectedResource`).expect(401);
});

test('should return 200 when user exists', () => {
test('should return 200 when user exists', async () => {
expect.assertions(0);
const user = new User({ id: 1, email: 'some@mail.com' });
const { base, request } = getSetup((app) =>
const { base, request } = await getSetup((app) =>
app.use((req, res, next) => {
req.user = user;
next();
Expand Down
8 changes: 4 additions & 4 deletions src/lib/routes/admin-api/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const uiConfig = {
slogan: 'hello',
};

function getSetup() {
async function getSetup() {
const base = `/random${Math.round(Math.random() * 1000)}`;
const config = createTestConfig({
server: { baseUriPath: base },
Expand All @@ -19,7 +19,7 @@ function getSetup() {
const stores = createStores();
const services = createServices(stores, config);

const app = getApp(config, stores, services);
const app = await getApp(config, stores, services);

return {
base,
Expand All @@ -36,8 +36,8 @@ let request;
let base;
let destroy;

beforeEach(() => {
const setup = getSetup();
beforeEach(async () => {
const setup = await getSetup();
request = setup.request;
base = setup.base;
destroy = setup.destroy;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/routes/admin-api/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createServices } from '../../services';
import permissions from '../../../test/fixtures/permissions';
import getApp from '../../app';

function getSetup() {
async function getSetup() {
const base = `/random${Math.round(Math.random() * 1000)}`;
const perms = permissions();
const config = createTestConfig({
Expand All @@ -15,7 +15,7 @@ function getSetup() {
const stores = createStores();

const services = createServices(stores, config);
const app = getApp(config, stores, services);
const app = await getApp(config, stores, services);

return {
base,
Expand All @@ -32,8 +32,8 @@ let base;
let request;
let destroy;

beforeEach(() => {
const setup = getSetup();
beforeEach(async () => {
const setup = await getSetup();
base = setup.base;
request = setup.request;
destroy = setup.destroy;
Expand Down
16 changes: 8 additions & 8 deletions src/lib/routes/admin-api/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createServices } from '../../services';
import permissions from '../../../test/fixtures/permissions';
import getApp from '../../app';

function getSetup() {
async function getSetup() {
const base = `/random${Math.round(Math.random() * 1000)}`;
const stores = createStores();
const perms = permissions();
Expand All @@ -15,17 +15,17 @@ function getSetup() {
});

const services = createServices(stores, config);
const app = getApp(config, stores, services);
const app = await getApp(config, stores, services);

return {
base,
request: supertest(app),
};
}

test('should render html preview of template', () => {
test('should render html preview of template', async () => {
expect.assertions(0);
const { request, base } = getSetup();
const { request, base } = await getSetup();
return request
.get(
`${base}/api/admin/email/preview/html/reset-password?name=Test%20Test`,
Expand All @@ -35,9 +35,9 @@ test('should render html preview of template', () => {
.expect((res) => 'Test Test' in res.body);
});

test('should render text preview of template', () => {
test('should render text preview of template', async () => {
expect.assertions(0);
const { request, base } = getSetup();
const { request, base } = await getSetup();
return request
.get(
`${base}/api/admin/email/preview/text/reset-password?name=Test%20Test`,
Expand All @@ -47,9 +47,9 @@ test('should render text preview of template', () => {
.expect((res) => 'Test Test' in res.body);
});

test('Requesting a non-existing template should yield 404', () => {
test('Requesting a non-existing template should yield 404', async () => {
expect.assertions(0);
const { request, base } = getSetup();
const { request, base } = await getSetup();
return request
.get(`${base}/api/admin/email/preview/text/some-non-existing-template`)
.expect(404);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/routes/admin-api/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import createStores from '../../../test/fixtures/store';

import getApp from '../../app';

function getSetup() {
async function getSetup() {
const base = `/random${Math.round(Math.random() * 1000)}`;
const stores = createStores();
const config = createTestConfig({
server: { baseUriPath: base },
});
const services = createServices(stores, config);
const app = getApp(config, stores, services);
const app = await getApp(config, stores, services);

return { base, eventStore: stores.eventStore, request: supertest(app) };
}

test('should get empty events list via admin', () => {
test('should get empty events list via admin', async () => {
expect.assertions(1);
const { request, base } = getSetup();
const { request, base } = await getSetup();
return request
.get(`${base}/api/admin/events`)
.expect('Content-Type', /json/)
Expand Down
8 changes: 4 additions & 4 deletions src/lib/routes/admin-api/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import getApp from '../../app';
import { createTestConfig } from '../../../test/config/test-config';
import { createServices } from '../../services';

function getSetup() {
async function getSetup() {
const stores = createStores();
const perms = permissions();
const config = createTestConfig({
preRouterHook: perms.hook,
});
const services = createServices(stores, config);
const app = getApp(config, stores, services);
const app = await getApp(config, stores, services);

return {
request: supertest(app),
Expand All @@ -30,8 +30,8 @@ let stores;
let request;
let destroy;

beforeEach(() => {
const setup = getSetup();
beforeEach(async () => {
const setup = await getSetup();
stores = setup.stores;
request = setup.request;
destroy = setup.destroy;
Expand Down
Loading

1 comment on commit 26b7da8

@vercel
Copy link

@vercel vercel bot commented on 26b7da8 Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.