aboutsummaryrefslogtreecommitdiffhomepage
path: root/api/routes/application.js
blob: add60bed8ffa4b5e9e49e21941714130be25f67e (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const childProcess = require('child_process')
const path = require('path')
const express = require('express')
const expressWs = require('express-ws')

const config = require('../config')

const appDir = path.join(__dirname, '..', '..', 'application')

function init (app) {
  expressWs(app)

  const opts = {
    cwd: appDir,
    env: Object.assign({}, process.env, {
      ENABLE_LOCAL: true,
      ENABLE_GITHUB: config.ENABLE_GITHUB,
      GITHUB_APP_NAME: config.GITHUB_APP_NAME,
      API_BASE_URL: 'http://localhost:8080',
      APP_BASE_URL: 'http://localhost:8080/application'
    })
  }

  childProcess.execFile('npm', ['run', 'build-watch'], opts, err => {
    console.error(err)
    console.error('Application serving failed')
    process.exit(1)
  })
  
  app.get('/', (req, res) => res.redirect('/application'))
  app.use('/application', express.static(path.join(appDir, 'dist')))
  
  const subscribers = []
  app.ws('/console', (ws, req) => {
    const { remoteAddress } = req.connection
    subscribers.push(ws)
  
    console.info(`[${new Date()}] [${remoteAddress}] connected`)
  
    ws.onerror = err => {
      console.error(`[${new Date()}] [${remoteAddress}]`, err)
    }
  
    ws.onclose = () => {
      console.info(`[${new Date()}] [${remoteAddress}] disconnected`)
      const index = subscribers.indexOf(ws)
      subscribers.splice(index, 1)
    }
  })
}

module.exports = init