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

Allow multiple domains in CORS in pmtiles serve #191

Open
t0mk opened this issue Oct 26, 2024 · 6 comments
Open

Allow multiple domains in CORS in pmtiles serve #191

t0mk opened this issue Oct 26, 2024 · 6 comments

Comments

@t0mk
Copy link

t0mk commented Oct 26, 2024

When serving tiles, it's sometimes useful to whitelist CORS "access" from more than one domain (but not from everywhere). It would be helpful for me if this was possible with pmtiles.

Since response header Access-Control-Allow-Origin only allows to specify one domain name, I think the server code could check Origin header from request, and if it would be one of the whitelisted origins, it would copy it to Access-Control-Allow-Origin in response.

The whitelisted CORS origins could be listed in the --cors argument, just comma-separated. For example:

pmtiles serve ./ --cors="https://mydomain.com,https://otherdomain.com"

I could contribute this. Would you be interested in taking it in?

@bdon
Copy link
Member

bdon commented Oct 26, 2024

This is intentionally limited to a single origin, because that CORS feature is intended only for development. If you need multiple CORS origins you should use a reverse proxy like Caddy or Nginx that already supports multiple origins via configuration.

@t0mk
Copy link
Author

t0mk commented Oct 28, 2024

@bdon , thanks for answering. You are right, it makes more sense to do CORS handling elsewhere. It's just too handy to only use your Docker container in Google Cloud Run. For me, the least-amount-of-effort solution for multiple-CORS is to extend the code, keep local fork, build and push Docker container and just change the container image name in your GCP walkthrough:
https://docs.protomaps.com/deploy/google-cloud#creating-a-cloud-run-container

Thanks for the work on pmtiles and feel free to close this.

@thisisaaronland
Copy link
Contributor

Just passing this along FYI: CORS support was added to the sfomuseum/go-sfomuseum-pmtiles wrapper that we use to serve pmtiles (using the rs/cors package).

@bdon
Copy link
Member

bdon commented Oct 28, 2024

You are right, it makes more sense to do CORS handling elsewhere. It's just too handy to only use your Docker container in Google Cloud Run.

It seems reasonable to add multiple CORS support via https://github.com/rs/cors, I imagined many use cases that use go-pmtiles are going to use a CDN in front, @t0mk are you accessing the Cloud Run directly from the browser with no caching in between?

@t0mk
Copy link
Author

t0mk commented Oct 29, 2024

It seems reasonable to add multiple CORS support via https://github.com/rs/cors, I imagined many use cases that use go-pmtiles are going to use a CDN in front, @t0mk are you accessing the Cloud Run directly from the browser with no caching in between?

Yes, I access the cloud run service directly. For the time being it's good enough for me, I'm still experimenting - deciding if I should host tiles myself with pmtiles or just pay for Stadia map. I could configure loadbalancer with CDN, but I don't want to use (or even activate) Compute. I think I could configure CORS with the Google Cloud Loadbalancer.

@bdon
Copy link
Member

bdon commented Nov 1, 2024

@t0mk @thisisaaronland here is a quick spike on using rs/cors, which supports multiple origins as well as wildcards.

#192

This does clean up our own server code quite a bit.

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

No branches or pull requests

3 participants