The edition comes only from a record for this exact edition: the Library of Congress record's edition statement first, then Open Library's edition name when it is a well-formed statement. Existing books are filled in at startup without changing anything else about them. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
52 lines
2.5 KiB
TypeScript
52 lines
2.5 KiB
TypeScript
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
import { dirname, extname, join, resolve as resolvePath } from 'node:path';
|
|
import { staticPath } from './static.ts';
|
|
import { serve } from '@hono/node-server';
|
|
import { openDb } from './db.ts';
|
|
import { createJobs } from './jobs.ts';
|
|
import { createApp, createEventBus } from './app.ts';
|
|
import { createResolver, TemporaryError } from './resolve/resolver.ts';
|
|
import { renderCalibrationPdf, renderCardsPdf } from './pdf/render.ts';
|
|
import { classNames } from '../shared/lcc-outline.ts';
|
|
import { hierarchyOf } from './hierarchy.ts';
|
|
|
|
const port = Number(process.env.PORT ?? 4321);
|
|
const dbPath = process.env.BOOKSORTER_DB ?? 'data/booksorter.db';
|
|
const webRoot = resolvePath(import.meta.dirname, '../../dist/web');
|
|
|
|
const MIME: Record<string, string> = {
|
|
'.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8',
|
|
'.wasm': 'application/wasm', '.json': 'application/json', '.svg': 'image/svg+xml', '.png': 'image/png',
|
|
'.ico': 'image/x-icon', '.webmanifest': 'application/manifest+json', '.map': 'application/json',
|
|
};
|
|
|
|
const capturesDir = process.env.BOOKSORTER_CAPTURES ?? join(dirname(dbPath), 'captures');
|
|
const db = openDb(dbPath);
|
|
const events = createEventBus();
|
|
const resolver = createResolver({ fetch: globalThis.fetch, cache: db.cache });
|
|
const jobs = createJobs({
|
|
db, resolver, onChange: (b) => events.emitBook(b), isTemporary: (e) => e instanceof TemporaryError,
|
|
});
|
|
const app = createApp({ port, db, jobs, resolver, renderCardsPdf, renderCalibrationPdf, classNames, hierarchy: hierarchyOf, events, capturesDir });
|
|
|
|
app.get('*', (c) => {
|
|
if (!existsSync(join(webRoot, 'index.html'))) {
|
|
return c.text('The web app has not been built. Run `npm run build` and reload.', 503);
|
|
}
|
|
const file = staticPath(webRoot, c.req.path);
|
|
if (!file || !existsSync(file) || !statSync(file).isFile()) return c.text('Not found', 404);
|
|
return c.body(readFileSync(file), 200, { 'Content-Type': MIME[extname(file)] ?? 'application/octet-stream' });
|
|
});
|
|
|
|
const server = serve({ fetch: app.fetch, port, hostname: '127.0.0.1' }, () => {
|
|
console.log(`Booksorter running at http://localhost:${port}`);
|
|
jobs.resume();
|
|
jobs.resumeTails();
|
|
jobs.resumeEditions();
|
|
});
|
|
server.on('error', (err: NodeJS.ErrnoException) => {
|
|
if (err.code === 'EADDRINUSE') console.error(`Port ${port} is already in use. Is Booksorter already running?`);
|
|
else console.error(err.message);
|
|
process.exit(1);
|
|
});
|