From 6af92a4a32dd7b0260208f00b57b9119cf081e78 Mon Sep 17 00:00:00 2001 From: Andrew Nicolaou Date: Thu, 16 Mar 2017 05:34:14 +0100 Subject: [PATCH] Exposes API endpoint URL to client via env variable (#323) --- README.md | 2 ++ client/modules/IDE/actions/files.js | 2 +- client/modules/IDE/actions/preferences.js | 2 +- client/modules/IDE/actions/project.js | 2 +- client/modules/IDE/actions/projects.js | 2 +- client/modules/IDE/actions/uploader.js | 2 +- client/modules/User/actions.js | 2 +- webpack.config.dev.js | 1 + webpack.config.prod.js | 3 ++- 9 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 60668dd6..25c0b965 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ This project is currently in development! It will be announced when there is a ( 5. Create a file called `.env` in the root of this directory that looks like ``` + API_URL=/api MONGO_URL=mongodb://localhost:27017/p5js-web-editor PORT=8000 SESSION_SECRET=whatever_you_want_this_to_be_it_only_matters_for_production @@ -36,6 +37,7 @@ This project is currently in development! It will be announced when there is a ( 5. Create a file called `.env` in the root of this directory that looks like ``` + API_URL=/api MONGO_URL=mongodb://localhost:27017/p5js-web-editor PORT=8000 SESSION_SECRET=make_this_a_long-random_string_like_maybe_126_characters_long diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index d55765d5..2667c718 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -5,7 +5,7 @@ import { reset } from 'redux-form'; import * as ActionTypes from '../../../constants'; import { setUnsavedChanges } from './ide'; -const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api'; +const ROOT_URL = process.env.API_URL; function appendToFilename(filename, string) { const dotIndex = filename.lastIndexOf('.'); diff --git a/client/modules/IDE/actions/preferences.js b/client/modules/IDE/actions/preferences.js index 796f1151..bdc40f76 100644 --- a/client/modules/IDE/actions/preferences.js +++ b/client/modules/IDE/actions/preferences.js @@ -1,7 +1,7 @@ import axios from 'axios'; import * as ActionTypes from '../../../constants'; -const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api'; +const ROOT_URL = process.env.API_URL; function updatePreferences(formParams, dispatch) { axios.put(`${ROOT_URL}/preferences`, formParams, { withCredentials: true }) diff --git a/client/modules/IDE/actions/project.js b/client/modules/IDE/actions/project.js index 20065b71..8054cb14 100644 --- a/client/modules/IDE/actions/project.js +++ b/client/modules/IDE/actions/project.js @@ -8,7 +8,7 @@ import { setUnsavedChanges, resetJustOpenedProject, showErrorModal } from './ide'; -const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api'; +const ROOT_URL = process.env.API_URL; export function setProject(project) { return { diff --git a/client/modules/IDE/actions/projects.js b/client/modules/IDE/actions/projects.js index bb9a0bf4..ee00e40b 100644 --- a/client/modules/IDE/actions/projects.js +++ b/client/modules/IDE/actions/projects.js @@ -3,7 +3,7 @@ import * as ActionTypes from '../../../constants'; import { showErrorModal, setPreviousPath } from './ide'; import { resetProject } from './project'; -const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api'; +const ROOT_URL = process.env.API_URL; export function getProjects(username) { return (dispatch) => { diff --git a/client/modules/IDE/actions/uploader.js b/client/modules/IDE/actions/uploader.js index b8991736..e88eec12 100644 --- a/client/modules/IDE/actions/uploader.js +++ b/client/modules/IDE/actions/uploader.js @@ -3,7 +3,7 @@ import { createFile } from './files'; const textFileRegex = /(text\/|application\/json)/; const s3BucketHttps = `https://s3-us-west-2.amazonaws.com/${process.env.S3_BUCKET}/`; -const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api'; +const ROOT_URL = process.env.API_URL; const MAX_LOCAL_FILE_SIZE = 80000; // bytes, aka 80 KB function localIntercept(file, options = {}) { diff --git a/client/modules/User/actions.js b/client/modules/User/actions.js index 89818ee8..c34c1a59 100644 --- a/client/modules/User/actions.js +++ b/client/modules/User/actions.js @@ -4,7 +4,7 @@ import * as ActionTypes from '../../constants'; import { showErrorModal, justOpenedProject } from '../IDE/actions/ide'; -const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api'; +const ROOT_URL = process.env.API_URL; export function authError(error) { return { diff --git a/webpack.config.dev.js b/webpack.config.dev.js index e8d32bc2..79db4136 100644 --- a/webpack.config.dev.js +++ b/webpack.config.dev.js @@ -34,6 +34,7 @@ module.exports = { }), new webpack.DefinePlugin({ 'process.env': { + API_URL: '"' + process.env.API_URL + '"', CLIENT: JSON.stringify(true), 'NODE_ENV': JSON.stringify('development'), 'S3_BUCKET': '"' + process.env.S3_BUCKET + '"' diff --git a/webpack.config.prod.js b/webpack.config.prod.js index fa68fb91..35adad95 100644 --- a/webpack.config.prod.js +++ b/webpack.config.prod.js @@ -76,6 +76,7 @@ module.exports = { plugins: [ new webpack.DefinePlugin({ 'process.env': { + 'API_URL': '"' + process.env.API_URL + '"', 'NODE_ENV': JSON.stringify('production'), 'S3_BUCKET': '"' + process.env.S3_BUCKET + '"' } @@ -112,4 +113,4 @@ module.exports = { clearMessages: true }) ] -}; \ No newline at end of file +};