-
Notifications
You must be signed in to change notification settings - Fork 9
/
app-code.js
73 lines (60 loc) · 1.51 KB
/
app-code.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
/**
* This is the app for code.highcharts.local
*/
// Modules sorted by names alphabetically
const express = require('express');
const http = require('http');
const {
crtFile,
codePort,
codeSecurePort,
pemFile
} = require('./lib/arguments.js');
const { getCodeFile } = require('./lib/functions');
const { startWatchServer } = require('./lib/websocket.js');
/**
* Create express application
*/
const app = express();
const options = {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
}
};
// Serve content of code directory
app.use('/', async (req, res) => {
const url = req.url.replace(/^\/(data-grid|gantt|maps|stock)\//g, '/');
const { error, content, path } = await getCodeFile(url, req);
if (error) {
res.end(error);
return;
}
if (content) {
res.type('text/javascript');
res.send(content);
return;
}
res.sendFile(path, options);
});
/**
* Create HTTP server.
*/
const server = http.createServer(app);
server.listen(codePort);
/**
* Create HTTPS server.
*/
if (codeSecurePort) {
const fs = require('fs');
const https = require('https');
const httpsOptions = {
key: fs.existsSync(pemFile) && fs.readFileSync(pemFile, 'utf-8'),
cert: fs.existsSync(crtFile) && fs.readFileSync(crtFile, 'utf-8')
};
if (httpsOptions.cert && httpsOptions.key) {
https.createServer(httpsOptions, app).listen(codeSecurePort);
}
}
// WebSocket server for codeWatch
startWatchServer(server);