main.js 1.05 KB
/**
 * `@voko/server` 进程入口:runtime → app → listen → close。
 */

import { buildApp } from './app.js'
import { createRuntime } from './assembly/runtime.js'

async function start() {
  const runtime = await createRuntime({ service: '@voko/server' })
  const app = buildApp({ services: runtime.services, logger: runtime.logger })
  await app.listen({ port: runtime.env.port, host: '0.0.0.0' })
  runtime.logger.info('服务已启动', { port: runtime.env.port, environment: runtime.env.nodeEnv })
  return {
    async close() {
      await app.close()
      await runtime.close()
    },
  }
}

start()
  .then(({ close }) => {
    const stop = async (signal) => {
      try {
        await close()
        process.exit(0)
      } catch (err) {
        console.error(`voko-server failed to stop after ${signal}:`, err)
        process.exit(1)
      }
    }
    process.once('SIGINT', () => stop('SIGINT'))
    process.once('SIGTERM', () => stop('SIGTERM'))
  })
  .catch((err) => {
    console.error('voko-server failed to start:', err)
    process.exit(1)
  })