Dynamic API url

This commit is contained in:
Pierre Burgy 2016-10-05 15:15:43 +02:00
parent 36d6b9ff7a
commit 19244cfbfb
9 changed files with 20 additions and 71 deletions

View File

@ -16,7 +16,7 @@ module.exports = {
const settings = _.pick(strapi.config, [ const settings = _.pick(strapi.config, [
'name', 'name',
'version', 'version',
'description' 'description'
]); ]);
this.body = settings; this.body = settings;
@ -26,9 +26,10 @@ module.exports = {
var data = this.request.body; var data = this.request.body;
try { 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; this.body = settingsUpdated.values;
} catch (err) { } catch (err) {
console.log('err', err);
this.status = err && err.status || 400; this.status = err && err.status || 400;
return this.body = { return this.body = {
message: err.msg || 'An error occurred during settings update' message: err.msg || 'An error occurred during settings update'

View File

@ -1,8 +1,8 @@
/** /**
* app.js * app.js
* *
* This is the entry file for the application, only setup and plugin * This is the entry file for the application,
* code. * only setup and plugin code.
*/ */
import { browserHistory } from 'react-router'; import { browserHistory } from 'react-router';
@ -19,11 +19,14 @@ const store = configureStore(initialState, browserHistory);
import App from 'containers/App'; import App from 'containers/App';
import createRoutes from './routes'; 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 // Register the plugin
if (window.Strapi) { if (window.Strapi) {
window.Strapi.registerPlugin({ window.Strapi.registerPlugin({
name: 'Settings Manager', name: 'Settings Manager',
id: 'settings-manager', id: pluginId,
leftMenuLink: { leftMenuLink: {
label: 'Settings Manager', label: 'Settings Manager',
to: '/settings-manager', to: '/settings-manager',
@ -33,7 +36,11 @@ if (window.Strapi) {
}); });
} }
// API
const apiUrl = `${window.Strapi.apiUrl}/${pluginId}`;
// Export store // Export store
export { export {
store, store,
apiUrl,
}; };

View File

@ -37,7 +37,6 @@ class LeftMenu extends React.Component { // eslint-disable-line react/prefer-sta
}]; }];
render() { render() {
const linksElements = this.links.map((link, i) => (<LeftMenuLink key={i} link={link}></LeftMenuLink>)); const linksElements = this.links.map((link, i) => (<LeftMenuLink key={i} link={link}></LeftMenuLink>));
return ( return (

View File

@ -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());
});
});

View File

@ -20,12 +20,13 @@ import {
selectDescription, selectDescription,
selectVersion, selectVersion,
} from 'containers/HomePage/selectors'; } from 'containers/HomePage/selectors';
import { apiUrl } from '../../app';
/** /**
* General Settings request/response handler * General Settings request/response handler
*/ */
export function* getGeneralSettings() { export function* getGeneralSettings() {
const requestURL = 'http://localhost:1337/settingsmanager/settings/general'; const requestURL = `${apiUrl}/settings/general`;
// Call our request helper (see 'utils/request') // Call our request helper (see 'utils/request')
const generalSettings = yield call(request, requestURL); const generalSettings = yield call(request, requestURL);
@ -50,7 +51,7 @@ export function* updateGeneralSettings() {
type: 'general', type: 'general',
}; };
const requestURL = 'http://localhost:1337/settingsmanager/settings'; const requestURL = `${apiUrl}/settings`;
// Call our request helper (see 'utils/request') // Call our request helper (see 'utils/request')
const generalSettings = yield call( const generalSettings = yield call(

View File

@ -8,7 +8,6 @@ const fs = require('fs');
const componentGenerator = require('./component/index.js'); const componentGenerator = require('./component/index.js');
const containerGenerator = require('./container/index.js'); const containerGenerator = require('./container/index.js');
const routeGenerator = require('./route/index.js'); const routeGenerator = require('./route/index.js');
const languageGenerator = require('./language/index.js');
module.exports = (plop) => { module.exports = (plop) => {
plop.setGenerator('component', componentGenerator); plop.setGenerator('component', componentGenerator);

View File

@ -6,7 +6,6 @@ const path = require('path');
const fs = require('fs'); const fs = require('fs');
const webpack = require('webpack'); const webpack = require('webpack');
const logger = require('../../server/logger'); const logger = require('../../server/logger');
const cheerio = require('cheerio');
const pkg = require(path.resolve(process.cwd(), 'package.json')); const pkg = require(path.resolve(process.cwd(), 'package.json'));
const dllPlugin = pkg.dllPlugin; const dllPlugin = pkg.dllPlugin;
const argv = require('minimist')(process.argv.slice(2)); const argv = require('minimist')(process.argv.slice(2));

View File

@ -5,15 +5,9 @@ const logger = require('./logger');
const argv = require('minimist')(process.argv.slice(2)); const argv = require('minimist')(process.argv.slice(2));
const setup = require('./middlewares/frontendMiddleware'); 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 resolve = require('path').resolve;
const app = express(); 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, { setup(app, {
outputPath: resolve(process.cwd(), 'build'), outputPath: resolve(process.cwd(), 'build'),
publicPath: '/', publicPath: '/',
@ -27,17 +21,4 @@ app.listen(port, (err) => {
if (err) { if (err) {
return logger.error(err.message); 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);
}
}); });

View File

@ -1,7 +1,5 @@
/* eslint-disable global-require */ /* eslint-disable global-require */
const express = require('express');
const path = require('path'); const path = require('path');
const compression = require('compression');
const pkg = require(path.resolve(process.cwd(), 'package.json')); const pkg = require(path.resolve(process.cwd(), 'package.json'));
// Dev middleware // 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 * Front-end middleware
*/ */
module.exports = (app, options) => { module.exports = (app) => {
const isProd = process.env.NODE_ENV === 'production'; const webpackConfig = require('../../internals/webpack/webpack.dev.babel');
addDevMiddlewares(app, webpackConfig);
if (isProd) {
addProdMiddlewares(app, options);
} else {
const webpackConfig = require('../../internals/webpack/webpack.dev.babel');
addDevMiddlewares(app, webpackConfig);
}
return app; return app;
}; };