-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
75 lines (61 loc) · 1.79 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
const aws = require('aws-sdk');
const fs = require('fs');
const https = require('https');
const http = require('http');
const keyPath = '/etc/letsencrypt/live/bugs.homegames.io/privkey.pem';
const certPath = '/etc/letsencrypt/live/bugs.homegames.io/fullchain.pem';
const getReqBody = (req, cb) => {
let _body = '';
req.on('data', chunk => {
_body += chunk.toString();
});
req.on('end', () => {
cb && cb(_body);
});
};
const storeRecord = (record) => {
if (record.length > 1000) {
console.log("Truncating record of length " + record.length);
record = record.substring(1000);
}
const errString = `[${Date.now()}] ${record.toString()}\n`;
const buf = Buffer.from(errString, 'utf-8');
fs.appendFileSync('logfile.txt', buf);
const fileInfo = fs.statSync('logfile.txt');
const fileSize = fileInfo.size;
const fileSizeMb = fileSize / (1024 * 1024);
if (fileSizeMb > 10) {
console.log('writing log file. size in mb: ' + fileSizeMb);
const s3 = new aws.S3();
const params = { Bucket: 'homegames', Key: 'error-logs/' + Date.now(), Body: fs.readFileSync('logfile.txt')};
s3.upload(params, {}, (err, data) => {
console.log('s3 response');
console.log(err);
console.log(data);
fs.writeFileSync('logfile.txt', '');
});
}
}
const settings = {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath)
};
const app = https.createServer(settings, (req, res) => {
console.log('got req ' + req.method);
if (req.method === 'POST') {
res.end('ok');
getReqBody(req, (data) => {
storeRecord(data);
});
}
res.writeHead(400);
res.end('no');
});
app.listen(443);
const redirectApp = http.createServer((req, res) => {
res.writeHead(301, {
'Location': 'https://bugs.homegames.io'
});
res.end();
});
redirectApp.listen(80);