Skip to content

Commit 6e38b54

Browse files
authored
Merge pull request #9 from iolufemi/enhancement/requestLogingOptimization
Enhancement/request loging optimization
2 parents 3b42f92 + 073be49 commit 6e38b54

File tree

21 files changed

+130
-84
lines changed

21 files changed

+130
-84
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
- mongodb
55
- redis-server
66
env:
7-
- MONGOLAB_URL=mongodb://127.0.0.1/snipe RATE_LIMIT=10 REDIS_URL=redis://127.0.0.1/1 SECURE_MODE=true NO_CACHE=no
7+
- MONGOLAB_URL=mongodb://127.0.0.1/snipe LOG_MONGOLAB_URL=mongodb://127.0.0.1/snipelogs RATE_LIMIT=10 REDIS_URL=redis://127.0.0.1/1 SECURE_MODE=true NO_CACHE=no ENFORCE_USER_ID_APP_ID_DEVELOPER_ID=yes
88
addons:
99
apt:
1010
sources:

TODO.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
| Filename | line # | TODO
33
|:------|:------:|:------
44
| app.js | 62 | Write a complete Documentation for this project
5-
| routes/index.js | 291 | Test API versioning
6-
| routes/index.js | 292 | Test rate limiting
7-
| routes/index.js | 293 | Test complete route Loader test
8-
| routes/index.js | 294 | Test _sanitizeRequestUrl middleware function
9-
| routes/index.js | 295 | Test _allRequestData middleware function for default value scenario
10-
| routes/index.js | 296 | Test _enforceUserIdAndAppId middle function for when req.body is an array
11-
| routes/index.js | 297 | Make Log requests testable and write unit tests for it
12-
| routes/index.js | 298 | Develop the route loader into a separate node module to be publish on npm
13-
| routes/index.js | 299 | Develop all services onto separate node module to be publish on npm
145
| config/index.js | 12 | Test for production and development senarios
6+
| models/index.js | 14 | Automatically generate tests with the schema structure
7+
| routes/index.js | 314 | Test API versioning
8+
| routes/index.js | 315 | Test rate limiting
9+
| routes/index.js | 316 | Test complete route Loader test
10+
| routes/index.js | 317 | Test _sanitizeRequestUrl middleware function
11+
| routes/index.js | 318 | Test _allRequestData middleware function for default value scenario
12+
| routes/index.js | 319 | Test _enforceUserIdAndAppId middle function for when req.body is an array
13+
| routes/index.js | 320 | Make Log requests testable and write unit tests for it
14+
| routes/index.js | 321 | Develop the route loader into a separate node module to be publish on npm
15+
| routes/index.js | 322 | Develop all services onto separate node module to be publish on npm
1516
| services/logger/index.js | 36 | Test Error Handler
1617
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs
1718
| services/queue/clock.js | 12 | Use the cron package here https://www.npmjs.com/package/cron for timer

config/development.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
secureMode: process.env.SECURE_MODE || false,
88
secret: process.env.SECRET || 'lakikihdgdfdjjjdgd67264664vdjhjdyncmxuei8336%%^#%gdvdhj????jjhdghduue',
99
mongoURL: process.env.MONGOLAB_URL || 'mongodb://192.168.99.100/snipe',
10+
logMongoURL: process.env.LOG_MONGOLAB_URL || 'mongodb://192.168.99.100/snipelogs',
1011
noFrontendCaching: process.env.NO_CACHE || 'yes',
1112
frontendCacheExpiry: process.env.FRONTEND_CACHE_EXPIRY || '90',
1213
backendCacheExpiry: process.env.BACKEND_CACHE_EXPIRY || '90',
@@ -21,5 +22,5 @@ module.exports = {
2122
queueUIUsername: process.env.QUEUE_UI_USERNAME || 'admin',
2223
queueUIPassword: process.env.QUEUE_UI_PASSWORD || 'password123/',
2324
queueUIPort: process.env.QUEUE_UI_PORT || 3000,
24-
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'yes'
25+
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'no'
2526
};

