-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
194 lines (153 loc) · 4.45 KB
/
server.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* eslint-disable no-path-concat */
const http = require('http')
const path = require('path')
const express = require('express')
const bodyParser = require('body-parser')
const opn = require('opn')
const os = require('os')
const _ = require('lodash')
const memCache = require('./utils/mem_cache')
const fs = require('fs')
const pw = require('./processWorker')
const { getCommandById } = require('./utils/commands')
const { PATHS, MESSAGE_TYPES } = require('./constants')
const kill = require('tree-kill')
const { createWSConnection, sendMessage } = require('./ws')
const { getMainConfig, getRC, updateRC, removeConfig } = require('./config')
const rootPath = path.resolve(__dirname, PATHS.WEB_APP_ROOT)
function start(config) {
console.log('Initializing web server')
const app = express()
app.use(bodyParser.json())
app.use(express.static(rootPath))
app.get('/', (req, res) => res.sendFile(rootPath))
app.get('/info', (req, res) => {
const rcSnapshot = memCache.get('rc-snapshot')
console.log('/info -> rcSnapshot', rcSnapshot)
res.send({
...rcSnapshot,
configs: _.map(rcSnapshot.configs, config => ({
..._.omit(config, 'pid', 'path'),
commands: _.map(config.commands, command => ({
...command,
logs: [],
})),
})),
})
})
app.post('/refresh-rc-configs', (req, res) => {
const freshRC = getRC()
const rcSnapshot = memCache.get('rc-snapshot')
const newConfig = freshRC.configs[freshRC.configs.length - 1]
memCache.set('rc-snapshot', {
configs: [...rcSnapshot.configs, newConfig],
})
kill(newConfig.pid, 'SIGINT')
sendMessage(MESSAGE_TYPES.APPS_LIST_UPDATE, { ok: true })
res.send('ok')
return null
})
app.post('/:configId/:taskId/envs', (req, res) => {
const { taskId, configId } = req.params
const envs = req.body
const currentCommand = getCommandById(configId, taskId)
currentCommand.logs = []
currentCommand.envs = envs
pw.reRun(currentCommand)
res.send('ok')
})
app.delete('/:configId', (req, res) => {
const { configId } = req.params
removeConfig(configId)
res.send('ok')
})
app.delete('/:configId/:taskId/logs', (req, res) => {
const { taskId, configId } = req.params
getCommandById(configId, taskId).logs = []
res.send('ok')
})
app.get('/:configId/:taskId/logs', (req, res) => {
const { taskId, configId } = req.params
res.send(getCommandById(configId, taskId).logs)
})
app.get('/project-version', (req, res) => {
try {
const packageJson = JSON.parse(fs.readFileSync('package.json').toString())
res.send(packageJson.version || null)
} catch (e) {
res.send(null)
}
})
app.post('/:configId/:taskId/run', (req, res) => {
const { taskId, configId } = req.params
const command = getCommandById(configId, taskId)
pw.run(command)
res.send('ok')
})
app.post('/:configId/:taskId/stop', (req, res) => {
const { taskId, configId } = req.params
const command = getCommandById(configId, taskId)
pw.stop(command)
res.send('ok')
})
const server = http.createServer(app)
createWSConnection(server)
server.listen(config.port, '0.0.0.0', () => {
console.log(`Flamebird launched on port ${config.port}`)
if (!config.withoutBrowser)
try {
opn('http://localhost:' + config.port, {
app: os.platform() === 'win32' ? 'chrome' : 'google-chrome',
})
} catch (e) {
console.error('Cannot open Google Chrome browser')
}
})
}
const update = config => {
console.log('Try to connect to main flamebird process')
const setConfigAsMain = () => {
console.log('setConfigAsMain')
updateRC(
memCache.set('rc-snapshot', {
configs: [
_.merge(config, {
main: true,
}),
],
})
)
start(config)
}
const mainConfig = getMainConfig()
if (!mainConfig) {
setConfigAsMain()
return
}
process.once('uncaughtException', setConfigAsMain)
http
.request(
{
timeout: 5000,
host: '127.0.0.1',
path: `/refresh-rc-configs`,
port: mainConfig.port,
method: 'POST',
},
res => {
res.resume()
res.on('error', err => {
console.log('TODO', err)
})
res.on('end', () => {
console.log('Successfully connected')
process.exit(0)
})
}
)
.end()
}
module.exports = {
start,
update,
}