process.env.GEM_MINE_CLI_API_MODE = 'true'

import * as http from 'http'
import config from './config'

import app from './app'
import attachSocket from './socket'
import { initDB } from './db'

async function start() {
  await initDB()
  const server = http.createServer(app.callback())
  attachSocket(server)
  server.listen(config.PORT, () => {
    console.log(`Koa Start at ${config.PORT}`)
    process.send && process.send({
      type: 'cli-service: start success',
      data: {}
    })
    process.on('message', (packet) => {
      if (packet.topic === 'cli-service: check alive') {
        process.send && process.send({
          type: 'cli-service: alive',
          data: {}
        })
      }
    })
  })
}

start()
