From 9105ceff1e14ad7fa561391aafc7690f8f96bd70 Mon Sep 17 00:00:00 2001 From: kemcake Date: Mon, 8 May 2017 20:50:16 -0700 Subject: [PATCH] Update Config vars - Improve docker prod image (#12) --- .eslintrc | 11 +++++++---- Dockerfile-prod | 10 +++++++--- bin/dev-server.js | 17 ----------------- bin/server.js | 19 +++++++++++++++++++ config/environments.config.js | 8 ++++---- config/project.config.js | 15 +++++++++------ package.json | 6 +++--- src/utils/Api.js | 18 +++++++++++------- 8 files changed, 60 insertions(+), 44 deletions(-) delete mode 100644 bin/dev-server.js create mode 100644 bin/server.js diff --git a/.eslintrc b/.eslintrc index 01cc2aa..fe7d8ec 100644 --- a/.eslintrc +++ b/.eslintrc @@ -13,10 +13,13 @@ "browser" : true }, "globals": { - "__DEV__" : false, - "__TEST__" : false, - "__PROD__" : false, - "__COVERAGE__" : false + "__DEV__" : false, + "__TEST__" : false, + "__PROD__" : false, + "__COVERAGE__" : false, + "__SERVER_HOST__" : false, + "__SERVER_PORT__" : false, + "__CORS_PROXY_PORT__" : false }, "rules": { "key-spacing" : 0, diff --git a/Dockerfile-prod b/Dockerfile-prod index 048f0d6..3f3cc3e 100644 --- a/Dockerfile-prod +++ b/Dockerfile-prod @@ -1,4 +1,4 @@ -FROM node:6.9 +FROM bitnami/node ENV TERM=xterm ENV ROOT /var/www/kubeless-ui @@ -9,8 +9,12 @@ RUN mkdir -p $ROOT/dist && \ COPY package.json $ROOT/src/ WORKDIR $ROOT/src -RUN npm install --global yarn + +RUN install_packages apt-transport-https +RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - +RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list +RUN install_packages yarn # build & test COPY . $ROOT/src/ -RUN rm -rf node_modules && yarn install && yarn cache clean +RUN yarn cache clean && npm cache clean && rm -rf /tmp/* diff --git a/bin/dev-server.js b/bin/dev-server.js deleted file mode 100644 index e9dd13c..0000000 --- a/bin/dev-server.js +++ /dev/null @@ -1,17 +0,0 @@ -const project = require('../config/project.config') -const server = require('../server/main') -const debug = require('debug')('app:bin:dev-server') -const corsProxy = require('cors-anywhere') - -corsProxy.createServer({ - // originWhitelist: [], // Allow all origins - // requireHeader: [], - // setHeaders: { }, - // removeHeaders: [] -}).listen(project.cors_proxy_port, project.server_host, () => { - debug(`\n\n ⚠️ CORS proxy running on ${project.server_host}:${project.cors_proxy_port}`) -}) - -server.listen(project.server_port, () => { - debug(`\n\n 💻 Server is now running at ${project.server_host}:${project.server_port}.`) -}) diff --git a/bin/server.js b/bin/server.js new file mode 100644 index 0000000..5514857 --- /dev/null +++ b/bin/server.js @@ -0,0 +1,19 @@ +const config = require('../config/project.config') +const server = require('../server/main') +const debug = require('debug')('app:bin:server') +const corsProxy = require('cors-anywhere') + +if (config.env === 'development') { + corsProxy.createServer({ + // originWhitelist: [], // Allow all origins + // requireHeader: [], + // setHeaders: { }, + // removeHeaders: [] + }).listen(config.cors_proxy_port, config.server_host, () => { + debug(`\n\n ⚠️ CORS proxy running on ${config.server_host}:${config.cors_proxy_port}`) + }) +} + +server.listen(config.server_port, () => { + debug(`\n\n 💻 Server is now running at ${config.server_host}:${config.server_port}.`) +}) diff --git a/config/environments.config.js b/config/environments.config.js index 9485103..6441a88 100644 --- a/config/environments.config.js +++ b/config/environments.config.js @@ -8,9 +8,9 @@ module.exports = { // NOTE: In development, we use an explicit public path when the assets // are served webpack by to fix this issue: // http://stackoverflow.com/questions/34133808/webpack-ots-parsing-error-loading-fonts/34133809#34133809 - // development : (config) => ({ - // compiler_public_path : `/` - // }), + development : (config) => ({ + compiler_public_path : `/` + }), // ====================================================== // Overrides when NODE_ENV === 'production' @@ -19,7 +19,7 @@ module.exports = { compiler_public_path : '/', compiler_fail_on_warning : false, compiler_hash_type : 'chunkhash', - compiler_devtool : null, + compiler_devtool : false, compiler_stats : { chunks : true, chunkModules : true, diff --git a/config/project.config.js b/config/project.config.js index e903979..895b097 100644 --- a/config/project.config.js +++ b/config/project.config.js @@ -78,12 +78,15 @@ config.globals = { 'process.env' : { 'NODE_ENV' : JSON.stringify(config.env) }, - 'NODE_ENV' : config.env, - '__DEV__' : config.env === 'development', - '__PROD__' : config.env === 'production', - '__TEST__' : config.env === 'test', - '__COVERAGE__' : !argv.watch && config.env === 'test', - '__BASENAME__' : JSON.stringify(process.env.BASENAME || '') + 'NODE_ENV' : config.env, + '__DEV__' : config.env === 'development', + '__PROD__' : config.env === 'production', + '__TEST__' : config.env === 'test', + '__COVERAGE__' : !argv.watch && config.env === 'test', + '__BASENAME__' : JSON.stringify(process.env.BASENAME || ''), + '__SERVER_HOST__' : JSON.stringify(config.server_host), + '__SERVER_PORT__' : config.server_port, + '__CORS_PROXY_PORT__' : config.cors_proxy_port } // ------------------------------------ diff --git a/package.json b/package.json index 10c4b7e..9cd1ad0 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ } }, "dev": { - "command": "nodemon bin/dev-server --ignore dist --ignore coverage --ignore tests --ignore src", + "command": "nodemon bin/server --ignore dist --ignore coverage --ignore tests --ignore src", "env": { "NODE_ENV": "development", "DEBUG": "app:*" @@ -57,7 +57,7 @@ } }, "start": { - "command": "node bin/dev-server", + "command": "node bin/server", "env": { "DEBUG": "app:*" } @@ -84,6 +84,7 @@ "babel-plugin-module-alias": "^1.6.0", "babel-plugin-transform-runtime": "^6.15.0", "babel-preset-es2015": "^6.14.0", + "babel-preset-flow": "^6.23.0", "babel-preset-react": "^6.11.1", "babel-preset-stage-0": "^6.3.13", "babel-runtime": "^6.11.6", @@ -134,7 +135,6 @@ "babel-cli": "^6.24.1", "babel-eslint": "^7.2.2", "babel-plugin-istanbul": "^4.1.1", - "babel-preset-flow": "^6.23.0", "cheerio": "^0.22.0", "codecov": "^2.1.0", "enzyme": "^2.8.2", diff --git a/src/utils/Api.js b/src/utils/Api.js index e4373f6..8efbf02 100644 --- a/src/utils/Api.js +++ b/src/utils/Api.js @@ -17,17 +17,21 @@ limitations under the License. import StatusCodes from 'utils/StatusCodes' import Qs from 'qs' import _ from 'lodash' -const CONFIG = { // TODO: take that from config - server_host: 'http://localhost', - cors_proxy_port: '3001' +const CONFIG = { + server_host: __SERVER_HOST__, + cors_proxy_port: __CORS_PROXY_PORT__ } - export default class Api { static apiFetch({ url, method, body, dataUrl, cluster, entity }) { - const { url: URL, headers } = this.updateParams({ url, method, body, dataUrl, cluster, entity }) - const proxiedURL = `${CONFIG.server_host}:${CONFIG.cors_proxy_port}/${encodeURI(URL)}` - return fetch(proxiedURL, { + let { url: URL, headers } = this.updateParams({ url, method, body, dataUrl, cluster, entity }) + + URL = encodeURI(URL) + if (__DEV__) { + URL = `${'http://'}${CONFIG.server_host}:${CONFIG.cors_proxy_port}/${URL}` // proxied url for CORS + } + + return fetch(URL, { method, headers, body: _.isEmpty(body) ? undefined : JSON.stringify(body)