v3: port static pages, make routes lib more flexible, share middlewares between v2 and v3, port v1, simplify server.js
This commit is contained in:
@@ -167,9 +167,6 @@ module.exports.analytics = { track: function() { }, trackPurchase: function() {
|
||||
* Load nconf and define default configuration values if config.json or ENV vars are not found
|
||||
*/
|
||||
module.exports.setupConfig = function(){
|
||||
IS_PROD = nconf.get('NODE_ENV') === 'production';
|
||||
BASE_URL = nconf.get('BASE_URL');
|
||||
|
||||
if (nconf.get('IS_DEV'))
|
||||
Error.stackTraceLimit = Infinity;
|
||||
if (IS_PROD && nconf.get('NEW_RELIC_ENABLED') === 'true')
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import fs from 'fs';
|
||||
import _ from 'lodash';
|
||||
|
||||
// Wrapper function to handler `async` route handlers that return promises
|
||||
// It takes the async function, execute it and pass any error to next (args[2])
|
||||
let _wrapAsyncFn = fn => (...args) => fn(...args).catch(args[2]);
|
||||
let noop = (req, res, next) => next();
|
||||
|
||||
module.exports.readController = function readController (router, controller) {
|
||||
_.each(controller, (action) => {
|
||||
let {method, url, middlewares = [], handler} = action;
|
||||
|
||||
method = method.toLowerCase();
|
||||
let fn = handler ? _wrapAsyncFn(handler) : noop;
|
||||
|
||||
router[method](url, ...middlewares, fn);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.walkControllers = function walkControllers (router, filePath) {
|
||||
fs
|
||||
.readdirSync(filePath)
|
||||
.forEach(fileName => {
|
||||
if (!fs.statSync(filePath + fileName).isFile()) {
|
||||
walkControllers(router, `${filePath}${fileName}/`);
|
||||
} else if (fileName.match(/\.js$/)) {
|
||||
let controller = require(filePath + fileName); // eslint-disable-line global-require
|
||||
module.exports.readController(router, controller);
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
import nconf from 'nconf';
|
||||
import logger from './logger';
|
||||
import autoinc from 'mongoose-id-autoinc';
|
||||
import mongoose from 'mongoose';
|
||||
import Q from 'q';
|
||||
|
||||
const IS_PROD = nconf.get('IS_PROD');
|
||||
|
||||
// Use Q promises instead of mpromise in mongoose
|
||||
mongoose.Promise = Q.Promise;
|
||||
|
||||
let mongooseOptions = !IS_PROD ? {} : {
|
||||
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
|
||||
server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
|
||||
};
|
||||
let db = mongoose.connect(nconf.get('NODE_DB_URI'), mongooseOptions, (err) => {
|
||||
if (err) throw err;
|
||||
logger.info('Connected with Mongoose.');
|
||||
});
|
||||
|
||||
autoinc.init(db);
|
||||
@@ -0,0 +1,24 @@
|
||||
import passport from 'passport';
|
||||
import nconf from 'nconf';
|
||||
import passportFacebook from 'passport-facebook';
|
||||
|
||||
const FacebookStrategy = passportFacebook.Strategy;
|
||||
|
||||
// Passport session setup.
|
||||
// To support persistent login sessions, Passport needs to be able to
|
||||
// serialize users into and deserialize users out of the session. Typically,
|
||||
// this will be as simple as storing the user ID when serializing, and finding
|
||||
// the user by ID when deserializing. However, since this example does not
|
||||
// have a database of user records, the complete Facebook profile is serialized
|
||||
// and deserialized.
|
||||
passport.serializeUser((user, done) => done(null, user));
|
||||
passport.deserializeUser((obj, done) => done(null, obj));
|
||||
|
||||
// TODO
|
||||
// This auth strategy is no longer used. It's just kept around for auth.js#loginFacebook() (passport._strategies.facebook.userProfile)
|
||||
// The proper fix would be to move to a general OAuth module simply to verify accessTokens
|
||||
passport.use(new FacebookStrategy({
|
||||
clientID: nconf.get('FACEBOOK_KEY'),
|
||||
clientSecret: nconf.get('FACEBOOK_SECRET'),
|
||||
// callbackURL: nconf.get("BASE_URL") + "/auth/facebook/callback"
|
||||
}, (accessToken, refreshToken, profile, done) => done(null, profile)));
|
||||
@@ -1,37 +0,0 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import express from 'express';
|
||||
import _ from 'lodash';
|
||||
|
||||
const CONTROLLERS_PATH = path.join(__dirname, '/../../controllers/api-v3/');
|
||||
let router = express.Router(); // eslint-disable-line babel/new-cap
|
||||
|
||||
// Wrapper function to handler `async` route handlers that return promises
|
||||
// It takes the async function, execute it and pass any error to next (args[2])
|
||||
let _wrapAsyncFn = fn => (...args) => fn(...args).catch(args[2]);
|
||||
let noop = (req, res, next) => next();
|
||||
|
||||
function walkControllers (filePath) {
|
||||
fs
|
||||
.readdirSync(filePath)
|
||||
.forEach(fileName => {
|
||||
if (!fs.statSync(filePath + fileName).isFile()) {
|
||||
walkControllers(`${filePath}${fileName}/`);
|
||||
} else if (fileName.match(/\.js$/)) {
|
||||
let controller = require(filePath + fileName); // eslint-disable-line global-require
|
||||
|
||||
_.each(controller, (action) => {
|
||||
let {method, url, middlewares = [], handler} = action;
|
||||
|
||||
method = method.toLowerCase();
|
||||
let fn = handler ? _wrapAsyncFn(handler) : noop;
|
||||
|
||||
router[method](url, ...middlewares, fn);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
walkControllers(CONTROLLERS_PATH);
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user