import express from 'express'; import { CatalogDatabase } from '../database/store.js'; import { dashboard, page, table } from './views.js'; export function createWebApp(databasePath: string) { const app = express(); app.get('/', (_req, res, next) => { withDatabase(databasePath, (db) => res.send(dashboard(db.dashboardCounts()))).catch(next); }); app.get('/links', (_req, res, next) => { withDatabase(databasePath, (db) => res.send( page( 'Links', `

Links

${table(db.contentLinks(), [ ['newsletter', 'Newsletter'], ['issueDate', 'Issue Date'], ['category', 'Category'], ['title', 'Title'], ['url', 'URL'], ['description', 'Description'] ])}` ) ) ).catch(next); }); app.get('/sponsors', (_req, res, next) => { withDatabase(databasePath, (db) => res.send( page( 'Sponsored Links', `

Sponsored Links

${table(db.sponsoredLinks(), [ ['newsletter', 'Newsletter'], ['sponsor', 'Sponsor'], ['description', 'Description'] ])}` ) ) ).catch(next); }); app.get('/dead-links', (_req, res, next) => { withDatabase(databasePath, (db) => res.send( page( 'Dead Links', `

Dead Links

${table(db.deadLinks(), [ ['url', 'URL'], ['status', 'Status'], ['source', 'Source'], ['date', 'Date'] ])}` ) ) ).catch(next); }); app.get('/runs', (_req, res, next) => { withDatabase(databasePath, (db) => res.send( page( 'Runs', `

Runs

${table(db.runs(), [ ['started_at', 'Started'], ['mode', 'Mode'], ['newsletters_processed', 'Newsletters'], ['links_extracted', 'Links'], ['sponsors', 'Sponsors'], ['dead_links', 'Dead Links'], ['errors', 'Errors'] ])}` ) ) ).catch(next); }); app.use( (error: Error, _req: express.Request, res: express.Response, _next: express.NextFunction) => { console.error(error); res.status(500).send(page('Error', '

Error

Something went wrong.

')); } ); return app; } async function withDatabase( databasePath: string, callback: (database: CatalogDatabase) => void ): Promise { const db = new CatalogDatabase(databasePath); try { db.migrate(); callback(db); } finally { db.close(); } }