-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
66 lines (54 loc) · 1.74 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
var
crypto = require('crypto'),
http = require('http'),
url = require('url'),
fs = require('fs'),
sys = require('sys'),
Relay = require('./relay'),
mimeTypes = {
'ico': 'image/vnd.microsoft.icon',
'js': 'text/javascript',
'css': 'text/css',
'html': 'text/html'
},
defaultMimeType = 'text/html',
getExtension = function(str) {
if (str.lastIndexOf('.') < 0) return null;
return str.substring(str.lastIndexOf('.') + 1);
},
getMimeType = function(extension) {
return (extension in mimeTypes) ? mimeTypes[extension] : defaultMimeType;
},
root = __dirname + '/htdocs',
server = http.createServer(function(req, res) {
var path = url.parse(req.url).pathname;
if (path[path.length - 1] == '/') {
path += 'index.html';
}
fs.readFile(root + path, function(err, data){
if (err) return send404(res);
res.writeHead(200, {'Content-Type': getMimeType(getExtension(path)), 'Content-Length': data.length});
res.end(data, 'utf8');
});
}),
send404 = function(res){
res.writeHead(404);
res.end('404');
};
settings = require('./settings');
var log = fs.createWriteStream(settings.LOG_FILE, {flags: 'a'});
var relayOptions = {
resource: 'socket.io',
transports: ['websocket', 'server-events', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling'],
//transportOptions: {},
log: function(str) {
// TODO: make this use proper logging.
sys.puts(str);
}
};
var privateKey = fs.readFileSync(settings.PRIVATE_KEY).toString();
var certificate = fs.readFileSync(settings.CERTIFICATE).toString();
var credentials = crypto.createCredentials({key: privateKey, cert: certificate});
server.setSecure(credentials);
server.listen(settings.PORT);
var relay = Relay(server, settings.BUFFER_SIZE, settings.MAX_BASE_NICKNAME_LENGTH, relayOptions);