Skip to content

Commit

Permalink
Use externalised Meanie packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Buczynski committed Jul 17, 2016
1 parent 914aa46 commit 36c9440
Show file tree
Hide file tree
Showing 46 changed files with 92 additions and 1,205 deletions.
9 changes: 7 additions & 2 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ rules:
- all
comma-dangle:
- error
- only-multiline
- always-multiline
new-cap:
- error
- newIsCap: true
Expand Down Expand Up @@ -116,6 +116,12 @@ rules:
brace-style:
- error
- stroustrup
no-implicit-coercion:
- error
- boolean: false
no-multiple-empty-lines:
- error
- max: 1
eol-last: error
dot-notation: error
space-infix-ops: error
Expand All @@ -131,5 +137,4 @@ rules:
no-whitespace-before-property: error
no-lonely-if: error
no-var: error
no-implicit-coercion: error
no-console: off
27 changes: 14 additions & 13 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let bodyParser = require('body-parser');
let compression = require('compression');
let serveStatic = require('serve-static');
let cookieParser = require('cookie-parser');
let errorMiddleware = require('meanie-express-error-middleware');
let router = require('./services/router');
let tokens = require('./services/tokens');
let db = require('./services/db');
Expand All @@ -38,9 +39,7 @@ i18n.configure({
locales: I18N_LOCALES,
defaultLocale: I18N_DEFAULT_LOCALE,
objectNotation: true,
api: {
'__': 't'
}
api: {'__': 't'},
});

/**
Expand All @@ -51,13 +50,16 @@ module.exports = function() {
//Initialize express app
let app = express();

//Set locals
app.locals = config;

//Setup database
db(app);

//Setup tokens
tokens.setDefaults({
issuer: TOKEN_DEFAULT_ISSUER,
audience: TOKEN_DEFAULT_AUDIENCE
audience: TOKEN_DEFAULT_AUDIENCE,
});
tokens.register(TOKEN_TYPES);

Expand All @@ -67,29 +69,29 @@ module.exports = function() {
//CORS
app.use(cors({
origin: CORS_ORIGINS,
credentials: true //NOTE: needed for cross domain cookies to work
credentials: true, //NOTE: needed for cross domain cookies to work
}));

//Compression
app.use(compression({
level: 3,
filter(req, res) {
return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
}
},
}));

//Logger
app.use(morgan('dev'));

//Parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: true
extended: true,
}));

//Parse application/json
app.use(bodyParser.json());
app.use(bodyParser.json({
type: 'application/vnd.api+json'
type: 'application/vnd.api+json',
}));

//Add cookie parser middleware
Expand All @@ -106,7 +108,7 @@ module.exports = function() {
if (SERVER_LATENCY) {
let latency = require('express-simulate-latency')({
min: SERVER_LATENCY_MIN,
max: SERVER_LATENCY_MAX
max: SERVER_LATENCY_MAX,
});
app.use(latency);
}
Expand All @@ -117,10 +119,9 @@ module.exports = function() {
//Load router
router(app);

//Create error handling middleware stack with two permanent handlers
ERROR_MIDDLEWARE
.concat(['process', 'send'])
.map(handler => require('./error/middleware/' + handler))
//Create error handling middleware stack
errorMiddleware
.load(ERROR_MIDDLEWARE.concat(['auth-clear-cookie', 'send']))
.forEach(handler => app.use(handler));

//Return express server instance
Expand Down
19 changes: 10 additions & 9 deletions app/auth/auth.ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/
let passport = require('passport');
let moment = require('moment');
let NotAuthenticatedError = require('../error/type/auth/not-authenticated');
let NotAuthorizedError = require('../error/type/auth/not-authorized');
let UserSuspendedError = require('../error/type/auth/user-suspended');
let types = require('meanie-express-error-types');
let NotAuthenticatedError = types.NotAuthenticatedError;
let NotAuthorizedError = types.NotAuthorizedError;
let UserSuspendedError = types.UserSuspendedError;
let tokens = require('../services/tokens');
let config = require('../config');

Expand All @@ -34,7 +35,7 @@ function toCamelCase(str, ucfirst) {
return str
.replace(/_+|\-+/g, ' ')
.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, (match, index) => {
if (+match === 0) {
if (Number(match) === 0) {
return '';
}
return (index === 0 && !ucfirst) ?
Expand All @@ -60,7 +61,7 @@ module.exports = {
forget(req, res) {
res.clearCookie('refreshToken', {
secure: REFRESH_TOKEN_COOKIE_SECURE,
httpOnly: true
httpOnly: true,
});
res.end();
},
Expand Down Expand Up @@ -120,7 +121,7 @@ module.exports = {
res.cookie('refreshToken', refreshToken, {
maxAge: REFRESH_TOKEN_COOKIE_MAX_AGE * 1000, //in ms
secure: REFRESH_TOKEN_COOKIE_SECURE,
httpOnly: true
httpOnly: true,
});
}

Expand Down Expand Up @@ -160,7 +161,7 @@ module.exports = {

//Authenticate now
passport.authenticate('bearer', {
session: false
session: false,
}, (error, user) => {

//Error given?
Expand Down Expand Up @@ -198,12 +199,12 @@ module.exports = {

//Authenticate now
passport.authenticate('bearer', {
session: false
session: false,
}, (error, user) => {
if (user) {
req.me = user;
}
next();
})(req, res, next);
}
},
};
3 changes: 2 additions & 1 deletion app/auth/strategies/bearer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/
let passport = require('passport');
let BearerStrategy = require('passport-http-bearer').Strategy;
let InvalidTokenError = require('../../error/type/client/invalid-token');
let types = require('meanie-express-error-types');
let InvalidTokenError = types.InvalidTokenError;
let tokens = require('../../services/tokens');
let User = require('../../services/user');

Expand Down
5 changes: 3 additions & 2 deletions app/auth/strategies/refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* Dependencies
*/
let passport = require('passport');
let RefreshStrategy = require('../../plugins/passport/refresh-strategy');
let InvalidTokenError = require('../../error/type/client/invalid-token');
let RefreshStrategy = require('meanie-passport-refresh-strategy');
let types = require('meanie-express-error-types');
let InvalidTokenError = types.InvalidTokenError;
let tokens = require('../../services/tokens');
let User = require('../../services/user');

Expand Down
3 changes: 2 additions & 1 deletion app/error/error.ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/**
* Dependencies
*/
let ReportedError = require('./type/reported');
let types = require('meanie-express-error-types');
let ReportedError = types.ReportedError;

/**
* Error controller
Expand Down
18 changes: 13 additions & 5 deletions app/error/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
* from the request.
*/

//Create error middleware stack
let config = require('../config');
let stack = config.ERROR_MIDDLEWARE
.map(handler => require('./middleware/' + handler));
/**
* Dependencies
*/
const errorMiddleware = require('meanie-express-error-middleware');
const config = require('../config');

//Export handler
/**
* Load error middleware stack
*/
const stack = errorMiddleware.load(config.ERROR_MIDDLEWARE);

/**
* Export wrapper handler
*/
module.exports = function(error, req) {

//Must have request specified
Expand Down
50 changes: 0 additions & 50 deletions app/error/middleware/log-to-console.js

This file was deleted.

56 changes: 0 additions & 56 deletions app/error/middleware/log-to-gcloud.js

This file was deleted.

Loading

0 comments on commit 36c9440

Please sign in to comment.