Skip to content

Commit

Permalink
Merge pull request #214 from wri/feature/gfw-api-key
Browse files Browse the repository at this point in the history
Proxy GFW API and use API key
  • Loading branch information
tsubik authored Jul 31, 2024
2 parents d2d4e28 + 2557fae commit 8ca1b20
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -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=

Expand Down
1 change: 0 additions & 1 deletion components/operators-detail/fmus.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class OperatorsDetailFMUs extends React.Component {
componentDidMount() {
const { fmus, fmu, operatorsDetailFmus } = this.props;


if (!operatorsDetailFmus.layersSettings['integrated-alerts']) {
this.props.getIntegratedAlertsMetadata();
}
Expand Down
8 changes: 5 additions & 3 deletions modules/operators-detail-fmus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(','),
Expand Down Expand Up @@ -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 */
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions pages/api/gfw-data/[...params].js
Original file line number Diff line number Diff line change
@@ -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);
});
}
2 changes: 1 addition & 1 deletion services/layers.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
48 changes: 47 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,13 @@
"@types/react" "*"
hoist-non-react-statics "^3.3.0"

"@types/[email protected]":
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"
Expand All @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -2544,6 +2558,11 @@ [email protected]:
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"
Expand Down Expand Up @@ -2854,6 +2873,15 @@ [email protected]:
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"
Expand Down Expand Up @@ -3535,6 +3563,14 @@ [email protected]:
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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==

[email protected], unpipe@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"
Expand Down

0 comments on commit 8ca1b20

Please sign in to comment.