Finished purchase-bff implementation and ocelot rerouting

This commit is contained in:
Eduard Tomàs 2018-02-02 15:55:33 +00:00
parent 5dfaf07059
commit 704e6ae693
10 changed files with 108 additions and 48 deletions

View File

@ -134,9 +134,6 @@ services:
environment: environment:
- ASPNETCORE_ENVIRONMENT=Development - ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://0.0.0.0:80 - 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 - PurchaseUrl=http://apigw/purchase-bff
- LocationsUrl=http://locations.api - 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. - 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.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -8,9 +8,6 @@ namespace Microsoft.eShopOnContainers.WebMVC
public class AppSettings public class AppSettings
{ {
public Connectionstrings ConnectionStrings { get; set; } 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 MarketingUrl { get; set; }
public string LocationsUrl { get; set; } public string LocationsUrl { get; set; }

View File

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

View File

@ -25,7 +25,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
_apiClient = httpClient; _apiClient = httpClient;
_logger = logger; _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) public async Task<Catalog> GetCatalogItems(int page, int take, int? brand, int? type)

View File

@ -21,7 +21,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
public OrderingService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor, IHttpClient httpClient) 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; _settings = settings;
_httpContextAccesor = httpContextAccesor; _httpContextAccesor = httpContextAccesor;
_apiClient = httpClient; _apiClient = httpClient;