/
routes.js
73 lines (68 loc) · 2.11 KB
/
routes.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
app.post('/mustekala/trigger', function(req, res) {
var password=req.body.password;
var channel=req.body.channel;
var action=req.body.action;
var data=req.body.data;
var result=mustekala.trigger(password, channel, action, data);
// console.log({'password': password, 'channel': channel, 'action': action, 'data': data})
res.send(JSON.stringify({'success':result, 'channel': channel, 'action': action, 'data': data}));
});
app.post('/mustekala/authentificate', function(req, res) {
var password=req.body.password;
var channel=req.body.channel;
var user=req.body.channel;
});
app.get('/mustekala.js', function(req, res) {
// we must "cheat" socket.io to get the socket.io.js with specific transports
var ioHead={};
var ioEnd={};
var fakeRes = {
writeHead: function(status, headers) {
ioHead.status=status;
ioHead.headers=headers;
},
end: function(content, encoding) {
ioEnd.content=content;
ioEnd.encoding=encoding;
finishRequest();
}
}
// fake uri
req.url="/socket.io/socket.io.js";
var data=mustekala.io.checkRequest(req);
var mustekalaJS=require('fs').readFileSync(process.cwd()+'/lib/mustekala.client.js');
mustekala.io.static.write(data.path, req, fakeRes);
// send js
function finishRequest() {
// inject mustekala
ioEnd.content=mustekalaJS+"\n"+ioEnd.content;
ioHead.headers['Content-Length']=ioEnd.content.length;
res.writeHead(ioHead.status, ioHead.headers)
res.end(ioEnd.content, ioEnd.encoding);
}
// TODO: verify if socket.io.js is dynamic or not to cache js.
});
app.get('/', function(req, res) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end('Mustekala v'+mustekala.version+'. Fork it at <a href="'+mustekala.github+'">'+mustekala.github+'</a>');
});
// TODO: Disable on production
app.post('/example/auth', function(req, res) {
var exampleUser = {
id: '12345'
,data: {
name: 'Joseph'
,nickname: 'joe'
}
}
require('request').post({
url: '/mustekala/authentificate'
,body: {
password: mustekala.config.password
,user: exampleUser
}
},function(error, response, body) {
console.log(body);
});
res.end(JSON.stringify({'authKey': '12345'}));
});