Add resolver, server, PDF rendering and web app
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
6a7fcdc4b8
commit
bdfdf581f3
59 files changed
+4088
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { existsSync, readFileSync, statSync } from 'node:fs';
|
||||
import { extname, join, normalize, resolve as resolvePath } from 'node:path';
|
||||
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';
|
||||
|
||||
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 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({ db, jobs, resolver, renderCardsPdf, renderCalibrationPdf, classNames, events });
|
||||
|
||||
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 rel = normalize(decodeURIComponent(c.req.path)).replace(/^([/\\])+/, '');
|
||||
const file = resolvePath(webRoot, rel === '' ? 'index.html' : rel);
|
||||
if (!file.startsWith(webRoot) || !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();
|
||||
});
|
||||
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);
|
||||
});
|
||||
Reference in new issue
Block a user