diff options
author | devin ivy <devinivy@gmail.com> | 2024-06-21 12:41:06 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-21 12:41:06 -0400 |
commit | 55812b03940852f1f91cd0a46b5c093601c854a9 (patch) | |
tree | 54956cb522786b1260b0a556f6f7c3ea1b0aed11 /bskylink/src/index.ts | |
parent | ba21fddd7897513fef663b826094878ad0ff1556 (diff) | |
download | voidsky-55812b03940852f1f91cd0a46b5c093601c854a9.tar.zst |
Bsky short link service (#4542)
* bskylink: scaffold service w/ initial config and schema * bskylink: implement link creation and redirects * bskylink: tidy * bskylink: tests * bskylink: tidy, add error handler * bskylink: add dockerfile * bskylink: add build * bskylink: fix some express plumbing * bskyweb: proxy fallthrough routes to link service redirects * bskyweb: build w/ link proxy * Add AASA to bskylink (#4588) --------- Co-authored-by: Hailey <me@haileyok.com>
Diffstat (limited to 'bskylink/src/index.ts')
-rw-r--r-- | bskylink/src/index.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/bskylink/src/index.ts b/bskylink/src/index.ts new file mode 100644 index 000000000..ca425eee8 --- /dev/null +++ b/bskylink/src/index.ts @@ -0,0 +1,45 @@ +import events from 'node:events' +import http from 'node:http' + +import cors from 'cors' +import express from 'express' +import {createHttpTerminator, HttpTerminator} from 'http-terminator' + +import {Config} from './config.js' +import {AppContext} from './context.js' +import {default as routes, errorHandler} from './routes/index.js' + +export * from './config.js' +export * from './db/index.js' +export * from './logger.js' + +export class LinkService { + public server?: http.Server + private terminator?: HttpTerminator + + constructor(public app: express.Application, public ctx: AppContext) {} + + static async create(cfg: Config): Promise<LinkService> { + let app = express() + app.use(cors()) + + const ctx = await AppContext.fromConfig(cfg) + app = routes(ctx, app) + app.use(errorHandler) + + return new LinkService(app, ctx) + } + + async start() { + this.server = this.app.listen(this.ctx.cfg.service.port) + this.server.keepAliveTimeout = 90000 + this.terminator = createHttpTerminator({server: this.server}) + await events.once(this.server, 'listening') + } + + async destroy() { + this.ctx.abortController.abort() + await this.terminator?.terminate() + await this.ctx.db.close() + } +} |