forked from microservice-framework/microservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
260 lines (225 loc) · 6.59 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* Process JSON validation and execute tasks.
* Parse request and s
*/
'use strict';
const Validator = require('jsonschema').Validator;
const MongoClient = require('mongodb').MongoClient;
const EventEmitter = require('events').EventEmitter;
const util = require('util');
const PostClass = require('./includes/postClass.js');
const PutClass = require('./includes/putClass.js');
const GetClass = require('./includes/getClass.js');
const DeleteClass = require('./includes/deleteClass.js');
const ValidateClass = require('./includes/validateClass.js');
const SearchClass = require('./includes/searchClass.js');
const AggregateClass = require('./includes/aggregateClass.js');
const OptionsClass = require('./includes/optionsClass.js');
const debugF = require('debug');
const fs = require('fs');
const bind = function(fn, me) { return function() { return fn.apply(me, arguments); }; };
/**
* Constructor of Microservice object.
* .
* settings.mongoUrl = process.env.MONGO_URL;
* settings.mongoTable = process.env.MONGO_TABLE;
* settings.secureKey = process.env.SECURE_KEY;
* settings.fileDir = process.env.FILE_DIR
* settings.schema = process.env.SCHEMA
*/
function Microservice(settings) {
// Use a closure to preserve `this`
var self = this;
self.settings = settings;
self.validate = bind(self.validate, self);
self.get = bind(self.get, self);
self.post = bind(self.post, self);
self.put = bind(self.put, self);
self.delete = bind(self.delete, self);
self.validateJson = bind(self.validateJson, self);
self.search = bind(self.search, self);
self.options = bind(self.options, self);
self.aggregate = bind(self.aggregate, self);
self.mongoDB = false;
if (self.settings.mongoUrl) {
MongoClient.connect(self.settings.mongoUrl, function(err, db) {
if (err) {
self.debug.debug('MongoClient:connect err: %O', err);
self.debug.log('MongoClient:connect failed');
self.emit('error', err);
return;
}
self.mongoDB = db;
self.emit('ready', db);
});
}
}
util.inherits(Microservice, EventEmitter);
/**
* Settings for microservice.
*/
Microservice.prototype.settings = {};
Microservice.prototype.debug = {
debug: debugF('microservice:debug'),
log: debugF('microservice:log')
};
/**
* Validate data by method.
*/
Microservice.prototype.validate = function(method, jsonData, requestDetails, callback) {
var self = this;
let db = false;
if (requestDetails.mongoDatabase) {
db = self.mongoDB.db(requestDetails.mongoDatabase);
} else {
db = self.mongoDB;
}
var Validate = new ValidateClass(db, self.settings, jsonData, requestDetails);
Validate.validate(method, callback);
return Validate;
};
/**
* Process Get request.
*/
Microservice.prototype.get = function(jsonData, requestDetails, callback) {
var self = this;
let db = false;
if (requestDetails.mongoDatabase) {
db = self.mongoDB.db(requestDetails.mongoDatabase);
} else {
db = self.mongoDB;
}
if (arguments.length === 2) {
// v1.3 < version compatibility
callback = requestDetails;
requestDetails = jsonData;
}
var Get = new GetClass(db, self.settings, requestDetails);
Get.process(callback);
return Get;
};
/**
* Process Get request.
*/
Microservice.prototype.post = function(jsonData, requestDetails, callback) {
var self = this;
var errors = self.validateJson(jsonData);
if (true !== errors) {
var error = new Error;
error.message = errors.join();
self.debug.debug('POST:validateJson %O', error);
return callback(error);
}
let db = false;
if (requestDetails.mongoDatabase) {
db = self.mongoDB.db(requestDetails.mongoDatabase);
} else {
db = self.mongoDB;
}
var Post = new PostClass(db,self.settings, jsonData, requestDetails);
Post.process(callback);
return Post;
};
/**
* Process PUT request.
*/
Microservice.prototype.put = function(jsonData, requestDetails, callback) {
var self = this;
let db = false;
if (requestDetails.mongoDatabase) {
db = self.mongoDB.db(requestDetails.mongoDatabase);
} else {
db = self.mongoDB;
}
var Put = new PutClass(db, self.settings, jsonData, requestDetails);
Put.process(callback);
return Put;
};
/**
* Process Get request.
*/
Microservice.prototype.delete = function(jsonData, requestDetails, callback) {
var self = this;
if (arguments.length === 2) {
// v1.3 < version compatibility
callback = requestDetails;
requestDetails = jsonData;
}
let db = false;
if (requestDetails.mongoDatabase) {
db = self.mongoDB.db(requestDetails.mongoDatabase);
} else {
db = self.mongoDB;
}
var Delete = new DeleteClass(db, self.settings, requestDetails);
Delete.process(callback);
return Delete;
};
/**
* Process SEARCH request.
*/
Microservice.prototype.search = function(jsonData, requestDetails, callback) {
var self = this;
let db = false;
if (requestDetails.mongoDatabase) {
db = self.mongoDB.db(requestDetails.mongoDatabase);
} else {
db = self.mongoDB;
}
var Search = new SearchClass(db, self.settings, jsonData, requestDetails);
Search.process(callback);
return Search;
};
/**
* Process OPTIONS request.
*/
Microservice.prototype.options = function(jsonData, requestDetails, callbacks, callback) {
var self = this;
if (arguments.length === 3) {
// v1.3 < version compatibility
callback = callbacks;
callbacks = requestDetails;
requestDetails = jsonData;
}
var Options = new OptionsClass(self.settings, callbacks, requestDetails);
Options.process(callback);
return Options;
};
/**
* Process SEARCH request.
*/
Microservice.prototype.aggregate = function(jsonData, requestDetails, callback) {
var self = this;
let db = false;
if (requestDetails.mongoDatabase) {
db = self.mongoDB.db(requestDetails.mongoDatabase);
} else {
db = self.mongoDB;
}
var Aggregate = new AggregateClass(db, self.settings, jsonData, requestDetails);
Aggregate.process(callback);
return Aggregate;
};
/**
* Process Get request.
*/
Microservice.prototype.validateJson = function(jsonData) {
var self = this;
var v = new Validator();
try {
var schemaTask = JSON.parse(fs.readFileSync('schema/' + self.settings.schema));
} catch (e) {
self.debug.debug('validateJson:Validator %O', e);
throw new Error('Internal error: schema syntax error.');
}
var result = v.validate(jsonData, schemaTask);
if (result.errors.length > 0) {
var errors = [];
for (var errorNum in result.errors) {
errors.push(result.errors[ errorNum ].property + ' ' + result.errors[ errorNum ].message);
}
return errors;
}
return true;
};
module.exports = Microservice;