Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
},
"extends": "airbnb",
"globals": {
"CLIENT": true,
"CLIENT_CONFIG": true,
"DEVELOPMENT": true,
"SERVER": true,
"assert": true,
"sinon": true,
"webpackIsomorphicTools": true
Expand Down
5 changes: 5 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module.exports = {
cookieMaxAge: 2592000,
cookieName: 'jwt_api_auth_token',

isDeployed: true,
isDevelopment: false,

// The canonical list of enabled apps.
validAppNames,

Expand All @@ -46,6 +49,8 @@ module.exports = {
'apiBase',
'cookieName',
'cookieMaxAge',
'isDeployed',
'isDevelopment',
'startLoginUrl',
],

Expand Down
3 changes: 3 additions & 0 deletions config/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
apiHost: 'https://addons-dev.allizom.org',
amoCDN: 'https://addons-dev-cdn.allizom.org',

isDeployed: false,
isDevelopment: true,

webpackServerHost: '127.0.0.1',
webpackServerPort: 3001,
webpackHost: defer((cfg) => `http://${cfg.webpackServerHost}:${cfg.webpackServerPort}`),
Expand Down
3 changes: 0 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ const coverageReporters = [{
const newWebpackConfig = Object.assign({}, webpackConfigProd, {
plugins: [
new webpack.DefinePlugin({
DEVELOPMENT: false,
CLIENT: true,
SERVER: false,
CLIENT_CONFIG: JSON.stringify(clientConfig),
}),
new webpack.NormalModuleReplacementPlugin(/config$/, 'client-config.js'),
Expand Down
25 changes: 4 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"eslint": "npm run lint",
"lint": "eslint .",
"servertest": "npm run build && better-npm-run servertest",
"start:disco": "better-npm-run start:disco",
"start:search": "better-npm-run start:search",
"start": "npm run version-check && NODE_PATH='./:./src' node bin/server.js",
"start:disco": "NODE_APP_INSTANCE=disco npm start",
"start:search": "NODE_APP_INSTANCE=search npm start",
"test": "better-npm-run test",
"unittest": "better-npm-run unittest",
"unittest:dev": "better-npm-run unittest:dev",
Expand Down Expand Up @@ -68,24 +69,6 @@
"NODE_ENV": "production"
}
},
"start": {
"command": "npm run version-check && node bin/server.js",
"env": {
"NODE_PATH": "./:./src"
}
},
"start:search": {
"command": "better-npm-run start",
"env": {
"NODE_APP_INSTANCE": "search"
}
},
"start:disco": {
"command": "better-npm-run start",
"env": {
"NODE_APP_INSTANCE": "disco"
}
},
"test": {
"command": "npm run version-check && npm run unittest && npm run servertest && npm run lint",
"env": {
Expand Down Expand Up @@ -127,9 +110,9 @@
"homepage": "https://github.com/mozillla/addons-frontend#readme",
"dependencies": {
"better-npm-run": "0.0.8",
"bunyan": "1.8.1",
"camelcase": "2.1.1",
"classnames": "2.2.5",
"common-tags": "0.0.3",
"config": "1.20.1",
"express": "4.13.4",
"extract-text-webpack-plugin": "1.0.1",
Expand Down
50 changes: 29 additions & 21 deletions src/core/server/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cookie from 'react-cookie';
import React from 'react';
import ReactDOM from 'react-dom/server';
import ReactHelmet from 'react-helmet';
import bunyan from 'bunyan';

import { Provider } from 'react-redux';
import { match } from 'react-router';
Expand All @@ -19,20 +20,32 @@ import config from 'config';
import { setJWT } from 'core/actions';

const env = config.util.getEnv('NODE_ENV');
const isDeployed = ['stage', 'dev', 'production'].indexOf(env) > -1;
const isDeployed = config.get('isDeployed');
const isDevelopment = config.get('isDevelopment');

const errorString = 'Internal Server Error';
const appName = config.get('appName');

// Globals (these are set by definePlugin for client-side builds).
global.CLIENT = false;
global.SERVER = true;
global.DEVELOPMENT = env === 'development';
const log = bunyan.createLogger({
name: 'server',
app: appName,
serializers: bunyan.stdSerializers,
});

function logRequests(req, res, next) {
const start = new Date();
next();
const finish = new Date();
const elapsed = finish - start;
log.info({req, res, start, finish, elapsed});
}

function baseServer(routes, createStore, { appInstanceName = appName } = {}) {
const app = new Express();
app.disable('x-powered-by');

app.use(logRequests);

// Sets X-Frame-Options
app.use(helmet.frameguard('deny'));

Expand All @@ -45,8 +58,8 @@ function baseServer(routes, createStore, { appInstanceName = appName } = {}) {
// CSP configuration.
app.use(helmet.contentSecurityPolicy(config.get('CSP')));

if (env === 'development') {
console.log('Running in Development Mode'); // eslint-disable-line no-console
if (isDevelopment) {
log.info('Running in Development Mode');

// clear require() cache if in development mode
webpackIsomorphicTools.refresh();
Expand All @@ -67,7 +80,7 @@ function baseServer(routes, createStore, { appInstanceName = appName } = {}) {
cookie.plugToRequest(req, res);

if (err) {
console.error(err); // eslint-disable-line no-console
log.error({err, req});
return res.status(500).end(errorString);
}

Expand Down Expand Up @@ -109,17 +122,15 @@ function baseServer(routes, createStore, { appInstanceName = appName } = {}) {
res.send(`<!DOCTYPE html>${HTML}`);
})
.catch((error) => {
// eslint-disable-next-line no-console
console.error(error.stack);
log.error({err: error});
res.status(500).end(errorString);
});
});
});

// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
// eslint-disable-next-line no-console
console.error(err.stack);
log.error({err});
res.status(500).end(errorString);
});

Expand All @@ -128,16 +139,15 @@ function baseServer(routes, createStore, { appInstanceName = appName } = {}) {

export function runServer({listen = true, app = appName} = {}) {
if (!app) {
// eslint-disable-next-line no-console
console.log(`Please specify a valid appName from ${config.get('validAppNames')}`);
log.fatal(`Please specify a valid appName from ${config.get('validAppNames')}`);
process.exit(1);
}

const port = config.get('serverPort');
const host = config.get('serverHost');

const isoMorphicServer = new WebpackIsomorphicTools(WebpackIsomorphicToolsConfig);
return isoMorphicServer.development(env === 'development')
return isoMorphicServer.development(isDevelopment)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So much nicer 👍

.server(config.get('basePath'))
.then(() => {
global.webpackIsomorphicTools = isoMorphicServer;
Expand All @@ -154,10 +164,9 @@ export function runServer({listen = true, app = appName} = {}) {
if (err) {
reject(err);
}
// eslint-disable-next-line no-console
console.log(`🔥 Addons-frontend server is running [ENV:${env}] [APP:${app}]`);
// eslint-disable-next-line no-console
console.log(`👁 Open your browser at http://${host}:${port} to view it.`);
log.info(`🔥 Addons-frontend server is running [ENV:${env}] [APP:${app}] ` +
`[isDevelopment:${isDevelopment}] [isDeployed:${isDeployed}]`);
log.info(`👁 Open your browser at http://${host}:${port} to view it.`);
resolve(server);
});
} else {
Expand All @@ -166,8 +175,7 @@ export function runServer({listen = true, app = appName} = {}) {
});
})
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err);
log.error({err});
});
}

Expand Down
10 changes: 10 additions & 0 deletions src/core/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { applyMiddleware } from 'redux';
import createLogger from 'redux-logger';
import config from 'config';

export function middleware({ __config = config } = {}) {
if (__config.get('isDevelopment')) {
return applyMiddleware(createLogger());
}
return undefined;
}
6 changes: 3 additions & 3 deletions src/disco/store.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { applyMiddleware, createStore as _createStore, combineReducers } from 'redux';
import { createStore as _createStore, combineReducers } from 'redux';
import { reducer as reduxAsyncConnect } from 'redux-async-connect';
import createLogger from 'redux-logger';
import { middleware } from 'core/store';

export default function createStore(initialState = {}) {
return _createStore(
combineReducers({reduxAsyncConnect}),
initialState,
applyMiddleware(createLogger()),
middleware(),
);
}
6 changes: 3 additions & 3 deletions src/search/store.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { applyMiddleware, createStore as _createStore, combineReducers } from 'redux';
import { createStore as _createStore, combineReducers } from 'redux';
import { reducer as reduxAsyncConnect } from 'redux-async-connect';
import createLogger from 'redux-logger';

import addons from 'core/reducers/addons';
import api from 'core/reducers/api';
import auth from 'core/reducers/authentication';
import search from 'search/reducers/search';
import { middleware } from 'core/store';

export default function createStore(initialState = {}) {
return _createStore(
combineReducers({addons, api, auth, search, reduxAsyncConnect}),
initialState,
applyMiddleware(createLogger()),
middleware(),
);
}
21 changes: 21 additions & 0 deletions tests/client/core/test_store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { middleware } from 'core/store';

describe('core store middleware', () => {
it('includes the middleware in development', () => {
const config = {
get() {
return true;
},
};
assert.isFunction(middleware({__config: config}));
});

it('is undefined when not in development', () => {
const config = {
get() {
return false;
},
};
assert.strictEqual(undefined, middleware({__config: config}));
});
});
3 changes: 0 additions & 3 deletions webpack.dev.config.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ export default Object.assign({}, webpackConfig, {
},
plugins: [
new webpack.DefinePlugin({
CLIENT: true,
CLIENT_CONFIG: JSON.stringify(clientConfig),
DEVELOPMENT: true,
SERVER: false,
}),
new webpack.NormalModuleReplacementPlugin(/config$/, 'client-config.js'),
new webpack.HotModuleReplacementPlugin(),
Expand Down
3 changes: 0 additions & 3 deletions webpack.prod.config.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ export default {
},
plugins: [
new webpack.DefinePlugin({
DEVELOPMENT: false,
CLIENT: true,
SERVER: false,
CLIENT_CONFIG: JSON.stringify(clientConfig),
}),
// Replaces server config module with the subset clientConfig object.
Expand Down