forked from webaverse/preview-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (78 loc) · 2.45 KB
/
index.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
const fs = require('fs');
const url = require('url');
const http = require('http');
const https = require('https');
const { _handlePreviewRequest } = require('./routes/preview.js')
const { _handleLandPreviewRequest } = require('./routes/land-preview.js')
const { _handleCardPreviewRequest } = require('./routes/card-preview.js')
const { _handleBakeRequest } = require('./routes/bake.js')
let CERT, PRIVKEY;
try {
CERT = fs.readFileSync('./certs/fullchain.pem');
PRIVKEY = fs.readFileSync('./certs/privkey.pem');
} catch {
try {
CERT = fs.readFileSync('./certs-local/fullchain.pem');
PRIVKEY = fs.readFileSync('./certs-local/privkey.pem');
} catch {
console.warn("No certs found");
}
}
const PORT = parseInt(process.env.PORT, 10) || 80;
Error.stackTraceLimit = 300;
const _req = protocol => (req, res) => {
try {
const o = url.parse(protocol + '//' + (req.headers['host'] || '') + req.url);
if (o.host === 'preview.webaverse.com') {
_handlePreviewRequest(req, res);
return;
} else if (o.host === 'land-preview.exokit.org') {
_handleLandPreviewRequest(req, res);
return;
} else if (o.host === 'card-preview.exokit.org') {
_handleCardPreviewRequest(req, res);
return;
} else if (o.host === 'bake.exokit.org') {
_handleBakeRequest(req, res);
return;
} else if (o.host.includes("preview-backend") || o.host.includes("webaverse-preview")){
_handlePreviewRequest(req, res);
return;
}
res.statusCode = 200;
res.end('host not found: ' + JSON.stringify(o));
} catch (err) {
console.warn(err.stack);
res.statusCode = 500;
res.end(err.stack);
}
};
const server = http.createServer(_req('http:'));
server.on('error', err => {
console.warn('http server error', err);
});
let server2;
if (CERT && PRIVKEY) {
server2 = https.createServer({
cert: CERT,
key: PRIVKEY,
}, _req('https:'));
server2.on('error', err => {
console.warn('https server error', err);
});
}
const _warn = err => {
console.warn('uncaught: ' + err.stack);
};
process.on('uncaughtException', _warn);
process.on('unhandledRejection', _warn);
server.listen(process.env.HTTP_PORT || PORT);
console.log(`http://127.0.0.1:${PORT}`);
if (CERT && PRIVKEY) {
const HTTPS_PORT = 443;
server2.listen(process.env.HTTPS_PORT || HTTPS_PORT);
console.log(`https://127.0.0.1:${HTTPS_PORT}`);
}
process.on('exit', (code, signal) => {
console.warn('process exited', {code, signal});
});