-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpedis.js
143 lines (131 loc) · 4.98 KB
/
httpedis.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
const nconf = require('nconf');
const Redis = require('ioredis');
const mime = require('mime-types');
const express = require('express');
const bodyParser = require('body-parser');
function HttPRedis(config) {
config = config || {};
const self = this;
self.app = express();
self.app.use(bodyParser.json());
self.app.get('/', (req, res) => {
res.status(200);
res.contentType(mime.lookup('json'));
res.end(JSON.stringify({
status: 'up',
uptime: process.uptime(),
description: "Http wrapper for ioredis"
}, null, 2));
});
const badRequest = (res, req) => {
res.status(400);
res.contentType(mime.lookup('json'));
res.end(JSON.stringify({params: req.params, query: req.query}, null, 2));
};
self.app.get('/:command/:key*', (req, res) => {
const handler = fetchCommandsHandler[req.params.command];
if (!handler) return badRequest(res, req);
handler(res, req);
});
self.app.put('/:command/:key*', (req, res) => {
const handler = updateCommandHandler[req.params.command];
if (!handler) return badRequest(res, req);
handler(res, req);
});
const fetchCommandsHandler = {
keys: (res, req) => {
const pattern = `${req.params.key}${req.params['0']}`;
self.redis.keys(pattern, (err, result) => {
if (err) {
res.status(500);
res.end(err.message);
return;
}
res.status(200);
res.contentType(mime.lookup('json'));
res.end(JSON.stringify(result, null, 2));
})
},
scan: (res, req) => {
const pattern = `${req.params.key}${req.params['0']}`;
self.redis.scan(req.query.cursor || 0, "MATCH", pattern, "COUNT", req.query.count || 100, (err, result) => {
if (err) {
res.status(500);
return res.end(err.message);
}
res.status(200);
res.contentType(mime.lookup('json'));
res.end(JSON.stringify(result && result.length > 1 && result[1] || [], null, 2));
})
},
get: (res, req) => {
const key = `${req.params.key}${req.params['0']}`;
self.redis.get(key, (err, result) => {
if (err) {
res.status(500);
return res.end(err.message);
}
const response = getResponse(key, result);
res.status(response.status);
res.contentType(mime.lookup(response.contentType));
res.end(response.body);
})
}
};
const getResponse = (key, result) => {
if (!result) {
return {status: 404, body: JSON.stringify({status: "NOT_FOUNT", key: key}), contentType: "json"};
}
try {
return {status: 200, body: JSON.stringify(JSON.parse(result), null, 2), contentType: "json"};
} catch (e) {
return {status: 200, body: result, contentType: "text"};
}
};
const updateCommandHandler = {
set: (res, req) => {
const key = `${req.params.key}${req.params['0']}`;
try {
const value = JSON.stringify(req.body);
self.redis.set(key, value, function (err) {
if (err) {
res.status(500);
return res.end(err.message);
}
res.status(200);
res.contentType(mime.lookup('json'));
res.end(JSON.stringify({key: key, value: req.body}, null, 2));
})
} catch (err) {
res.status(400);
res.end(err.message);
}
}
};
self.env = nconf.defaults(config).argv().env();
const getEnv = (variable, _default) => self.env.get(variable) || _default;
const connect = (config) => {
self.env = nconf.defaults(config).argv().env();
const host = getEnv('REDIS_HOST', '127.0.0.1');
const redisPort = getEnv('REDIS_PORT', 6379);
const auth = getEnv('REDIS_AUTH', '');
const db = getEnv('REDIS_DB', 0);
self.redis = new Redis(`redis://:${auth}@${host}:${redisPort}/${db}`);
};
return {
reload: connect,
start: (cb) => {
connect(config);
const port = getEnv('HTTP_PORT', 7369);
self.server = self.app.listen(port, function (err) {
console.log(err || `HTTPEDIS started and listening on ${port}`);
return cb && cb();
})
},
stop: (cb) => self.server.close(function (err) {
console.log(err || `HTTPEDIS stopped`);
return cb && cb();
})
}
}
module.exports = HttPRedis;