config/production.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
secureMode: process.env.SECURE_MODE || true,
88
secret: process.env.SECRET || 'lakikihdgdfdjjjdgd67264660okjnbgtrdxswerfgytg373745ei8336%%^#%gdvdhj????jjhdghduue',
99
mongoURL: process.env.MONGOLAB_URL || 'mongodb://192.168.99.100/snipe',
10+
logMongoURL: process.env.LOG_MONGOLAB_URL || 'mongodb://192.168.99.100/snipelogs',
1011
noFrontendCaching: process.env.NO_CACHE || 'no',
1112
frontendCacheExpiry: process.env.FRONTEND_CACHE_EXPIRY || '90',
1213
backendCacheExpiry: process.env.BACKEND_CACHE_EXPIRY || '90',
@@ -21,5 +22,5 @@ module.exports = {
2122
queueUIUsername: process.env.QUEUE_UI_USERNAME || 'admin',
2223
queueUIPassword: process.env.QUEUE_UI_PASSWORD || 'password123/',
2324
queueUIPort: process.env.QUEUE_UI_PORT || 3000,
24-
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'yes'
25+
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'no'
2526
};

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ gulp.task('test', function() {
4545
process.env.SECURE_MODE = true;
4646
process.env.NO_CACHE = 'no';
4747
process.env.NODE_ENV = 'test';
48+
process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID = 'yes';
4849
gulp.src('./test', {read: false})
4950
// `gulp-mocha` needs filepaths so you can't have any plugins before it
5051
.pipe(mocha({

models/RequestLogs.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var db = require('../services/database').mongo;
3+
var db = require('../services/database').logMongo;
44

55
var collection = 'RequestLogs';
66

@@ -29,21 +29,21 @@ var schemaObject = {
2929
default: service
3030
},
3131
body: {
32-
type: db.Schema.Types.Mixed
32+
type: db._mongoose.Schema.Types.Mixed
3333
},
3434
app: {
35-
type: db.Schema.Types.ObjectId,
35+
type: db._mongoose.Schema.Types.ObjectId,
3636
ref: 'Applications'
3737
},
3838
user: {
39-
type: db.Schema.Types.ObjectId,
39+
type: db._mongoose.Schema.Types.ObjectId,
4040
ref: 'Users'
4141
},
4242
device: {
4343
type: 'String'
4444
},
4545
response: {
46-
type: db.Schema.Types.Mixed
46+
type: db._mongoose.Schema.Types.Mixed
4747
},
4848
};
4949

@@ -58,22 +58,22 @@ schemaObject.updatedAt = {
5858
};
5959

6060
schemaObject.owner = {
61-
type: db.Schema.Types.ObjectId,
61+
type: db._mongoose.Schema.Types.ObjectId,
6262
ref: 'Users'
6363
};
6464

6565
schemaObject.createdBy = {
66-
type: db.Schema.Types.ObjectId,
66+
type: db._mongoose.Schema.Types.ObjectId,
6767
ref: 'Users'
6868
};
6969

7070
schemaObject.client = {
71-
type: db.Schema.Types.ObjectId,
71+
type: db._mongoose.Schema.Types.ObjectId,
7272
ref: 'Clients'
7373
};
7474

7575
schemaObject.developer = {
76-
type: db.Schema.Types.ObjectId,
76+
type: db._mongoose.Schema.Types.ObjectId,
7777
ref: 'Users'
7878
};
7979

@@ -83,7 +83,7 @@ schemaObject.tags = {
8383
};
8484

8585
// Let us define our schema
86-
var Schema = db.Schema(schemaObject);
86+
var Schema = new db._mongoose.Schema(schemaObject);
8787

8888
// Index all text for full text search
8989
// MyModel.find({$text: {$search: searchString}})
@@ -149,13 +149,13 @@ Schema.post('validate', function() {
149149
});
150150

151151
Schema.pre('find', function(next) {
152-
debug(this instanceof db.Query); // true
152+
debug(this instanceof db._mongoose.Query); // true
153153
this.start = Date.now();
154154
next();
155155
});
156156

157157
Schema.post('find', function(result) {
158-
debug(this instanceof db.Query); // true
158+
debug(this instanceof db._mongoose.Query); // true
159159
// prints returned documents
160160
debug('find() returned ' + JSON.stringify(result));
161161
// prints number of milliseconds the query took
@@ -184,6 +184,6 @@ Schema.pre('update', function(next) {
184184
});
185185

186186
var Model = db.model(collection, Schema);
187-
Model._mongoose = db;
187+
Model._mongoose = db._mongoose;
188188

189189
module.exports = Model;

models/Trash.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var db = require('../services/database').mongo;
3+
var db = require('../services/database').logMongo;
44

55
var queue = require('../services/queue');
66

@@ -12,7 +12,7 @@ var debug = require('debug')(collection);
1212

1313
var schemaObject = {
1414
data: {
15-
type: db.Schema.Types.Mixed
15+
type: db._mongoose.Schema.Types.Mixed
1616
},
1717
service: {
1818
type: 'String',
@@ -31,22 +31,22 @@ schemaObject.updatedAt = {
3131
};
3232

3333
schemaObject.owner = {
34-
type: db.Schema.Types.ObjectId,
34+
type: db._mongoose.Schema.Types.ObjectId,
3535
ref: 'Users'
3636
};
3737

3838
schemaObject.deletedBy = {
39-
type: db.Schema.Types.ObjectId,
39+
type: db._mongoose.Schema.Types.ObjectId,
4040
ref: 'Users'
4141
};
4242

4343
schemaObject.client = {
44-
type: db.Schema.Types.ObjectId,
44+
type: db._mongoose.Schema.Types.ObjectId,
4545
ref: 'Clients'
4646
};
4747

4848
schemaObject.developer = {
49-
type: db.Schema.Types.ObjectId,
49+
type: db._mongoose.Schema.Types.ObjectId,
5050
ref: 'Users'
5151
};
5252

@@ -56,7 +56,7 @@ schemaObject.tags = {
5656
};
5757

5858
// Let us define our schema
59-
var Schema = db.Schema(schemaObject);
59+
var Schema = new db._mongoose.Schema(schemaObject);
6060

6161
// Index all text for full text search
6262
// MyModel.find({$text: {$search: searchString}})
@@ -122,13 +122,13 @@ Schema.post('validate', function() {
122122
});
123123

124124
Schema.pre('find', function(next) {
125-
debug(this instanceof db.Query); // true
125+
debug(this instanceof db._mongoose.Query); // true
126126
this.start = Date.now();
127127
next();
128128
});
129129

130130
Schema.post('find', function(result) {
131-
debug(this instanceof db.Query); // true
131+
debug(this instanceof db._mongoose.Query); // true
132132
// prints returned documents
133133
debug('find() returned ' + JSON.stringify(result));
134134
// prints number of milliseconds the query took
@@ -153,6 +153,6 @@ Schema.pre('update', function(next) {
153153
});
154154

155155
var Model = db.model(collection, Schema);
156-
Model._mongoose = db;
156+
Model._mongoose = db._mongoose;
157157

158158
module.exports = Model;

models/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ require("fs").readdirSync(normalizedPath).forEach(function(file) {
1111
});
1212

1313
module.exports = models;
14+
// Todo: Automatically generate tests with the schema structure
15+
// Add the option of elasticsearch for API search

routes/index.js

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -107,31 +107,59 @@ router._enforceUserIdAndAppId = function(req,res,next){
107107
var userId = req.param('userId');
108108
var appId = req.param('appId');
109109
var developer = req.param('developer');
110-
if(!userId){
111-
return res.badRequest(false,'No userId parameter was passed in the payload of this request. Please pass a userId.');
112-
}else if(!appId){
113-
return res.badRequest(false,'No appId parameter was passed in the payload of this request. Please pass an appId.');
114-
}else if(!developer){
115-
return res.badRequest(false,'No developer parameter was passed in the payload of this request. Please pass a developer id.');
110+
// The routes after this line will require userid, appid and developer.
111+
if(config.enforceUserIdAppIdDeveloperId === 'yes'){
112+
// Make userId compolsory in every request
113+
if(!userId){
114+
return res.badRequest(false,'No userId parameter was passed in the payload of this request. Please pass a userId.');
115+
}else if(!appId){
116+
return res.badRequest(false,'No appId parameter was passed in the payload of this request. Please pass an appId.');
117+
}else if(!developer){
118+
return res.badRequest(false,'No developer parameter was passed in the payload of this request. Please pass a developer id.');
119+
}else{
120+
req.userId = userId;
121+
req.appId = appId;
122+
req.developer = developer;
123+
if(req.body){
124+
if(req.body && req.body.length){
125+
req.body = _.map(req.body,function(value){
126+
value.client = appId;
127+
value.owner = userId;
128+
value.createdBy = userId;
129+
value.developer = developer;
130+
return value;
131+
});
132+
next();
133+
}else{
134+
req.body.client = appId;
135+
req.body.owner = userId;
136+
req.body.createdBy = userId;
137+
req.body.developer = developer;
138+
next();
139+
}
140+
}
141+
}
116142
}else{
117143
req.userId = userId;
118144
req.appId = appId;
119145
req.developer = developer;
120-
if(req.body.length){
121-
req.body = _.map(req.body,function(value){
122-
value.client = appId;
123-
value.owner = userId;
124-
value.createdBy = userId;
125-
value.developer = developer;
126-
return value;
127-
});
128-
next();
129-
}else{
130-
req.body.client = appId;
131-
req.body.owner = userId;
132-
req.body.createdBy = userId;
133-
req.body.developer = developer;
134-
next();
146+
if(req.body){
147+
if(req.body && req.body.length){
148+
req.body = _.map(req.body,function(value){
149+
value.client = appId;
150+
value.owner = userId;
151+
value.createdBy = userId;
152+
value.developer = developer;
153+
return value;
154+
});
155+
next();
156+
}else{
157+
req.body.client = appId;
158+
req.body.owner = userId;
159+
req.body.createdBy = userId;
160+
req.body.developer = developer;
161+
next();
162+
}
135163
}
136164
}
137165
};
@@ -258,17 +286,12 @@ router.get(config.letsencryptSSLVerificationURL, function(req,res){
258286
res.send(config.letsencryptSSLVerificationBody);
259287
});
260288

261-
router.use('/', initialize);
262-
263-
// Publicly available routes here, IE. routes that should work with out requiring userid, appid and developer.
264289

265290

291+
// Publicly available routes here, IE. routes that should work with out requiring userid, appid and developer.
292+
router.use('/', initialize);
266293

267-
// The routes after this line will require userid, appid and developer.
268-
if(config.enforceUserIdAppIdDeveloperId === 'yes'){
269-
// Make userId compolsory in every request
270-
router.use(router._enforceUserIdAndAppId);
271-
}
294+
router.use(router._enforceUserIdAndAppId);
272295

273296
// Should automatically load routes
274297
// Other routes here

services/database/api.js

Whitespace-only changes.

0 commit comments

Comments
 (0)