-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
348 lines (294 loc) · 7.97 KB
/
app.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
require('json5/lib/require'); // Override "require" function. Make "require" can load json5 format.
const
fs = require('fs'),
URL = require('url'),
express = require('express'),
bodyParser = require('body-parser'),
Configstore = require('configstore'),
basicAuth = require('express-basic-auth'),
Project = require('./Project'),
Port = require('./Port'),
defaultConfig = require('./config/default.json5'),
pkg = require('./package.json'),
config = new Configstore(pkg.name, defaultConfig),
app = express(),
isHttps = !!config.get('ssl') || false,
TIMEOUT_TIME = config.get('limitTime') || 5000, // limit runC9 time.
ports = [],
projects = [],
c9s = [];
/* Initial config */
const defaultPorts = config.get('ports') || [];
defaultPorts.forEach((portNumber) => ports.push(new Port(portNumber)));
const defaultProject = config.get('projects') || [];
defaultProject.forEach((project) => projects.push(new Project(project)));
Project.TIMEOUT_TIME = TIMEOUT_TIME;
app.set('view engine', 'ejs');
/* Basic auth */
const authConfig = {};
authConfig[config.get('account')] = config.get('password');
app.use(basicAuth({
users: authConfig,
challenge: true,
realm: 'Cloud9 Launcher'
}));
/* Static */
app.use('/assets', express.static('assets'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
/* Router */
app.get('/', function(req, res) {
res.render('index', {
projects: projects,
url: `http${(isHttps)?'s':''}:\/\/${config.get('c9Host')}`
});
});
app.get('/setting', function(req, res) {
res.render('setting', {
ports: ports
});
});
const api = express.Router();
api.route('/launch') // used to open or close process
.post(async function(req, res) {
const targetProjectName = req.body.target;
const project = getProject(targetProjectName);
if (!project)
return res.status(404).send({
error: {
message: 'Cannot find the project by name.'
}
});
// If the project is active, exit.
if (project.isActive)
return res.status(403).send({
error: {
message: 'The project is running on port ' + project.port.number + '.',
data: {
port: project.port.number
}
}
});
// Find a free port and assign to target project.
const port = await getFreePort();
if (!port)
return res.status(404).send({
error: {
message: 'No free port. Please add or release some free ports.'
}
});
try {
const runPortNumber = await project.start(port);
console.info(`Project "${project.name}" is running. Use port ${runPortNumber}.`);
res.send({
succsess: {
data: {
port: runPortNumber
}
}
});
} catch (err) {
console.error(err);
return res.status(403).send(err);
}
})
.delete(function(req, res) {
const targetProjectName = req.body.target;
const project = getProject(targetProjectName);
if (!project)
return res.status(404).send({
error: {
message: 'Cannot find the project by name.'
}
});
// If the project is active, exit.
if (!project.isActive)
return res.status(403).send({
error: {
message: 'The project is not running.'
}
});
const
usedPort = project.port.number,
result = project.stop();
console.info(`Project "${project.name}" is shutdown. Port ${usedPort} is free.`);
if (result)
return res.send({
success: true
});
else
return res.status(403).send({
error: {
message: 'The project is not running.'
}
});
});
api.route('/project') // used to add, edit or delete project setting.
.post(function(req, res) {
const
projectName = req.body.name,
path = req.body.path;
if (!projectName || !path)
return res.status(403).send({
error: {
message: 'Lack of arguments.'
}
});
const project = new Project({
name: projectName,
path: path
});
projects.push(project);
updateProjectsConfig();
return res.send({
success: true
});
})
.put(function(req, res) {
const
targetProjectName = req.body.name,
path = req.body.path;
const project = getProject(targetProjectName);
if (!project)
return res.status(403).send({
error: {
message: 'Cannot find the project by name'
}
});
project.path = path;
updateProjectsConfig();
if (project.isActive)
return res.send({
success: {
message: 'Success but it\'s effective in next launch.'
}
});
else
return res.send({
success: true
});
})
.delete(function(req, res) {
const targetProjectName = req.body.name;
const project = popProject(targetProjectName);
if (!project)
return res.status(404).send({
error: {
message: 'Cannot find the project by name'
}
});
updateProjectsConfig();
res.send({
success: true
});
});
api.route('/port') // used to add or delete port setting
.post(async function(req, res) {
let portNumber = Number(req.body.number);
if (!portNumber || portNumber === 0)
return res.status(403).send({
error: {
message: 'Lack of port number.'
}
});
const port = new Port(portNumber);
if (ports.find((portNum) => portNum === portNumber))
return res.status(403).send({
error: {
message: 'Already has same port in ports list.'
}
});
ports.push(port);
updatePortsConfig();
return res.send({
success: true
});
})
.delete(function(req, res) {
const portNumber = req.body.number;
const port = popPort(portNumber);
if (!port)
return res.status(404).send({
error: {
message: 'Cannot find the port by port number.'
}
});
updatePortsConfig();
res.send({
success: true
});
});
app.use('/api', api);
// Launch UI server
const
sslConfig = config.get('ssl'),
port = config.get('port') || 8080;
if (isHttps) {
const option = {};
for (let k in sslConfig) {
if (fs.existsSync(sslConfig[k]))
option[k] = fs.readFileSync(sslConfig[k]);
else
console.warn('Config: property of ' + k + ' is not a correct path.');
}
require('https')
.createServer(option, app)
.listen(port, function() {
console.info('UI server listening on port ' + port);
});
} else
app.listen(port, function() {
console.info('UI server listening on port ' + port);
});
process.on('exit', function() {
c9s.forEach((c9) => {
if (c9 && c9.pid && !c9.killed) {
process.kill(c9.pid);
console.info('c9 killed');
}
});
});
// Return a free port.
/* TODO: change to port.usable. */
function getFreePort() {
return ports.find((port) => port.usableCache);
}
// Use project name to find out project.
function getProject(name) {
return projects.find((project) => project.name === name);
}
function popProject(name) {
const index = projects.findIndex((project) => project.name === name);
// If cannot find the project, return false.
if (!index)
return false;
// Remove project from array projects, and return this project.
const project = projects[index];
projects.splice(index, 1);
return project;
}
function popPort(number) {
const index = ports.findIndex((port) => port.number === number);
// If cannot find the port, return false.
if (!index)
return false;
// Remove port from array ports, and return this port.
const port = ports[index];
ports.splice(index, 1);
return port;
}
// save projects to config file.
function updateProjectsConfig() {
config.set('projects', projects.map((project) => {
return {
name: project.name,
path: project.path
};
}));
}
// save ports to config file.
function updatePortsConfig() {
config.set('ports', ports.map((port) => port.number));
}