Browse Source

Finished purchase-bff implementation and ocelot rerouting

pull/565/head^2
Eduard Tomàs 7 years ago
parent
commit
704e6ae693
10 changed files with 108 additions and 48 deletions
  1. +0
    -3
      docker-compose.override.yml
  2. +9
    -4
      src/Apigw/OcelotApiGw/Program.cs
  3. +75
    -23
      src/Apigw/OcelotApiGw/configuration/configuration.json
  4. +5
    -0
      src/BuildingBlocks/Resilience/Resilience.Http/ResilientHttpClient.cs
  5. +5
    -0
      src/BuildingBlocks/Resilience/Resilience.Http/StandardHttpClient.cs
  6. +4
    -0
      src/Services/Basket/Basket.API/Controllers/BasketController.cs
  7. +0
    -3
      src/Web/WebMVC/AppSettings.cs
  8. +8
    -13
      src/Web/WebMVC/Services/BasketService.cs
  9. +1
    -1
      src/Web/WebMVC/Services/CatalogService.cs
  10. +1
    -1
      src/Web/WebMVC/Services/OrderingService.cs

+ 0
- 3
docker-compose.override.yml View File

@ -134,9 +134,6 @@ services:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://0.0.0.0:80
- CatalogUrl=http://apigw/purchase-bff/catalog
- OrderingUrl=http://ordering.api
- BasketUrl=http://basket.api
- PurchaseUrl=http://apigw/purchase-bff
- LocationsUrl=http://locations.api
- IdentityUrl=http://10.0.75.1:5105 # Local Mac: Use http://docker.for.mac.localhost:5105 || Local Windows: Use 10.0.75.1 in a "Docker for Windows" environment, if using "localhost" from browser. || #Remote access: Use ${ESHOP_EXTERNAL_DNS_NAME_OR_IP} if using external IP or DNS name from browser.


+ 9
- 4
src/Apigw/OcelotApiGw/Program.cs View File

@ -7,6 +7,7 @@ using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
namespace OcelotApiGw
{
@ -17,10 +18,14 @@ namespace OcelotApiGw
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
public static IWebHost BuildWebHost(string[] args)
{
var builder = WebHost.CreateDefaultBuilder(args);
builder.ConfigureServices(s => s.AddSingleton(builder))
.ConfigureAppConfiguration(ic => ic.AddJsonFile(Path.Combine("configuration", "configuration.json")))
.UseStartup<Startup>()
.Build();
.UseStartup<Startup>();
var host = builder.Build();
return host;
}
}
}

+ 75
- 23
src/Apigw/OcelotApiGw/configuration/configuration.json View File

@ -1,20 +1,44 @@
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamPathTemplate": "/api/{version}/{everything}",
"DownstreamScheme": "http",
"DownstreamHost": "catalog.api",
"DownstreamPort": 80,
"UpstreamPathTemplate": "/purchase-bff/catalog/{everything}",
"DownstreamHostAndPorts": [
{
"Host": "catalog.api",
"Port": 80
}
],
"UpstreamPathTemplate": "/purchase-bff/api/{version}/c/{everything}",
"UpstreamHttpMethod": [ "GET" ]
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamPathTemplate": "/api/{version}/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "basket.api",
"Port": 80
}
],
"UpstreamPathTemplate": "/purchase-bff/api/{version}/b/{everything}",
"UpstreamHttpMethod": [],
"AuthenticationOptions": {
"AuthenticationProviderKey": "IdentityApiKey",
"AllowedScopes": []
}
},
{
"DownstreamPathTemplate": "/api/{version}/{everything}",
"DownstreamScheme": "http",
"DownstreamHost": "basket.api",
"DownstreamPort": 80,
"UpstreamPathTemplate": "/purchase-bff/basket/{everything}",
"UpstreamHttpMethod": [ "GET" ],
"DownstreamHostAndPorts": [
{
"Host": "ordering.api",
"Port": 80
}
],
"UpstreamPathTemplate": "/purchase-bff/api/{version}/o/{everything}",
"UpstreamHttpMethod": [],
"AuthenticationOptions": {
"AuthenticationProviderKey": "IdentityApiKey",
"AllowedScopes": []
@ -23,8 +47,12 @@
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHost": "purchasebff",
"DownstreamPort": 80,
"DownstreamHostAndPorts": [
{
"Host": "purchasebff",
"Port": 80
}
],
"UpstreamPathTemplate": "/purchase-bff/{everything}",
"UpstreamHttpMethod": [ "POST", "PUT", "GET" ],
"AuthenticationOptions": {
@ -35,48 +63,72 @@
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHost": "ordering.api",
"DownstreamPort": 80,
"DownstreamHostAndPorts": [
{
"Host": "ordering.api",
"Port": 80
}
],
"UpstreamPathTemplate": "/orders-api/{everything}",
"UpstreamHttpMethod": []
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHost": "basket.api",
"DownstreamPort": 80,
"DownstreamHostAndPorts": [
{
"Host": "basket.api",
"Port": 80
}
],
"UpstreamPathTemplate": "/basket-api/{everything}",
"UpstreamHttpMethod": []
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHost": "catalog.api",
"DownstreamPort": 80,
"DownstreamHostAndPorts": [
{
"Host": "catalog.api",
"Port": 80
}
],
"UpstreamPathTemplate": "/catalog-api/{everything}",
"UpstreamHttpMethod": []
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHost": "marketing.api",
"DownstreamPort": 80,
"DownstreamHostAndPorts": [
{
"Host": "marketing.api",
"Port": 80
}
],
"UpstreamPathTemplate": "/marketing-api/{everything}",
"UpstreamHttpMethod": []
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHost": "payment.api",
"DownstreamPort": 80,
"DownstreamHostAndPorts": [
{
"Host": "payment.api",
"Port": 80
}
],
"UpstreamPathTemplate": "/payment-api/{everything}",
"UpstreamHttpMethod": []
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHost": "locations.api",
"DownstreamPort": 80,
"DownstreamHostAndPorts": [
{
"Host": "locations.api",
"Port": 80
}
],
"UpstreamPathTemplate": "/location-api/{everything}",
"UpstreamHttpMethod": []
}


+ 5
- 0
src/BuildingBlocks/Resilience/Resilience.Http/ResilientHttpClient.cs View File

@ -97,6 +97,11 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http
throw new HttpRequestException();
}
if (!response.IsSuccessStatusCode)
{
return null;
}
return await response.Content.ReadAsStringAsync();
});
}


