Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// Entry point. On Vercel this file is the function: it exports the Express app
// and Vercel handles requests. Anywhere else (`npm run dev`, `npm start`) it
// listens on PORT as a normal server.
//
// Vercel only recognises an Express project if this file itself imports and
// creates Express, which is why the app is made here rather than in src/.
import express from 'express';
import { ConfigError } from './src/errors.js';
import { createSite, serve } from './src/site.js';

const app = express();

let site;
try {
site = await createSite();
site = await createSite({ app });
} catch (error) {
// A readable message instead of a stack trace; on Vercel, rethrow so the deploy's logs show it.
if (!(error instanceof ConfigError) || process.env.VERCEL) throw error;
Expand All @@ -16,4 +22,4 @@ try {

if (!site.config.onVercel) serve(site);

export default site.app;
export default app;
12 changes: 10 additions & 2 deletions src/http/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { randomBytes, randomUUID } from 'node:crypto';
import { fileURLToPath } from 'node:url';
import compression from 'compression';
import ejs from 'ejs';
import express from 'express';
import helmet from 'helmet';
import { pinoHttp } from 'pino-http';
Expand Down Expand Up @@ -48,10 +49,17 @@ function sendVersioned(req, res, etag, body) {
return res.json(body);
}

export function createApp({ config, catalogue, reporter, logger, assets }) {
const app = express();
/**
* Adds the site's middleware and routes to an Express app. The app is passed in
* (rather than created here) because Vercel's framework detection only
* recognises an entry file that itself imports and creates Express.
*/
export function createApp({ config, catalogue, reporter, logger, assets, app = express() }) {
app.disable('x-powered-by');
app.set('trust proxy', config.trustProxy);
// Registered explicitly: Express would load EJS by name at runtime, which Vercel's
// file tracer can't see, so EJS would be missing from the deployed function.
app.engine('ejs', ejs.__express);
app.set('view engine', 'ejs');
app.set('views', viewsDir);

Expand Down
7 changes: 4 additions & 3 deletions src/site.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fileURLToPath } from 'node:url';
import express from 'express';
import { waitUntil } from '@vercel/functions';
import { createCatalogue } from './catalogue.js';
import { loadEnvFile, readConfig } from './config.js';
Expand All @@ -15,11 +16,11 @@ const SHUTDOWN_TIMEOUT_MS = 10_000;

/**
* Builds the whole site: configuration, logging, error reporting, TIDAL and
* the Express app. Starts loading playlists, but does not listen on a port,
* the Express app (created by the caller, see app.js). Starts loading playlists, but does not listen on a port,
* so the same app runs as a Vercel Function or as a normal server.
* Throws a ConfigError when settings are missing or invalid.
*/
export async function createSite() {
export async function createSite({ app = express() } = {}) {
loadEnvFile();
const config = readConfig();

Expand Down Expand Up @@ -58,7 +59,7 @@ export async function createSite() {
versioned: config.isProduction,
});

const app = createApp({ config, catalogue, reporter, logger, assets });
createApp({ app, config, catalogue, reporter, logger, assets });
await catalogue.start();
return { app, config, logger, reporter, assets };
}
Expand Down
Loading