Skip to content

Commit d3f3046

Browse files
committed
setting up the playground
1 parent 8583f46 commit d3f3046

File tree

14 files changed

+391
-0
lines changed

14 files changed

+391
-0
lines changed

app.js

Whitespace-only changes.

config/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
module.exports = {
3+
env: process.env.NODE_ENV || 'development',
4+
port: process.env.PORT || 3000,
5+
trustProxy: process.env.TRUST_PROXY || 'no',
6+
bugsnagKey: process.env.BUGSNAG_KEY || false,
7+
secureMode: process.env.SECURE_MODE || false
8+
};

gulpfile.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const gulp = require('gulp');
2+
const jshint = require('gulp-jshint');
3+
const stylish = require('jshint-stylish');
4+
5+
gulp.task('lint', function() {
6+
return gulp.src('./lib/*.js')
7+
.pipe(jshint())
8+
.pipe(jshint.reporter(stylish));
9+
});

package.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "snipe",
3+
"version": "0.0.1",
4+
"description": "API service for a freelancer marketplace",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "mocha"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/EnsembleLab/snipe.git"
12+
},
13+
"keywords": [
14+
"marketplace",
15+
"freelance"
16+
],
17+
"author": "Olufemi Olanipekun <[email protected]>",
18+
"license": "GPL-3.0",
19+
"bugs": {
20+
"url": "https://github.com/EnsembleLab/snipe/issues"
21+
},
22+
"homepage": "https://github.com/EnsembleLab/snipe#readme",
23+
"devDependencies": {
24+
"chai": "^3.5.0",
25+
"gulp": "^3.9.1",
26+
"gulp-jshint": "^2.0.4",
27+
"gulp-mocha": "^4.1.0",
28+
"gulp-nodemon": "^2.2.1",
29+
"jshint": "^2.9.4",
30+
"jshint-stylish": "^2.2.1",
31+
"mocha": "^3.2.0"
32+
},
33+
"dependencies": {
34+
"aes-js": "^3.0.0",
35+
"body-parser": "^1.17.1",
36+
"bugsnag": "^1.9.1",
37+
"crypto": "0.0.3",
38+
"debug": "^2.6.3",
39+
"express": "^4.15.2",
40+
"lodash": "^4.17.4",
41+
"mongoose": "^4.9.0",
42+
"q": "^1.4.1",
43+
"randomstring": "^1.1.5",
44+
"util": "^0.10.3",
45+
"winston": "^2.3.1",
46+
"winston-bugsnag": "^2.1.0"
47+
}
48+
}

services/services/logger/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
var log = require('winston');
3+
var config = require('../../config');
4+
var response = require('../response');
5+
var bugsnag = require('bugsnag');
6+
var winstonBugsnag = require('winston-bugsnag');
7+
8+
if(config.env === 'production'){
9+
if(!config.bugsnagKey){
10+
log.add(log.transports.File, { filename: 'app-'+new Date().toDateString().split(' ').join('_')+'.log', level: 'warn'});
11+
log.remove(log.transports.Console);
12+
}else{
13+
bugsnag.register(config.bugsnagKey);
14+
log.add(log.transports.File, { filename: 'app-'+new Date().toDateString().split(' ').join('_')+'.log', level: 'warn'});
15+
log.add(winstonBugsnag,{level: 'warn'});
16+
log.remove(log.transports.Console);
17+
}
18+
}
19+
20+
module.exports = log;
21+
module.exports.errorHandler = function(err, req, res, next){ // jshint ignore:line
22+
response(req, res, next);
23+
log.error(err);
24+
if(err.statusCode === 404){
25+
res.notFound(err);
26+
}else if(err.statusCode === 401){
27+
res.unauthorized(err);
28+
}else if(err.statusCode === 400){
29+
res.badRequest(err);
30+
}else if(err.statusCode === 503){
31+
res.forbidden(err);
32+
}else{
33+
res.serverError(err);
34+
}
35+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
var log = require('../logger');
3+
4+
module.exports = function(data, message){
5+
log.warn('Sending bad request response: ', data, message || 'bad request');
6+
7+
if (data !== undefined && data !== null) {
8+
if(Object.keys(data).length === 0 && JSON.stringify(data) === JSON.stringify({})){
9+
data = data.toString();
10+
}
11+
}
12+
13+
if(data){
14+
this.status(400).json({status: 'error', data: data, message: message ? message : 'bad request'});
15+
}else{
16+
this.status(400).json({status: 'error', message: message ? message : 'bad request'});
17+
}
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
var log = require('../logger');
3+
4+
module.exports = function(data, message){
5+
log.warn('Sending forbidden response: ', data, message || 'forbidden');
6+
7+
if (data !== undefined && data !== null) {
8+
if(Object.keys(data).length === 0 && JSON.stringify(data) === JSON.stringify({})){
9+
data = data.toString();
10+
}
11+
}
12+
13+
if(data){
14+
this.status(503).json({status: 'error', data: data, message: message ? message : 'forbidden'});
15+
}else{
16+
this.status(503).json({status: 'error', message: message ? message : 'forbidden'});
17+
}
18+
};

services/services/response/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
var _ = require('lodash');
3+
4+
module.exports = function(req, res, next){
5+
var responseTypes = {
6+
ok: require('./ok'),
7+
badRequest: require('./badRequest'),
8+
forbidden: require('./forbidden'),
9+
notFound: require('./notFound'),
10+
serverError: require('./serverError'),
11+
unauthorized: require('./unauthorized')
12+
};
13+
14+
res = _.extend(res, responseTypes);
15+
next();
16+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
var log = require('../logger');
3+
4+
module.exports = function(){
5+
log.warn('Sending 404 response: '+'not found');
6+
this.status(404).json({status: 'error', message: 'not found'});
7+
};

services/services/response/ok.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict";
2+
var log = require('../logger');
3+
var config = require('../../config');
4+
var encryption = require('../encryption');
5+
var debug = require('debug')('response');
6+
7+
module.exports = function(data){
8+
debug("sending ok response");
9+
var req = this.req;
10+
var res = this;
11+
12+
if(req.header('x-tag') && req.method === 'POST' && config.secureMode && data){
13+
debug("i want to encrypt");
14+
var key = req.header('x-tag');
15+
debug('our encryption key: ', key);
16+
var text = JSON.stringify(data);
17+
debug("about to call encryption method");
18+
encryption.encrypt(text, key)
19+
.then(function(resp){
20+
debug("got response from encryption method: ",resp);
21+
log.info('Sending ok response: ', data);
22+
res.status(200).json({status: 'success', data: resp, secure: true});
23+
})
24+
.catch(function(err){
25+
debug("got error from encryption method: ", err);
26+
res.serverError(err,'Error encrypting response.');
27+
});
28+
}else{
29+
log.info('Sending ok response: ', data);
30+
if(data){
31+
res.status(200).json({status: 'success', data: data});
32+
}else{
33+
res.status(200).json({status: 'success'});
34+
}
35+
}
36+
};

0 commit comments

Comments
 (0)