+ 5
- 0
src/BuildingBlocks/Resilience/Resilience.Http/StandardHttpClient.cs View File

@ -36,6 +36,11 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http
var response = await _client.SendAsync(requestMessage);
if (!response.IsSuccessStatusCode)
{
return null;
}
return await response.Content.ReadAsStringAsync();
}


+ 4
- 0
src/Services/Basket/Basket.API/Controllers/BasketController.cs View File

@ -34,6 +34,10 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
public async Task<IActionResult> Get(string id)
{
var basket = await _repository.GetBasketAsync(id);
if (basket == null)
{
return NotFound();
}
return Ok(basket);
}


+ 0
- 3
src/Web/WebMVC/AppSettings.cs View File

@ -8,9 +8,6 @@ namespace Microsoft.eShopOnContainers.WebMVC
public class AppSettings
{
public Connectionstrings ConnectionStrings { get; set; }
public string CatalogUrl { get; set; }
public string OrderingUrl { get; set; }
public string BasketUrl { get; set; }
public string MarketingUrl { get; set; }
public string LocationsUrl { get; set; }


+ 8
- 13
src/Web/WebMVC/Services/BasketService.cs View File

@ -16,7 +16,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
{
private readonly IOptionsSnapshot<AppSettings> _settings;
private readonly IHttpClient _apiClient;
private readonly string _remoteServiceBaseUrl;
private readonly string _basketByPassUrl;
private readonly string _purchaseUrl;
private readonly IHttpContextAccessor _httpContextAccesor;
@ -26,7 +26,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
IHttpContextAccessor httpContextAccesor, IHttpClient httpClient)
{
_settings = settings;
_remoteServiceBaseUrl = $"{_settings.Value.BasketUrl}/api/v1/basket";
_basketByPassUrl = $"{_settings.Value.PurchaseUrl}/api/v1/b/basket";
_purchaseUrl = $"{_settings.Value.PurchaseUrl}/api/v1";
_httpContextAccesor = httpContextAccesor;
_apiClient = httpClient;
@ -35,24 +35,19 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
public async Task<Basket> GetBasket(ApplicationUser user)
{
var token = await GetUserTokenAsync();
var getBasketUri = API.Basket.GetBasket(_remoteServiceBaseUrl, user.Id);
var getBasketUri = API.Basket.GetBasket(_basketByPassUrl, user.Id);
var dataString = await _apiClient.GetStringAsync(getBasketUri, token);
// Use the ?? Null conditional operator to simplify the initialization of response
var response = JsonConvert.DeserializeObject<Basket>(dataString) ??
new Basket()
{
BuyerId = user.Id
};
return response;
return string.IsNullOrEmpty(dataString) ?
new Basket() { BuyerId = user.Id} :
JsonConvert.DeserializeObject<Basket>(dataString);
}
public async Task<Basket> UpdateBasket(Basket basket)
{
var token = await GetUserTokenAsync();
var updateBasketUri = API.Basket.UpdateBasket(_remoteServiceBaseUrl);
var updateBasketUri = API.Basket.UpdateBasket(_basketByPassUrl);
var response = await _apiClient.PostAsync(updateBasketUri, basket, token);
@ -64,7 +59,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
public async Task Checkout(BasketDTO basket)
{
var token = await GetUserTokenAsync();
var updateBasketUri = API.Basket.CheckoutBasket(_remoteServiceBaseUrl);
var updateBasketUri = API.Basket.CheckoutBasket(_basketByPassUrl);
var response = await _apiClient.PostAsync(updateBasketUri, basket, token);


+ 1
- 1
src/Web/WebMVC/Services/CatalogService.cs View File

@ -25,7 +25,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
_apiClient = httpClient;
_logger = logger;
_remoteServiceBaseUrl = $"{_settings.Value.CatalogUrl}/api/v1/catalog/";
_remoteServiceBaseUrl = $"{_settings.Value.PurchaseUrl}/api/v1/c/catalog/";
}
public async Task<Catalog> GetCatalogItems(int page, int take, int? brand, int? type)


+ 1
- 1
src/Web/WebMVC/Services/OrderingService.cs View File

@ -21,7 +21,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
public OrderingService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor, IHttpClient httpClient)
{
_remoteServiceBaseUrl = $"{settings.Value.OrderingUrl}/api/v1/orders";
_remoteServiceBaseUrl = $"{settings.Value.PurchaseUrl}/api/v1/o/orders";
_settings = settings;
_httpContextAccesor = httpContextAccesor;
_apiClient = httpClient;


Loading…
Cancel
Save