-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·202 lines (173 loc) · 5.34 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* index.js: Skyline REST API boilerplate
*
*/
var cluster = require('cluster');
var restify = require('restify');
var util = require('util');
var iutil = require('island-util');
var fs = require('fs');
var Step = require('step');
var _ = require('underscore');
var _s = require('underscore.string');
var db = require('mongish');
var zmq = require('zmq');
var createInterface = exports.createInterface = function (opts, cb) {
cb = cb || function(){};
var clusterFrontPort = 'ipc:///tmp/cluster';
var clusterBackPort = 'ipc:///tmp/queue';
if (cluster.isMaster) {
var cpus = require('os').cpus().length;
for (var i = 0; i < cpus; ++i) {
cluster.fork();
}
cluster.on('exit', function (worker) {
util.log('Worker ' + worker.id + ' died');
cluster.fork();
});
// Optionally create a worker client for the interface.
var client;
if (opts.workerClient) {
client = opts.workerClient(process.pid);
client.on('message', function (data) {
// Inform dealer.
queue.send(data);
});
}
// Create a router and dealer for cluster messages.
var clusterFrontSock = zmq.socket('router');
var clusterBackSock = zmq.socket('dealer');
clusterFrontSock.identity = 'cr' + process.pid;
clusterBackSock.identity = 'cd' + process.pid;
// Router handling.
clusterFrontSock.bindSync(clusterFrontPort);
clusterFrontSock.on('message', function () {
// Inform server.
clusterBackSock.send(Array.prototype.slice.call(arguments));
});
// Dealer handling.
clusterBackSock.bindSync(clusterBackPort);
clusterBackSock.on('message', function (id, del, data) {
// Send message receipt to appropriate client.
clusterFrontSock.send(Array.prototype.slice.call(arguments));
});
// Create a queue for cluster messages.
var queue = zmq.socket('rep');
queue.identity = 'cq' + process.pid;
// Queue handling.
queue.on('message', function (data) {
if (client) {
// Forward message with client.
client.send(data);
} else {
// Inform dealer.
queue.send(data);
}
});
// Connect to router.
queue.connect(clusterBackPort, function (err) {
if (err) throw err;
});
} else {
// Create a client for cluster messages.
var sock = zmq.socket('req');
sock.identity = 'cc' + process.pid;
sock.connect(clusterFrontPort);
// Create server.
var server = restify.createServer();
server.use(restify.acceptParser(server.acceptable));
server.use(restify.authorizationParser());
server.use(restify.dateParser());
server.use(restify.queryParser());
server.use(restify.jsonp());
server.use(restify.gzipResponse());
server.use(restify.bodyParser());
server.use(restify.throttle({burst: 100, rate: 50, ip: true,
overrides: {
'192.168.1.1': {rate: 0, burst: 0}
}
}));
server.use(restify.conditionalRequest());
if (_.isArray(opts.allowOrigins)) {
server.use(restify.CORS({'origins': opts.allowOrigins}));
}
// Healthcheck
server.get('/__hc__', function (req, res, next) {
res.send();
next();
});
Step(
function () {
// Open DB connection if URI is present in opts.
if (opts.mongoURI) {
new db.Connection(opts.mongoURI, {ensureIndexes: opts.indexDb &&
cluster.worker.id === 1}, this);
} else {
this();
}
},
function (err, connection) {
if (err) return this(err);
// Init collections.
if (!connection || _.size(opts.collections) === 0) {
return this();
}
_.each(opts.collections, _.bind(function (c, name) {
connection.add(name, c, this.parallel());
}, this));
},
function (err) {
if (err) return this(err);
// Init resources.
if (_.size(opts.resources) === 0) {
return this();
}
_.each(opts.resources, _.bind(function (r, name) {
var tmp = new r[_s.capitalize(name)]({sock: sock, server: server,
db: db, config: opts}).init(this.parallel());
}, this));
},
function (err) {
if (err) return cb(err);
// Start server.
server.listen(opts.port, function () {
if (cluster.worker.id === 1) {
util.log('REST API listening on port ' + opts.port);
}
});
cb(null, server);
}
);
}
}
// Parent class for resources (not to be used directly)
var Resource = exports.Resource = function (opts) {
this.sock = opts.sock;
this.server = opts.server;
this.db = opts.db;
this.config = opts.config;
// Handle socket callbacks.
if (this.sock) {
this.callbacks = {};
this.sock.on('message', _.bind(function (data) {
data = JSON.parse(data.toString());
var __cb = data.__cb;
delete data.__cb;
this.callbacks[__cb](data.msg || data.error);
delete this.callbacks[__cb];
}, this));
// Send over socket w/callback support.
this.send = _.bind(function (msg, cb) {
var __cb = iutil.createId_32();
this.callbacks[__cb] = cb;
this.sock.send(JSON.stringify({__cb: __cb, msg: msg}));
}, this);
}
}
Resource.prototype.init = function (cb) {
this.routes();
cb(null, this);
}
Resource.prototype.routes = function () {
return this;
}