my fork of the bluesky client
1import events from 'node:events'
2import http from 'node:http'
3
4import cors from 'cors'
5import express from 'express'
6import {createHttpTerminator, HttpTerminator} from 'http-terminator'
7
8import {Config} from './config.js'
9import {AppContext} from './context.js'
10import {default as routes, errorHandler} from './routes/index.js'
11
12export * from './config.js'
13export * from './db/index.js'
14export * from './logger.js'
15
16export class LinkService {
17 public server?: http.Server
18 private terminator?: HttpTerminator
19
20 constructor(public app: express.Application, public ctx: AppContext) {}
21
22 static async create(cfg: Config): Promise<LinkService> {
23 let app = express()
24 app.use(cors())
25
26 const ctx = await AppContext.fromConfig(cfg)
27 app = routes(ctx, app)
28 app.use(errorHandler)
29
30 return new LinkService(app, ctx)
31 }
32
33 async start() {
34 this.server = this.app.listen(this.ctx.cfg.service.port)
35 this.server.keepAliveTimeout = 90000
36 this.terminator = createHttpTerminator({server: this.server})
37 await events.once(this.server, 'listening')
38 }
39
40 async destroy() {
41 this.ctx.abortController.abort()
42 await this.terminator?.terminate()
43 await this.ctx.db.close()
44 }
45}