Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: BCGW layer doesn't work for Mobile #3736

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions api/src/paths/proxy/openmaps.ts
meghna0593 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { RequestHandler } from 'express';
import { Operation } from 'express-openapi';
import { ALL_ROLES, SECURITY_ON } from 'constants/misc';
import { getLogger } from 'utils/logger';
import { InvasivesRequest } from 'utils/auth-utils';
const https = require('https');

const defaultLog = getLogger('proxy');

const GET: Operation = [getOpenMapsWMSTiles()];

GET.apiDoc = {
description: 'Proxy requests to a BCGW WMS endpoint',
tags: ['proxy'],
parameters: [
{
name: 'url',
in: 'query',
required: true,
type: 'string',
description: 'The target WMS URL to proxy'
}
],
responses: {
200: {
description: 'Successful response',
schema: {
type: 'string'
}
},
400: {
description: 'Invalid request'
},
500: {
description: 'Internal server error'
},
401: {
$ref: '#/components/responses/401'
},
503: {
$ref: '#/components/responses/503'
},
default: {
$ref: '#/components/responses/default'
}
}
};

/**
* Proxy request to Openmaps from mobile, redirect the response back to the client
*
* @return {RequestHandler}
*/
function getOpenMapsWMSTiles(): RequestHandler {
return async (req, res, next) => {
const targetUrl = req.query.url;

if (!targetUrl) {
return res.status(400).json({
message: 'Missing URL parameter',
request: req.body,
namespace: 'openmaps/url={url}',
code: 400
});
}

https
.get(targetUrl, (apiRes) => {
if (apiRes.statusCode !== 200) {
meghna0593 marked this conversation as resolved.
Show resolved Hide resolved
console.error(`Failed to fetch data: ${apiRes.statusMessage}`);
return res.status(apiRes.statusCode).send(`Error fetching data: ${apiRes.statusMessage}`);
}

// Pass through headers from the OpenMaps API response
res.setHeader('Content-Type', apiRes.headers['content-type']);
res.setHeader('Cache-Control', apiRes.headers['cache-control'] || 'no-cache');

// Pipe the API response directly to the client
apiRes.pipe(res);
})
.on('error', (err) => {
console.error('Request error:', err);
res.status(500).send('Internal Server Error');
});
meghna0593 marked this conversation as resolved.
Show resolved Hide resolved
};
}

export { GET };
3 changes: 2 additions & 1 deletion app/src/UI/Map2/LayerPicker/LpLayers/LpLayers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ const LpLayers = () => {
const WmsLayers = useSelector((state) => state.Map?.simplePickerLayers2);
const KmlLayers = useSelector((state) => state.Map?.serverBoundaries);
const drawnLayers = useSelector((state) => state.Map?.clientBoundaries);
const isAuth = useSelector((state: any) => state.Auth?.authenticated);
meghna0593 marked this conversation as resolved.
Show resolved Hide resolved

return (
<div id="lp-layers">
<h3>
DataBC Layers <TooltipWithIcon tooltipText={WmsTooltip} />
</h3>
<div>
{WmsLayers?.length > 0 && connectedToNetwork ? (
{WmsLayers?.length > 0 && connectedToNetwork && isAuth ? (
<ul className="layerList">
{WmsLayers.map((layer, index) => (
<LpLayersOption
Expand Down
12 changes: 11 additions & 1 deletion app/src/UI/Map2/helpers/map-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const mapInit = (options: MapInitOptions) => {
maxZoom: 24,
zoom: 3,
minZoom: 0,
transformRequest: (url) => {
transformRequest: (url, resourceType) => {
if (url.includes(api_base)) {
return {
url,
Expand All @@ -137,6 +137,16 @@ export const mapInit = (options: MapInitOptions) => {
}
};
}

// const api_local = 'http://localhost:3002';
meghna0593 marked this conversation as resolved.
Show resolved Hide resolved
// to make bcgw layers work on mobile
if (MOBILE && url.includes('openmaps.gov.bc.ca/geo/ows')) {
const proxyUrl = `${api_base}/api/proxy/openmaps?url=${encodeURIComponent(url)}`;
return {
url: proxyUrl
};
}

return {
url
};
Expand Down
Loading