Skip to content

Commit

Permalink
clean up dev server variables and requests (#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
allyoucanmap authored Dec 21, 2021
1 parent e492931 commit b09c547
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 55 deletions.
4 changes: 4 additions & 0 deletions geonode_mapstore_client/client/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

DEV_SERVER_PROTOCOL=http
DEV_SERVER_HOSTNAME=localhost
DEV_TARGET_GEONODE_HOST=localhost:8000
1 change: 1 addition & 0 deletions geonode_mapstore_client/client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ package-lock.json
yarn.lock
*.code-workspace
docs/
.env
14 changes: 6 additions & 8 deletions geonode_mapstore_client/client/devServer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
const path = require('path');
const fs = require('fs');
const envConfig = fs.existsSync(path.join(__dirname, '.env'))
? require('dotenv').config().parsed
: {};

module.exports = (devServerDefault, projectConfig) => {

const appDirectory = projectConfig.appDirectory;
const envPath = path.resolve(appDirectory, 'env.json');
const envJson = fs.existsSync(envPath) ? require(envPath) : {};
const packageJSON = require(path.resolve(appDirectory, 'package.json')) || {};
const geoNodeProjectConfig = packageJSON.geonode || {};
const devServerOptions = geoNodeProjectConfig.devServer || {};
const devServerHost = devServerOptions.host || envJson.DEV_SERVER_HOST || 'localhost';
const proxyTargetHost = devServerOptions.proxyTargetHost || envJson.DEV_SERVER_PROXY_TARGET_HOST || 'localhost:8000';
const protocol = devServerOptions.protocol || envJson.DEV_SERVER_HOST_PROTOCOL || 'http';
const devServerHost = envConfig.DEV_SERVER_HOSTNAME || 'localhost';
const proxyTargetHost = envConfig.DEV_TARGET_GEONODE_HOST || 'localhost:8000';
const protocol = envConfig.DEV_SERVER_PROTOCOL || 'http';

const proxyTargetURL = `${protocol}://${proxyTargetHost}`;

Expand Down
37 changes: 9 additions & 28 deletions geonode_mapstore_client/client/js/api/geonode/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

import axios from '@mapstore/framework/libs/ajax';
import {
parseDevHostname,
setRequestOptions,
getRequestOptions
parseDevHostname
} from '@js/utils/APIUtils';
import merge from 'lodash/merge';
import mergeWith from 'lodash/mergeWith';
Expand Down Expand Up @@ -62,23 +60,6 @@ function addCountToLabel(name, count) {
return `${name} (${count || 0})`;
}

const requestOptions = (name, requestFunc) => {
const options = getRequestOptions(name);
if (!options) {
return axios.options(parseDevHostname(endpoints[name]))
.then(({ data }) => {
setRequestOptions(name, data);
return requestFunc(data);
})
.catch(() => {
const error = { error: true };
setRequestOptions(name, error);
return requestFunc(error);
});
}
return requestFunc(options);
};

// some fields such as search_fields does not support the array notation `key[]=value1&key[]=value2`
// this function will parse all values included array in the `key=value1&key=value2` format
function addQueryString(requestUrl, params) {
Expand Down Expand Up @@ -153,7 +134,7 @@ export const getResources = ({
.filter(({ id }) => castArray(f || []).indexOf(id) !== -1)
.reduce((acc, filter) => mergeCustomQuery(acc, filter.query || {}), {}) || {};

return requestOptions(RESOURCES, () => axios.get(parseDevHostname(
return axios.get(parseDevHostname(
addQueryString(endpoints[RESOURCES], q && {
search: q,
search_fields: ['title', 'abstract']
Expand All @@ -176,7 +157,7 @@ export const getResources = ({
return resource;
})
};
}));
});
};

export const getMaps = ({
Expand All @@ -186,7 +167,7 @@ export const getMaps = ({
sort,
...params
}) => {
return requestOptions(MAPS, () => axios
return axios
.get(
parseDevHostname(
addQueryString(endpoints[MAPS], q && {
Expand All @@ -211,7 +192,7 @@ export const getMaps = ({
return resource;
})
};
}));
});
};

export const getDatasets = ({
Expand All @@ -221,7 +202,7 @@ export const getDatasets = ({
sort,
...params
}) => {
return requestOptions(DATASETS, () => axios
return axios
.get(
parseDevHostname(
addQueryString(endpoints[DATASETS], q && {
Expand All @@ -246,7 +227,7 @@ export const getDatasets = ({
return resource;
})
};
}));
});
};

export const getDocumentsByDocType = (docType = 'image', {
Expand All @@ -257,7 +238,7 @@ export const getDocumentsByDocType = (docType = 'image', {
...params
}) => {

return requestOptions(MAPS, () => axios
return axios
.get(
parseDevHostname(
addQueryString(endpoints[DOCUMENTS], q && {
Expand All @@ -283,7 +264,7 @@ export const getDocumentsByDocType = (docType = 'image', {
return resource;
})
};
}));
});
};

export const setMapThumbnail = (pk, body) => {
Expand Down
9 changes: 1 addition & 8 deletions geonode_mapstore_client/client/js/utils/APIUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import url from 'url';

let requestOptions = {};

/**
* Utilities for api requests
* @module utils/APIUtils
Expand Down Expand Up @@ -37,11 +35,6 @@ export const parseDevHostname = (requestUrl) => {
return requestUrl;
};

export const setRequestOptions = (name, options) => { requestOptions[name] = options; };
export const getRequestOptions = name => requestOptions[name];

export default {
parseDevHostname,
setRequestOptions,
getRequestOptions
parseDevHostname
};
24 changes: 20 additions & 4 deletions geonode_mapstore_client/client/js/utils/AppUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,23 @@ import url from 'url';
import axios from '@mapstore/framework/libs/ajax';

let actionListeners = {};
// Add a taget url here to fix proxy issue
const targetURL = '';
// Target url here to fix proxy issue
let targetURL = '';
const getTargetUrl = () => {
if (!__DEVTOOLS__) {
return '';
}
if (targetURL) {
return targetURL;
}
const geonodeUrl = getConfigProp('geoNodeSettings')?.geonodeUrl || '';
if (!geonodeUrl) {
return '';
}
const { host, protocol } = url.parse(geonodeUrl);
targetURL = `${protocol}//${host}`;
return targetURL;
};

export function getVersion() {
if (!__DEVTOOLS__) {
Expand Down Expand Up @@ -62,10 +77,11 @@ export function initializeApp() {
}
};
}
if (__DEVTOOLS__ && targetURL && config.url?.match(targetURL)?.[0]) {
const tUrl = getTargetUrl();
if (tUrl && config.url?.match(tUrl)?.[0]) {
return {
...config,
url: config.url.replace(targetURL, '')
url: config.url.replace(tUrl, '')
};
}
return config;
Expand Down
8 changes: 1 addition & 7 deletions geonode_mapstore_client/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"license": "BSD-2-Clause",
"devDependencies": {
"@mapstore/project": "1.0.20",
"dotenv": "10.0.0",
"jsdoc": "3.6.7"
},
"dependencies": {
Expand All @@ -35,13 +36,6 @@
"mapstore": "file:MapStore2",
"react-helmet": "6.1.0"
},
"geonode": {
"devServer": {
"host": "localhost",
"proxyTargetHost": "localhost:8000",
"protocol": "http"
}
},
"mapstore": {
"output": "dist",
"publicPath": "/static/mapstore/dist/",
Expand Down

0 comments on commit b09c547

Please sign in to comment.