import fs from 'node:fs' import path from 'node:path' import { serve } from '@hono/node-server' import { serveStatic } from '@hono/node-server/serve-static' import { Hono, type Handler } from 'hono' import { BotAccount } from './atproto/bot.ts' import { createOAuthClient } from './atproto/oauth.ts' import { StreamerActions } from './atproto/streamer.ts' import { BotManager, makePlatform } from './bot/manager.ts' import { Db } from './db/db.ts' import { Store } from './db/store.ts' import { env } from './env.ts' import { oauthPrivateJwk, sessionSecret } from './secrets.ts' import { apiRoutes } from './http/api.ts' import { authRoutes } from './http/auth.ts' import type { AppEnv, Deps } from './http/context.ts' import { hookRoutes } from './http/hooks.ts' import { publicRoutes } from './http/public.ts' import { Jetstream } from './jetstream.ts' import { RECORD_COLLECTIONS } from './records/collections.ts' import { pdsReader } from './records/reader.ts' import { RecordSync } from './records/sync.ts' // Without SESSION_SECRET or OAUTH_PRIVATE_JWK in the environment, generate them once and keep them in DATA_DIR. if (!process.env.SESSION_SECRET) env.sessionSecret = sessionSecret(env.dataDir) if (!env.oauthPrivateJwk && !env.isLoopback) env.oauthPrivateJwk = await oauthPrivateJwk(env.dataDir) const store = new Store(new Db(path.join(env.dataDir, 'superbot.db'))) const oauth = await createOAuthClient(store) const streamer = new StreamerActions(oauth) const bot = new BotAccount(store) await bot.start().catch((err) => console.error('[bot] sign-in failed:', err.message)) const manager = new BotManager(store, makePlatform(bot, streamer)) // Public config lives in its owner's repo. Watch those repos for changes made // anywhere, and catch up on whatever happened while we weren't watching. const records = new RecordSync(store, streamer, pdsReader) const recordFeed = new Jetstream(store, 'records', [...RECORD_COLLECTIONS], store.listRecordOwners()) recordFeed.on('commit', (evt) => records.onCommit({ did: evt.did, ...evt.commit })) store.on('write', (e) => { if (e.type === 'channel' || e.type === 'lookalike') recordFeed.setDids(store.listRecordOwners()) }) const deps: Deps = { store, manager, oauth, streamer, bot, records } const app = new Hono() app.route('/', authRoutes(deps)) app.route('/api/hooks', hookRoutes(deps)) app.route('/api', apiRoutes(deps)) app.route('/api/public', publicRoutes(deps)) app.get('/healthz', (c) => c.json({ ok: true, bot: bot.ready })) // In production the server also serves the built dashboard, with SPA fallback. const dist = path.resolve('dist') if (fs.existsSync(path.join(dist, 'index.html'))) { const socialCardUrl = `${new URL(env.publicUrl).origin}/social-card.png` const index = fs .readFileSync(path.join(dist, 'index.html'), 'utf8') .replace('content="/social-card.png"', `content="${socialCardUrl}"`) const renderIndex: Handler = (c) => c.html(index) app.get('/', renderIndex) app.get('/index.html', renderIndex) app.use('/*', serveStatic({ root: path.relative(process.cwd(), dist) })) app.get('*', renderIndex) } await manager.start() recordFeed.start() void records.reconcileAll() const recordTimers = [setInterval(() => void records.flush(), 60_000), setInterval(() => void records.reconcileAll(), 6 * 3600_000)] const server = serve({ fetch: app.fetch, port: env.port, hostname: env.production ? '0.0.0.0' : '127.0.0.1' }, (info) => { console.log(`[http] listening on :${info.port}. Dashboard at ${env.publicUrl}`) }) const shutdown = () => { manager.stop() recordFeed.stop() for (const t of recordTimers) clearInterval(t) server.close() process.exit(0) } process.on('SIGINT', shutdown) process.on('SIGTERM', shutdown)