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

feat: proxy for mapbox #730

Merged
merged 20 commits into from
Oct 16, 2023
Merged

feat: proxy for mapbox #730

merged 20 commits into from
Oct 16, 2023

Conversation

ap0nia
Copy link
Collaborator

@ap0nia ap0nia commented Oct 12, 2023

Summary

  • Addresses Replace public mapbox token #718
  • Secure the access token on the backend by proxying requests to MapBox.
  • Cache the request for map tiles because that overwhelms the free tier allowance.

@github-actions github-actions bot requested a review from EricPedley October 12, 2023 19:57
@ap0nia ap0nia linked an issue Oct 12, 2023 that may be closed by this pull request
@EricPedley
Copy link
Member

Works on localhost, but on staging, directions work but tiles don't 😭

@EricPedley
Copy link
Member

My only theory rn is that staging didn't actually update the backend when I updated the tile proxy code, so I'm gonna try opening another PR on the same branch to get another fresh staging deployment

@EricPedley
Copy link
Member

Interestingly, now it marks the staging deploy for this PR as In progress as well while #732 is deploying.

@EricPedley
Copy link
Member

image
CORS 💀💀💀💀💀

@EricPedley
Copy link
Member

image
Maybe not CORS 🤔

@EricPedley
Copy link
Member

image
localhost response (13128-2) has a different size than the response from the backend on staging 🤔🤔🤔

@EricPedley
Copy link
Member

image
image
When we're reading the image from mapbox, it's the correct size, but when we send the response, it's 20kb larger????

@EricPedley
Copy link
Member

ok I kinda figured it out. The response is larger because it's base64 encoding the image. I manually verified this with base64 -w 0 and diff utilities on my machine, and had the suspicion this was the case after reading this: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html and then opening both files in a text editor.

@EricPedley
Copy link
Member

So I noticed that the request headers didn't have image/png explicitly under their Accept header, but it turns out we can't even change that via library code or anything because it's done from HTML img elements:
image

@EricPedley
Copy link
Member

The base64 to binary conversion isn't being run as a response integration because we're using a lambda proxy.
image
Apparently not doing this will fix it.
image
But, this is done by default in the component we're using to wrap our lambda in an API gateway :(
image

@ap0nia
Copy link
Collaborator Author

ap0nia commented Oct 14, 2023

FWIW, this is exactly what I was anticipating when you suggested proxying image requests from S3 lol. Dealing with encoding over Lambda is a headache, and why I like to have the browser do it when it fetches directly from S3 :^)

P.S. If it's possible, I'd like to have a solution that doesn't involve editing the devops, because I don't like complexity there 🤓

@EricPedley
Copy link
Member

FWIW, this is exactly what I was anticipating when you suggested proxying image requests from S3 lol. Dealing with encoding over Lambda is a headache, and why I like to have the browser do it when it fetches directly from S3 :^)

This isn't even touching S3 yet, what I'm stuck on is literally just the thinnest proxy possible with no caching on our end.

P.S. If it's possible, I'd like to have a solution that doesn't involve editing the devops, because I don't like complexity there 🤓

I'm not sure this is possible with how the current solution is looking. One thing we could do is swap out our tile provider but keep the directions api integration, but I've tried ~20 tilesets so far and they all are worse than MapBox's. https://wiki.openstreetmap.org/wiki/Raster_tile_providers

Most of them have either not enough detail like the names of the buildings, or too much extraneous overlay info.

@EricPedley
Copy link
Member

IT WORKS NOW!!!!!
image

@EricPedley
Copy link
Member

SO post that did the trick:
https://stackoverflow.com/a/47684868

@EricPedley EricPedley self-requested a review October 15, 2023 22:28
Copy link
Member

@EricPedley EricPedley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ap0nia can you take a look and lmk if it looks good? Since it's your PR I can't request your review (maybe you can self-request?) Worst-case I can just override and merge.

@MinhxNguyen7
Copy link
Member

Holy shit you're actually doing this

Copy link
Collaborator Author

@ap0nia ap0nia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think everything looks reasonable 🅱️

@EricPedley EricPedley merged commit 223238f into main Oct 16, 2023
6 checks passed
@EricPedley EricPedley deleted the mapbox-proxy branch October 16, 2023 03:58
@EricPedley
Copy link
Member

Now we hope and pray that nothing different about prod breaks CORS or anything 🙏

@EricPedley EricPedley mentioned this pull request Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace public mapbox token
3 participants