about summary refs log tree commit diff
path: root/bskylink/src/bin.ts
blob: 3e0746a9899a884d14d461eb14567ba49fc9002d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import {Database, envToCfg, httpLogger, LinkService, readEnv} from './index.js'
async function main() {
  const env = readEnv()
  const cfg = envToCfg(env)
  if (cfg.db.migrationUrl) {
    const migrateDb = Database.postgres({
      url: cfg.db.migrationUrl,
      schema: cfg.db.schema,
    })
    await migrateDb.migrateToLatestOrThrow()
    await migrateDb.close()
  }

  const link = await LinkService.create(cfg)

  if (link.ctx.cfg.service.safelinkEnabled) {
    link.ctx.safelinkClient.runFetchEvents()
  }

  await link.start()
  httpLogger.info('link service is running')
  process.on('SIGTERM', async () => {
    httpLogger.info('link service is stopping')
    await link.destroy()
    httpLogger.info('link service is stopped')
  })
}

main()