import WebSocket from 'ws'
import KetchupServer from 'ketchup-server'
import { initial, reducer } from 'wonders-common'

const port = parseInt(process.env.PORT ?? '80')

const wss = new WebSocket.Server({ port })

const ks = new KetchupServer(initial, reducer)

wss.on('connection', ws => {
  ks.addRemoteClient(ws)
})

console.log(`Listening on ${port}`)

function shutdown() {
  console.log('Shutting down')
  wss.close()
}
process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)
