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

Serve product images from WebApp, so CatalogApi doesn't have to be publicly reachable #74

Merged
merged 3 commits into from
Nov 27, 2023

Conversation

SteveSandersonMS
Copy link
Member

Fixes #31

This will require more collaboration:

  • ... with @Eilon otherwise it will break the HybridApp project. That app is going to need to start knowing the URL to WebApp so it can fetch product images from there, or the Mobile.Bff.Shopping project could be changed to override the PictureUri values on products so it points to WebApp, using the URL pattern /product-images/{id}.
    • The reason it would break is that HybridApp shares the CatalogListItem.razor component. I've introduced a new interface, IProductImageUrlProvider, for which HybridApp can provide an implementation that returns URLs on WebApp.
    • WebApp has an implementation of that interface, but it doesn't need to know absolute URLs because the whole point here is that on the web, you're getting the images from the same origin.
  • ... with the MAUI project (@jamesmontemagno ?). How does it access the product images currently? If it's trying to fetch them directly from item.PictureUri, that will be incorrect because we need to stop assuming that CatalogApi is publicly reachable. Most likely it should be changed to fetch the product images from WebApp instead, using the URL pattern /product-images/{id}.

Given this complexity, I'm not sure we should do this at the last minute, though I do agree it's a change we should make when possible.

@danmoseley
Copy link
Member

danmoseley commented Nov 14, 2023

as this was done from a private fork, I believe you now need to delete that fork (keep clone locally!) then re-fork now this repo is public, push original branch up, then close this PR and create a new one from your new public fork.

@adityamandaleeka
Copy link
Member

@danmoseley It doesn't look like this was done from a private fork to me? Or did something change after you posted that comment?

Comment on lines 106 to 109
app.MapGet("/product-images/{id:int}", async (IHttpClientFactory httpClientFactory, int id) =>
{
var httpClient = httpClientFactory.CreateClient(ProductImageProxyHttpClient);
var imageResponse = await httpClient.GetAsync($"/api/v1/catalog/items/{id}/pic");
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Yep

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh that's very nice. I started by trying to do this with Yarp but didn't discover the MapForwarder method, and got lost in the weeds with LoadFromMemory, trying to call PathSet and all kinds of mess. I did make it work in the end but then it was super unclear how to wire it up to service discovery, so I thought it was better not to introduce all that for this sample.

I'll have another go with MapForwarder. Is there an example of using this with service discovery somewhere? It needs to be able to make an outbound request to http://catalogapi so it needs to use an HttpClient that knows about this.

Copy link
Member

Choose a reason for hiding this comment

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

Is there an example of using this with service discovery somewhere?

Yep! The same app uses the Microsoft.Extensions.ServiceDiscovery.Yarp package as well. See https://github.com/dotnet/aspire/blob/ab931f067195a7af0468c18974b864c51a2963cd/samples/eShopLite/MyFrontend/Program.cs#L9

Copy link
Member Author

Choose a reason for hiding this comment

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

Excellent, thanks. It works perfectly - this PR now uses that.

@danmoseley
Copy link
Member

Ah I saw stevesa: but it's stevesa/

@SteveSandersonMS
Copy link
Member Author

This is now complete, in that WebApp and HybridApp are both updated to get the images via proxies instead of having the client reference URLs on catalog-api, so I'll merge.

I don't know whether the MAUI app needs a similar change to avoid referencing catalog-api directly from the client but if so that can be handled separately (cc @jamesmontemagno).

@SteveSandersonMS
Copy link
Member Author

Looks like merging now requires an approval. @Eilon can you approve this, since you've already been through all this code and wrote part of it?

@SteveSandersonMS SteveSandersonMS merged commit 1e764ba into main Nov 27, 2023
3 checks passed
@SteveSandersonMS SteveSandersonMS deleted the stevesa/proxy-images branch November 27, 2023 17:03
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.

Mixed content warnings when running the web app
6 participants