diff --git a/.env.default b/.env.default index 03030dca..0fd3c9d4 100644 --- a/.env.default +++ b/.env.default @@ -9,6 +9,8 @@ OTP_API_KEY= OTP_COUNTRIES= OTP_COUNTRIES_IDS= RW_API= +GFW_API=https://data-api.globalforestwatch.org +GFW_API_KEY= MAPBOX_API_KEY= DOCUMENTS_MINDATE= diff --git a/components/operators-detail/fmus.js b/components/operators-detail/fmus.js index 11d458bd..f9180b60 100644 --- a/components/operators-detail/fmus.js +++ b/components/operators-detail/fmus.js @@ -57,7 +57,6 @@ class OperatorsDetailFMUs extends React.Component { componentDidMount() { const { fmus, fmu, operatorsDetailFmus } = this.props; - if (!operatorsDetailFmus.layersSettings['integrated-alerts']) { this.props.getIntegratedAlertsMetadata(); } diff --git a/modules/operators-detail-fmus.js b/modules/operators-detail-fmus.js index 625f1b2f..7fd253b0 100644 --- a/modules/operators-detail-fmus.js +++ b/modules/operators-detail-fmus.js @@ -228,7 +228,7 @@ function fetchIntegratedAlertsAnalysis(dispatch, getState, data, fmu, type) { gfw_integrated_alerts__date <= '${trimEndDate}' GROUP BY gfw_integrated_alerts__confidence ` - const url = new URL(`${process.env.GFW_API}/dataset/gfw_integrated_alerts/latest/query`); + const url = new URL(`${process.env.GFW_PROXY_API}/dataset/gfw_integrated_alerts/latest/query`); url.searchParams.set('geostore_id', geostoreId); url.searchParams.set('geostore_origin', 'rw'); @@ -283,7 +283,7 @@ const ANALYSIS = { } function fetchZonalAnalysis(geostoreId, startDate, endDate, analysis) { - const url = new URL(`${process.env.GFW_API}/analysis/zonal/${geostoreId}`); + const url = new URL(`${process.env.GFW_PROXY_API}/analysis/zonal/${geostoreId}`); url.searchParams.set('geostore_origin', 'rw'); url.searchParams.set('start_date', startDate); @@ -338,7 +338,9 @@ function fetchAnalysis(dispatch, getState, data, fmu, type) { } }); }) - .catch(error => dispatch({ type: GET_FMU_ANALYSIS_ERROR, payload: { type } })); + .catch(error => { + dispatch({ type: GET_FMU_ANALYSIS_ERROR, payload: { type } }); + }); } // GEOSTORE diff --git a/next.config.js b/next.config.js index 7f6ced8e..c14290a0 100644 --- a/next.config.js +++ b/next.config.js @@ -3,12 +3,13 @@ const { withSentryConfig } = require('@sentry/nextjs'); require('dotenv').config(); const config = { + // only PUBLIC env variables here (accessible on the client side) env: { ENV: process.env.ENV, PORT: process.env.PORT, APP_URL: process.env.APP_URL, RW_API: process.env.RW_API, - GFW_API: process.env.GFW_API, + GFW_PROXY_API: process.env.APP_URL + "/gfw-data-api", OTP_API: process.env.OTP_API, OTP_API_KEY: process.env.OTP_API_KEY, OTP_COUNTRIES: process.env.OTP_COUNTRIES.split(','), @@ -64,7 +65,15 @@ const config = { permanent: true }, ] - } + }, + async rewrites() { + return [ + { + source: "/gfw-data-api/:path*", + destination: "/api/gfw-data/:path*", + }, + ]; + }, /* productionBrowserSourceMaps: true, // for debugging prod build locally */ }; diff --git a/package.json b/package.json index d9a529d4..a37e9389 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "mapbox-gl": "^1.8.1", "moveto": "^1.8.1", "next": "12", + "next-http-proxy-middleware": "^1.2.6", "next-redux-wrapper": "^4.0.1", "normalize.css": "^8.0.1", "prop-types": "^15.8.1", diff --git a/pages/api/gfw-data/[...params].js b/pages/api/gfw-data/[...params].js new file mode 100644 index 00000000..f333259c --- /dev/null +++ b/pages/api/gfw-data/[...params].js @@ -0,0 +1,26 @@ +import httpProxyMiddleware from 'next-http-proxy-middleware'; + +export const config = { + api: { + externalResolver: true, + bodyParser: false, + }, +}; + +export default (req, res) => { + return httpProxyMiddleware(req, res, { + target: process.env.GFW_API, + pathRewrite: [ + { + patternStr: `^/?gfw-data-api`, + replaceStr: '/', + }, + ], + headers: { + 'x-api-key': process.env.GFW_API_KEY, + }, + followRedirects: true, + }).catch(async (error) => { + res.end(error.message); + }); +} diff --git a/services/layers.js b/services/layers.js index 5e44e41b..81516a0c 100644 --- a/services/layers.js +++ b/services/layers.js @@ -1,7 +1,7 @@ import * as Sentry from "@sentry/nextjs"; export function fetchIntegratedAlertsMetadata() { - return fetch(`${process.env.GFW_API}/dataset/gfw_integrated_alerts/latest`, { + return fetch(`${process.env.GFW_PROXY_API}/dataset/gfw_integrated_alerts/latest`, { method: 'GET' }) .then((response) => { diff --git a/yarn.lock b/yarn.lock index 9b93aec1..55295d94 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1014,6 +1014,13 @@ "@types/react" "*" hoist-non-react-statics "^3.3.0" +"@types/http-proxy@1.17.3": + version "1.17.3" + resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.3.tgz#348e1b808ff9585423cb909e9992d89ccdbf4c14" + integrity sha512-wIPqXANye5BbORbuh74exbwNzj+UWCwWyeEFJzUQ7Fq3W2NSAy+7x7nX1fgbEypr2/TdKqpeuxLnXWgzN533/Q== + dependencies: + "@types/node" "*" + "@types/json5@^0.0.29": version "0.0.29" resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" @@ -1026,6 +1033,13 @@ dependencies: "@types/geojson" "*" +"@types/node@*": + version "20.14.12" + resolved "https://registry.yarnpkg.com/@types/node/-/node-20.14.12.tgz#129d7c3a822cb49fc7ff661235f19cfefd422b49" + integrity sha512-r7wNXakLeSsGT0H1AU863vS2wa5wBOK4bWMjZz2wj+8nBx+m5PeIn0k8AloSLpRuiwdRQZwarZqHE4FNArPuJQ== + dependencies: + undici-types "~5.26.4" + "@types/parse-json@^4.0.0": version "4.0.2" resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.2.tgz#5950e50960793055845e956c427fc2b0d70c5239" @@ -2389,7 +2403,7 @@ eventemitter3@^3.1.2: resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7" integrity sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q== -eventemitter3@^4.0.1: +eventemitter3@^4.0.0, eventemitter3@^4.0.1: version "4.0.7" resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f" integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw== @@ -2544,6 +2558,11 @@ follow-redirects@1.5.10: dependencies: debug "=3.1.0" +follow-redirects@^1.0.0: + version "1.15.6" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b" + integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA== + for-each@^0.3.3: version "0.3.3" resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e" @@ -2854,6 +2873,15 @@ http-errors@2.0.0: statuses "2.0.1" toidentifier "1.0.1" +http-proxy@^1.18.1: + version "1.18.1" + resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.18.1.tgz#401541f0534884bbf95260334e72f88ee3976549" + integrity sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ== + dependencies: + eventemitter3 "^4.0.0" + follow-redirects "^1.0.0" + requires-port "^1.0.0" + https-proxy-agent@^5.0.0: version "5.0.1" resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz#c59ef224a04fe8b754f3db0063a25ea30d0005d6" @@ -3535,6 +3563,14 @@ negotiator@0.6.3: resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd" integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg== +next-http-proxy-middleware@^1.2.6: + version "1.2.6" + resolved "https://registry.yarnpkg.com/next-http-proxy-middleware/-/next-http-proxy-middleware-1.2.6.tgz#b954873cb33f519c39312b81b896efb32bb52261" + integrity sha512-vHmtFLeO+HomU4Fx/CoA4MbLnXya1B17yR5qOmpYZqRjzGa17a9dgXh9ONvquSZdMrIn7bUfjoPLxMkYMtKj3Q== + dependencies: + "@types/http-proxy" "1.17.3" + http-proxy "^1.18.1" + next-redux-wrapper@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/next-redux-wrapper/-/next-redux-wrapper-4.0.1.tgz#70f2d83b5bd0fb07ec19e6e70bea6b179d295fd7" @@ -4339,6 +4375,11 @@ regexp.prototype.flags@^1.5.2: es-errors "^1.3.0" set-function-name "^2.0.1" +requires-port@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff" + integrity sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ== + reselect@^4.1.8: version "4.1.8" resolved "https://registry.yarnpkg.com/reselect/-/reselect-4.1.8.tgz#3f5dc671ea168dccdeb3e141236f69f02eaec524" @@ -4875,6 +4916,11 @@ unbox-primitive@^1.0.2: has-symbols "^1.0.3" which-boxed-primitive "^1.0.2" +undici-types@~5.26.4: + version "5.26.5" + resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617" + integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA== + unpipe@1.0.0, unpipe@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"