-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (100 loc) · 3.32 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
const express = require('express');
const app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
const server = app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
// socket.io
const socketIO = require('socket.io');
const io = socketIO.listen(server);
const fs = require('fs');
const https = require('https');
const rmdir = require('rimraf');
const publicDirPath = './public/';
const configPath = publicDirPath + 'config.json';
const tmpDirLink = 'tmp/';
function log(){
const msg = Array.from(arguments).reduce((a,b) => a + ' ' + (
b instanceof Error ? b.toString():
typeof b === 'object' ? JSON.stringify(b, null, 2):
b
), '');
console.log(`${new Date()} ${msg}`);
}
io.on('connection', function(socket){
log('Receive', 'CONNECTION');
socket.on('disconnection', function(){
log('Receive', 'DISCONNECTION');
});
socket.on('config', function(){
log('Receive', 'CONFIG');
requestMLab('GET', {_id:'default'}, function(configs){
if(configs.length != 1){
log('ERROR: Found not exactly one config')
}
else{
var config = configs[0].config;
log('GET');
socket.emit('got-config', config);
}
});
});
socket.on('save', function(config){
log('Receive', 'SAVE');
requestMLab('POST', {_id:'default', config:config}, function(response){
log('POST', response);
socket.emit('saved');
});
});
var timer = null;
socket.on('download', function(data){
log('Receive', 'DOWNLOAD');
clearTimeout(timer);
const downloadLink = tmpDirLink + data.filename;
const downloadPath = publicDirPath + downloadLink;
const tmpDirPath = publicDirPath + tmpDirLink;
if(!fs.existsSync(tmpDirPath)) fs.mkdirSync(tmpDirPath);
fs.writeFile(downloadPath, data.content, function(err){
if(err){
log('ERROR', err);
socket.emit('downloaded', null);
}
else{
log('Done writing', downloadPath);
socket.emit('downloaded', downloadLink);
timer = setTimeout(function(){
rmdir(tmpDirPath, function(){
log('Deleted', tmpDirPath);
});
}, 60*1000);
}
});
});
});
function requestMLab(method, data, callback){
data = data? JSON.stringify(data): '';
var apiKey = 'RUg7fCn5rsI85SowYalPJzSMU2Bf1bi5';
var options = {
host: 'api.mlab.com',
path: '/api/1/databases/tkc/collections/tkc-membership?apiKey=' + apiKey,
method,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
},
};
var req = https.request(options, function(res) {
var buf = new Buffer(1024*1024);
var n = 0;
res.setEncoding('utf8');
res.on('data', function (chunk) {
n += buf.write(chunk, n);
});
res.on('end', function(){
callback(JSON.parse(buf.slice(0,n).toString().toString()));
});
});
req.write(data);
req.end();
}