Monorepo for wisp.place. A static site hosting service built on top of the AT Protocol.
wisp.place
1import app from './server';
2import { serve } from '@hono/node-server';
3import { FirehoseWorker } from './lib/firehose';
4import { logger } from './lib/observability';
5import { mkdirSync, existsSync } from 'fs';
6import { backfillCache } from './lib/backfill';
7
8const PORT = process.env.PORT ? parseInt(process.env.PORT) : 3001;
9const CACHE_DIR = process.env.CACHE_DIR || './cache/sites';
10
11// Parse CLI arguments
12const args = process.argv.slice(2);
13const hasBackfillFlag = args.includes('--backfill');
14const backfillOnStartup = hasBackfillFlag || process.env.BACKFILL_ON_STARTUP === 'true';
15
16// Ensure cache directory exists
17if (!existsSync(CACHE_DIR)) {
18 mkdirSync(CACHE_DIR, { recursive: true });
19 console.log('Created cache directory:', CACHE_DIR);
20}
21
22// Start firehose worker with observability logger
23const firehose = new FirehoseWorker((msg, data) => {
24 logger.info(msg, data);
25});
26
27firehose.start();
28
29// Run backfill if requested
30if (backfillOnStartup) {
31 console.log('🔄 Backfill requested, starting cache backfill...');
32 backfillCache({
33 skipExisting: true,
34 concurrency: 3,
35 }).then((stats) => {
36 console.log('✅ Cache backfill completed');
37 }).catch((err) => {
38 console.error('❌ Cache backfill error:', err);
39 });
40}
41
42// Add health check endpoint
43app.get('/health', (c) => {
44 const firehoseHealth = firehose.getHealth();
45 return c.json({
46 status: 'ok',
47 firehose: firehoseHealth,
48 });
49});
50
51// Start HTTP server with Node.js adapter
52const server = serve({
53 fetch: app.fetch,
54 port: PORT,
55});
56
57console.log(`
58Wisp Hosting Service
59
60Server: http://localhost:${PORT}
61Health: http://localhost:${PORT}/health
62Cache: ${CACHE_DIR}
63Firehose: Connected to Firehose
64`);
65
66// Graceful shutdown
67process.on('SIGINT', async () => {
68 console.log('\n🛑 Shutting down...');
69 firehose.stop();
70 server.close();
71 process.exit(0);
72});
73
74process.on('SIGTERM', async () => {
75 console.log('\n🛑 Shutting down...');
76 firehose.stop();
77 server.close();
78 process.exit(0);
79});