-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·143 lines (107 loc) · 4.51 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var q = require('q');
var options = {};
var routerCoreLibrary = require('./lib/router-core.js');
var routerQ= require('./lib/router-queue.js');
var routerCore = {};
var router = {};
module.exports = {
initRouter: function (authenticateUserIdByCookieString, io, dbStr, app) {
options = {
//the routing map is a union of all routers' routing maps
routingMap: {},
authenticateUserIdByCookieString: authenticateUserIdByCookieString,
io: io,
dbStr: dbStr,
app: app
};
app.get('/console', function (req, res) {
options.authenticateUserIdByCookieString(req.headers.cookie).then(
function (userId) {
res.sendfile(__dirname + '/html/console.html');
},
function () {
res.redirect('login');
}
);
});
routerCore = routerCoreLibrary(options);
router = {
middleware: function (req, res, next) {
if(req.method == 'queue'){
console.log('queued routes are not allowed through http');
next();
return;
}
var method = req.method.toLowerCase();
if (!options.routingMap[method] || !options.routingMap[method][req._parsedUrl.pathname]) {
console.log('explicitTestMiddleware NOT RELEVANT');
next();
return;
}
var route = options.routingMap[method][req._parsedUrl.pathname];
console.log('router: executing middleware route: ' + method + "|" + req._parsedUrl.pathname);
var inputDataObject = method == 'get' ? req.query : req.body;
routerCore.executeRoute(route, inputDataObject, routerCore.getCallbackHttp(res), req.headers.cookie);
},
socketHandler: function (cookie, inputData, clientCallback) {
if (!inputData['method']) {
console.log("no method on socket data");
return;
}
if(req.method == 'queue'){
console.log('queued routes are not allowed through socket');
return;
}
var method = inputData['method'].toLowerCase();
if (!options.routingMap[method] || !options.routingMap[method][inputData['url']]) {
console.log('socketHandler NOT RELEVANT');
return;
}
var route = options.routingMap[method][inputData['url']];
console.log('router: executing socketHandler route: ' + method + "|" + inputData['url']);
routerCore.executeRoute(route, inputData, routerCore.getCallbackSocket(clientCallback), cookie);
}
};
io.on('connection', function (socket) {
authenticateUserIdByCookieString(socket.handshake.headers.cookie).then(function (userId) {
if (userId) {
console.log('socket join ' + userId);
socket.join(userId.toString());
}
});
socket.on('http', function (inputData, clientCallback) {
// todo: cookie is not always updated on logout
router.socketHandler(socket.handshake.headers.cookie, inputData, clientCallback);
});
});
app.use(router.middleware);
// return router;
},
registerRoutes: function(routesArray){
routesArray.map(function(route){
routerCore.registerRoute(route);
return route;
});
},
directCall: function (method, path, inputDataObject, userId, sendMessageToUserByUserIdAndJson) {
function directCallCallback(deferred) {
return function (code, data) {
var result = {
code: code,
data: data
};
deferred.resolve(result);
};
}
if(method === 'queue') {
return routerQ.insertToQ(path, inputDataObject, userId);
}
var deferred = q.defer();
var controller = options.routingMap[method][path].controller;
controller(inputDataObject, directCallCallback(deferred), userId, sendMessageToUserByUserIdAndJson);
return deferred.promise;
},
foreverHandleTasks: function(){
routerQ.foreverHandleTasks();
}
};