-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Conversation
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. |
@danmoseley It doesn't look like this was done from a private fork to me? Or did something change after you posted that comment? |
src/WebApp/Extensions/Extensions.cs
Outdated
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can/should Yarp be used for this? That's what "eshopLite" does: https://github.com/dotnet/aspire/blob/cb54d832ce8328c2ac681b031a8c0e0d0f709a7e/samples/eShopLite/MyFrontend/Program.cs#L31
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Ah I saw stevesa: but it's stevesa/ |
7714cca
to
fa6d920
Compare
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). |
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? |
Fixes #31
This will require more collaboration:
HybridApp
project. That app is going to need to start knowing the URL toWebApp
so it can fetch product images from there, or theMobile.Bff.Shopping
project could be changed to override thePictureUri
values on products so it points toWebApp
, using the URL pattern/product-images/{id}
.HybridApp
shares theCatalogListItem.razor
component. I've introduced a new interface,IProductImageUrlProvider
, for whichHybridApp
can provide an implementation that returns URLs onWebApp
.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.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 fromWebApp
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.