-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (47 loc) · 1.3 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
/* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */
"use strict";
var fs = require('fs');
var util = require('util');
var _ = require(__dirname + '/package.json');
var HTTP_RES_HEADER = {
'Content-Type' : 'text/plain',
'X-Powered-By' : util.format('node.js %s, with %s %s', process.versions.node, _.name, _.version)
};
exports.create = function (options, next) {
var _options = {
'statusurl' : '/status.taobao',
'statusfile' : '',
'setonline' : true,
};
for (var i in options) {
_options[i] = options[i];
}
var _online = _options.setonline;
var _me = {};
_me.online = function () {
_online = true;
};
_me.offline = function () {
_online = false;
};
_me.filter = function (req, res) {
if (!req || !req.url || !res || !res.writeHead || !res.end) {
return;
}
var u = req.url.toString().split('?').shift();
if (_options.statusurl !== u) {
next && next(req, res);
return;
}
if (true === _online && !_options.statusfile) {
res.writeHead(200, HTTP_RES_HEADER);
res.end('');
return;
}
fs.readFile(_options.statusfile, function (error, data) {
res.writeHead((!_online || error) ? 404 : 200, HTTP_RES_HEADER);
res.end('HEAD' === req.method ? '' : data);
});
};
return _me;
};