From 19244cfbfbcc14ce0b12b91f22588b0f97e8b963 Mon Sep 17 00:00:00 2001 From: Pierre Burgy Date: Wed, 5 Oct 2016 15:15:43 +0200 Subject: [PATCH] Dynamic API url --- controllers/SettingsManager.js | 5 ++-- public/app/app.js | 15 +++++++--- public/app/components/LeftMenu/index.js | 1 - .../containers/App/tests/selectors.test.js | 16 ----------- public/app/containers/HomePage/sagas.js | 5 ++-- public/internals/generators/index.js | 1 - public/internals/webpack/webpack.dev.babel.js | 1 - public/server/index.js | 19 ------------- .../server/middlewares/frontendMiddleware.js | 28 ++----------------- 9 files changed, 20 insertions(+), 71 deletions(-) delete mode 100644 public/app/containers/App/tests/selectors.test.js diff --git a/controllers/SettingsManager.js b/controllers/SettingsManager.js index 8bf8e219c5..ef75151fdd 100644 --- a/controllers/SettingsManager.js +++ b/controllers/SettingsManager.js @@ -16,7 +16,7 @@ module.exports = { const settings = _.pick(strapi.config, [ 'name', 'version', - 'description' + 'description' ]); this.body = settings; @@ -26,9 +26,10 @@ module.exports = { var data = this.request.body; try { - const settingsUpdated = yield strapi.plugins.settingsmanager.services.settingsservice.configurationsManager(strapi, data); + const settingsUpdated = yield strapi.plugins['settings-manager'].services.settingsservice.configurationsManager(strapi, data); this.body = settingsUpdated.values; } catch (err) { + console.log('err', err); this.status = err && err.status || 400; return this.body = { message: err.msg || 'An error occurred during settings update' diff --git a/public/app/app.js b/public/app/app.js index 41ee7c3f38..01791d491b 100644 --- a/public/app/app.js +++ b/public/app/app.js @@ -1,8 +1,8 @@ /** * app.js * - * This is the entry file for the application, only setup and plugin - * code. + * This is the entry file for the application, + * only setup and plugin code. */ import { browserHistory } from 'react-router'; @@ -19,11 +19,14 @@ const store = configureStore(initialState, browserHistory); import App from 'containers/App'; import createRoutes from './routes'; +// Plugin identifier based on the package.json `name` value +const pluginId = require('../package.json').name.replace(/^strapi-/i, ''); + // Register the plugin if (window.Strapi) { window.Strapi.registerPlugin({ name: 'Settings Manager', - id: 'settings-manager', + id: pluginId, leftMenuLink: { label: 'Settings Manager', to: '/settings-manager', @@ -33,7 +36,11 @@ if (window.Strapi) { }); } +// API +const apiUrl = `${window.Strapi.apiUrl}/${pluginId}`; + // Export store export { store, -}; \ No newline at end of file + apiUrl, +}; diff --git a/public/app/components/LeftMenu/index.js b/public/app/components/LeftMenu/index.js index 94eb251170..8b8bc4cfb6 100644 --- a/public/app/components/LeftMenu/index.js +++ b/public/app/components/LeftMenu/index.js @@ -37,7 +37,6 @@ class LeftMenu extends React.Component { // eslint-disable-line react/prefer-sta }]; render() { - const linksElements = this.links.map((link, i) => ()); return ( diff --git a/public/app/containers/App/tests/selectors.test.js b/public/app/containers/App/tests/selectors.test.js deleted file mode 100644 index cb0e798e47..0000000000 --- a/public/app/containers/App/tests/selectors.test.js +++ /dev/null @@ -1,16 +0,0 @@ -import { fromJS } from 'immutable'; -import expect from 'expect'; - -import { selectLocationState } from 'containers/App/selectors'; - -describe('selectLocationState', () => { - it('should select the route as a plain JS object', () => { - const route = fromJS({ - locationBeforeTransitions: null, - }); - const mockedState = fromJS({ - route, - }); - expect(selectLocationState()(mockedState)).toEqual(route.toJS()); - }); -}); diff --git a/public/app/containers/HomePage/sagas.js b/public/app/containers/HomePage/sagas.js index ffda5d3771..16a7f33e92 100644 --- a/public/app/containers/HomePage/sagas.js +++ b/public/app/containers/HomePage/sagas.js @@ -20,12 +20,13 @@ import { selectDescription, selectVersion, } from 'containers/HomePage/selectors'; +import { apiUrl } from '../../app'; /** * General Settings request/response handler */ export function* getGeneralSettings() { - const requestURL = 'http://localhost:1337/settingsmanager/settings/general'; + const requestURL = `${apiUrl}/settings/general`; // Call our request helper (see 'utils/request') const generalSettings = yield call(request, requestURL); @@ -50,7 +51,7 @@ export function* updateGeneralSettings() { type: 'general', }; - const requestURL = 'http://localhost:1337/settingsmanager/settings'; + const requestURL = `${apiUrl}/settings`; // Call our request helper (see 'utils/request') const generalSettings = yield call( diff --git a/public/internals/generators/index.js b/public/internals/generators/index.js index 7af51f56fe..4fdd4c53ec 100644 --- a/public/internals/generators/index.js +++ b/public/internals/generators/index.js @@ -8,7 +8,6 @@ const fs = require('fs'); const componentGenerator = require('./component/index.js'); const containerGenerator = require('./container/index.js'); const routeGenerator = require('./route/index.js'); -const languageGenerator = require('./language/index.js'); module.exports = (plop) => { plop.setGenerator('component', componentGenerator); diff --git a/public/internals/webpack/webpack.dev.babel.js b/public/internals/webpack/webpack.dev.babel.js index 61612fe331..176cb4b8db 100644 --- a/public/internals/webpack/webpack.dev.babel.js +++ b/public/internals/webpack/webpack.dev.babel.js @@ -6,7 +6,6 @@ const path = require('path'); const fs = require('fs'); const webpack = require('webpack'); const logger = require('../../server/logger'); -const cheerio = require('cheerio'); const pkg = require(path.resolve(process.cwd(), 'package.json')); const dllPlugin = pkg.dllPlugin; const argv = require('minimist')(process.argv.slice(2)); diff --git a/public/server/index.js b/public/server/index.js index f37711526b..53c0f10b51 100644 --- a/public/server/index.js +++ b/public/server/index.js @@ -5,15 +5,9 @@ const logger = require('./logger'); const argv = require('minimist')(process.argv.slice(2)); const setup = require('./middlewares/frontendMiddleware'); -const isDev = process.env.NODE_ENV !== 'production'; -const ngrok = (isDev && process.env.ENABLE_TUNNEL) || argv.tunnel ? require('ngrok') : false; const resolve = require('path').resolve; const app = express(); -// If you need a backend, e.g. an API, add your custom backend-specific middleware here -// app.use('/api', myApi); - -// In production we need to pass these values in instead of relying on webpack setup(app, { outputPath: resolve(process.cwd(), 'build'), publicPath: '/', @@ -27,17 +21,4 @@ app.listen(port, (err) => { if (err) { return logger.error(err.message); } - - // Connect to ngrok in dev mode - if (ngrok) { - ngrok.connect(port, (innerErr, url) => { - if (innerErr) { - return logger.error(innerErr); - } - - logger.appStarted(port, url); - }); - } else { - logger.appStarted(port); - } }); diff --git a/public/server/middlewares/frontendMiddleware.js b/public/server/middlewares/frontendMiddleware.js index 9331e12508..d31e8dfd7a 100644 --- a/public/server/middlewares/frontendMiddleware.js +++ b/public/server/middlewares/frontendMiddleware.js @@ -1,7 +1,5 @@ /* eslint-disable global-require */ -const express = require('express'); const path = require('path'); -const compression = require('compression'); const pkg = require(path.resolve(process.cwd(), 'package.json')); // Dev middleware @@ -42,32 +40,12 @@ const addDevMiddlewares = (app, webpackConfig) => { }); }; -// Production middlewares -const addProdMiddlewares = (app, options) => { - const publicPath = options.publicPath || '/'; - const outputPath = options.outputPath || path.resolve(process.cwd(), 'build'); - - // compression middleware compresses your server responses which makes them - // smaller (applies also to assets). You can read more about that technique - // and other good practices on official Express.js docs http://mxs.is/googmy - app.use(compression()); - app.use(publicPath, express.static(outputPath)); - - app.get('*', (req, res) => res.sendFile(path.resolve(outputPath, 'index.html'))); -}; - /** * Front-end middleware */ -module.exports = (app, options) => { - const isProd = process.env.NODE_ENV === 'production'; - - if (isProd) { - addProdMiddlewares(app, options); - } else { - const webpackConfig = require('../../internals/webpack/webpack.dev.babel'); - addDevMiddlewares(app, webpackConfig); - } +module.exports = (app) => { + const webpackConfig = require('../../internals/webpack/webpack.dev.babel'); + addDevMiddlewares(app, webpackConfig); return app; };