Scratch space for learning atproto app development
1import { createHttpTerminator } from 'http-terminator'
2import { once } from 'node:events'
3
4import { createAppContext } from '#/context'
5import { env } from '#/env'
6import { run } from '#/lib/process'
7import { createRouter } from '#/routes'
8import { startServer } from '#/lib/http'
9
10run(async (killSignal) => {
11 // Create the application context
12 const ctx = await createAppContext()
13
14 // Create the HTTP router
15 const router = createRouter(ctx)
16
17 // Start the HTTP server
18 const { terminate } = await startServer(router, { port: env.PORT })
19
20 const url = env.PUBLIC_URL || `http://localhost:${env.PORT}`
21 ctx.logger.info(`Server (${env.NODE_ENV}) running at ${url}`)
22
23 // Subscribe to events on the firehose
24 ctx.ingester.start()
25
26 // Wait for a termination signal
27 if (!killSignal.aborted) await once(killSignal, 'abort')
28 ctx.logger.info(`Signal received, shutting down...`)
29
30 // Gracefully shutdown the http server
31 await terminate()
32
33 // Close the firehose connection
34 await ctx.ingester.destroy()
35})