Skip to content
This repository was archived by the owner on Apr 11, 2019. It is now read-only.

Commit 2f9fc40

Browse files
committed
Feat: introduce .env file and delete dynamic configuration
1 parent fe403c4 commit 2f9fc40

File tree

11 files changed

+110
-55
lines changed

11 files changed

+110
-55
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BASE_URL='http://localhost:1337/'
2+
API_URL='http://localhost:3000/'
3+
DEBUG=false

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
app/build/
22
app/dist/
33
webpack.config.*.js
4+
webpack.config.js
45
config/

app/src/apolloClient.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import ApolloClient, {
22
createNetworkInterface,
33
addTypeName,
44
} from 'apollo-client';
5-
import { BASE_URL } from 'config'; // eslint-disable-line
65

7-
const url = `${BASE_URL}graphql`;
6+
const baseUrl = process.env.API_URL || 'http://localhost:3000';
7+
const url = `${baseUrl}graphql`;
88

99
const client = new ApolloClient({
1010
networkInterface: createNetworkInterface({

app/src/components/Navbar/tests/__snapshots__/index.test.js.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ exports[`<Navbar /> should render with default props 1`] = `
22
<div>
33
<Header
44
align="center"
5-
colorIndex="neutral"
65
direction="row"
76
justify="between"
87
pad={

app/src/config/index.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

devServer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ var webpack = require('webpack');
33
var WebpackDevServer = require('webpack-dev-server');
44
var config = require('./webpack.config.js');
55
const path = require('path');
6+
const env = require('node-env-file');
67

7-
const PORT = process.env.PORT || 1337;
8-
const IP = process.env.IP || 'localhost';
8+
env(path.join(__dirname, '.env'));
9+
10+
const serverUrl = process.env.BASE_URL || 'http://localhost:1337';
11+
const PORT = serverUrl.match(/\d+/g)[0];
12+
const IP = serverUrl.match(/\w+/g)[1];
913

1014
new WebpackDevServer(webpack(config), {
1115
publicPath: config.output.publicPath,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
"markdown-loader": "^0.1.7",
130130
"minimist": "^1.2.0",
131131
"morgan": "^1.7.0",
132+
"node-env-file": "^0.1.8",
132133
"offline-plugin": "^3.4.2",
133134
"react": "^15.1.0",
134135
"react-addons-css-transition-group": "^15.2.1",
@@ -171,10 +172,10 @@
171172
"css-loader": "^0.23.0",
172173
"eslint": "^3.15.0",
173174
"eslint-config-airbnb": "^14.1.0",
175+
"eslint-import-resolver-webpack": "0.8.0",
174176
"eslint-loader": "^1.1.1",
175177
"eslint-plugin-graphql": "^0.4.0",
176178
"eslint-plugin-import": "^2.2.0",
177-
"eslint-import-resolver-webpack": "0.8.0",
178179
"eslint-plugin-jsx-a11y": "^4.0.0",
179180
"eslint-plugin-react": "^6.9.0",
180181
"expect-jsx": "^2.6.0",

server/app.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
/* eslint-disable */
1+
/* eslint-disable no-console */
22
import express from 'express';
33
import path from 'path';
44
import morgan from 'morgan';
55
import React from 'react';
6+
import env from 'node-env-file';
67
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
78
import { match, RouterContext } from 'react-router';
89
import { ApolloProvider } from 'react-apollo';
910
import { getDataFromTree } from 'react-apollo/server';
1011
import { createNetworkInterface } from 'apollo-client';
11-
import store from '../app/src/store.js';
12-
import { routes } from '../app/src/routes.js';
13-
import { BASE_URL } from '../app/src/config';
12+
import styleSheet from 'styled-components/lib/models/StyleSheet';
13+
import store from '../app/src/store';
14+
import { routes } from '../app/src/routes';
1415
import Html from './utils/Html';
1516
import createApolloClient from './utils/create-apollo-client';
1617
import manifest from './public/manifest.json';
17-
import styleSheet from 'styled-components/lib/models/StyleSheet';
1818

19-
const app = express();
20-
const isDeveloping = process.env.NODE_ENV !== 'production';
19+
env(path.join(__dirname, '..', 'env'));
2120

22-
// Need to set this to your api url
23-
const IP = process.env.IP || 'localhost';
24-
const PORT = process.env.PORT || 1337;
25-
const apiUrl = `${BASE_URL}graphql`;
21+
const app = express();
22+
const serverUrl = process.env.BASE_URL || 'http://localhost:1337';
23+
const apiUrl = process.env.API_URL || 'http://localhost:3000';
24+
const PORT = serverUrl.match(/\d+/g)[0];
25+
const IP = serverUrl.match(/\w+/g)[1];
26+
const graphqlUrl = `${apiUrl}graphql`;
27+
const debug = process.env.DEBUG || false;
2628

27-
app.use(morgan('combined'));
28-
app.use(express.static(__dirname + '/public'));
29+
if (debug) { app.use(morgan('combined')); }
30+
app.use(express.static(path.join(__dirname, '/public')));
2931

3032
app.use((req, res) => {
3133
match({ routes, location: req.url },
@@ -40,7 +42,7 @@ app.use((req, res) => {
4042
const client = createApolloClient({
4143
ssrMode: true,
4244
networkInterface: createNetworkInterface({
43-
uri: apiUrl,
45+
uri: graphqlUrl,
4446
credentials: 'same-origin',
4547
headers: req.headers,
4648
}),
@@ -56,19 +58,19 @@ app.use((req, res) => {
5658
const html = (
5759
<Html
5860
content={content}
59-
scriptHash={manifest["/main.js"]}
60-
vendorHash={manifest["/vendor.js"]}
61-
cssHash={manifest["/main.css"]}
61+
scriptHash={manifest['/main.js']}
62+
vendorHash={manifest['/vendor.js']}
63+
cssHash={manifest['/main.css']}
6264
styles={styles}
6365
state={ctx.store.getState()}
6466
/>
6567
);
6668
res.status(200).send(`<!doctype html>\n${renderToStaticMarkup(html)}`);
67-
}).catch(e => console.error('RENDERING ERROR:', e)); // eslint-disable-line no-console
69+
}).catch(e => console.error('RENDERING ERROR:', e));
6870
} else {
6971
res.status(404).send('Not found');
7072
}
71-
})
73+
});
7274
});
7375

7476
app.listen(PORT, IP, (err) => {
@@ -77,4 +79,3 @@ app.listen(PORT, IP, (err) => {
7779
}
7880
return console.info(`==> 😎 Listening on port ${PORT}. Open http://${IP}:${PORT} in your browser.`);
7981
});
80-
/* eslint-enable */

webpack.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ module.exports = {
8686
components: path.resolve(ROOT_PATH, 'app/src/components'),
8787
containers: path.resolve(ROOT_PATH, 'app/src/containers'),
8888
pages: path.resolve(ROOT_PATH, 'app/src/pages'),
89-
fragments: path.resolve(ROOT_PATH, 'app/src/fragments'),
90-
config: path.resolve(ROOT_PATH, 'app/src/config'),
89+
utils: path.resolve(ROOT_PATH, 'app/src/utils')
9190
},
9291
},
9392
postcss: function () {

webpack.config.prod.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ module.exports = {
100100
components: path.resolve(ROOT_PATH, 'app/src/components'),
101101
containers: path.resolve(ROOT_PATH, 'app/src/containers'),
102102
pages: path.resolve(ROOT_PATH, 'app/src/pages'),
103-
fragments: path.resolve(ROOT_PATH, 'app/src/fragments'),
104-
config: path.resolve(ROOT_PATH, 'app/src/config'),
103+
utils: path.resolve(ROOT_PATH, 'app/src/utils')
105104
},
106105
},
107106
output: {

0 commit comments

Comments
 (0)