This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RedisAjaxPush.js
89 lines (73 loc) · 1.85 KB
/
RedisAjaxPush.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
var config = require("./config").config;
var http = require("http");
var url = require("url");
var task_query = require("./TaskQuery");
var RedisListenerPool = require("./RedisListenerPool").RedisListenerPool;
var redis = require("redis");
var console = require("console");
console.log("redis-ajax-push starting up");
global.trace = function (message)
{
if (config.console.trace) {
console.log(message);
}
}
global.newRedisClient = function ()
{
return redis.createClient(
config.redis.port, config.redis.host, config.redis.options
);
}
global.redis_client = global.newRedisClient();
global.redis_client.on("error", function (err) {
console.warn("Global redis client error: " + err);
});
global.redis_client.on("ready", function (err) {
global.trace("Global redis client ready");
});
global.redis_listener_pool = new RedisListenerPool();
global.redisKeyName = function (name)
{
return config.redis.prefix + name;
}
global.sendError = function (response, message)
{
var data = {
'status': 'error',
'error_message': message,
};
sendJSON(response, data);
}
global.sendJSON = function (response, obj)
{
response.write(JSON.stringify(obj));
response.end();
}
function handleRequest (request, response)
{
var parts = url.parse(request.url).pathname.split('/');
var subsystem = parts[1];
var task_id = parts[2];
response.writeHeader(200, {
"Content-Type": "application/json",
"Server": "redis-ajax-push"
});
if (subsystem != 'task') {
global.sendError(response, "Not Found");
return;
}
task_query.handleRequest(task_id, request, response);
}
if (!config.http.ip) {
config.http.ip = "0.0.0.0";
}
http.createServer(handleRequest).listen(
config.http.port,
config.http.ip,
function () {
console.info(
"Listening on http://" + config.http.ip + ":" +
config.http.port
);
}
);