Skip to content

Commit e415f2a

Browse files
author
ericuss
committed
migrate web bff to 3.0 and apply grcp in order controller
1 parent 3f61eb0 commit e415f2a

19 files changed

Lines changed: 321 additions & 188 deletions

docker-compose.override.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ services:
272272
- MarketingUrlHC=http://marketing.api/hc
273273
- PaymentUrlHC=http://payment.api/hc
274274
- LocationUrlHC=http://locations.api/hc
275-
- IdentityUrlExternal=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5105
275+
- IdentityUrlExternal=http://10.0.75.1:5105
276276
ports:
277277
- "5203:80"
278278
volumes:
@@ -305,6 +305,8 @@ services:
305305
- urls__catalog=http://catalog.api
306306
- urls__orders=http://ordering.api
307307
- urls__identity=http://identity.api
308+
- urls__grpcBasket=http://10.0.75.1:5580
309+
- urls__grpcOrdering=http://10.0.75.1:5581
308310
- CatalogUrlHC=http://catalog.api/hc
309311
- OrderingUrlHC=http://ordering.api/hc
310312
- IdentityUrlHC=http://identity.api/hc

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="$(AspNetCore_HealthChecks_UI_Client)" />
2020
<PackageReference Include="Grpc.AspNetCore.Server.ClientFactory" Version="$(Grpc_AspNetCore_Server_ClientFactory)" />
2121
<PackageReference Include="Google.Protobuf" Version="$(Google_Protobuf)" />
22-
<PackageReference Include="Grpc.Core" Version="1.22.0" />
22+
<PackageReference Include="Grpc.Core" Version="$(Grpc_Core)" />
2323
<PackageReference Include="Grpc.Tools" Version="$(Grpc_Tools)" PrivateAssets="All" />
2424
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="$(Microsoft_AspNetCore_Diagnostics_HealthChecks)" />
2525
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="$(Microsoft_Extensions_Diagnostics_HealthChecks)" />

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Program.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,6 @@ public static IWebHost BuildWebHost(string[] args) =>
3232
ReloadOnChange = false
3333
});
3434
})
35-
.ConfigureKestrel(options =>
36-
{
37-
var ports = GetDefinedPorts(_configuration);
38-
39-
options.Listen(IPAddress.Any, ports.httpPort, listenOptions =>
40-
{
41-
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
42-
});
43-
44-
options.Listen(IPAddress.Any, ports.grpcPort, listenOptions =>
45-
{
46-
listenOptions.Protocols = HttpProtocols.Http2;
47-
});
48-
})
4935
.UseStartup<Startup>()
5036
.UseSerilog((builderContext, config) =>
5137
{
@@ -67,12 +53,5 @@ private static IConfiguration GetConfiguration()
6753

6854
return builder.Build();
6955
}
70-
71-
private static (int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config)
72-
{
73-
var grpcPort = config.GetValue("GRPC_PORT", 5001);
74-
var port = config.GetValue("PORT", 80);
75-
return (port, grpcPort);
76-
}
7756
}
7857
}

src/ApiGateways/Web.Bff.Shopping/aggregator/Config/UrlsConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ public class OrdersOperations
2929
public string Basket { get; set; }
3030
public string Catalog { get; set; }
3131
public string Orders { get; set; }
32+
public string GrpcBasket { get; set; }
33+
public string GrpcOrdering { get; set; }
3234
}
3335
}

src/ApiGateways/Web.Bff.Shopping/aggregator/Controllers/BasketController.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public BasketController(ICatalogService catalogService, IBasketService basketSer
3131
[ProducesResponseType(typeof(BasketData), (int)HttpStatusCode.OK)]
3232
public async Task<ActionResult<BasketData>> UpdateAllBasketAsync([FromBody] UpdateBasketRequest data)
3333
{
34-
Log.Information("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ UpdateAllBasketAsync @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
34+
Log.Debug("UpdateAllBasketAsync");
3535

3636
if (data.Items == null || !data.Items.Any())
3737
{
@@ -41,10 +41,12 @@ public async Task<ActionResult<BasketData>> UpdateAllBasketAsync([FromBody] Upda
4141
// Retrieve the current basket
4242
Log.Information("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ GetByIdAsync @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
4343

44-
var basket = await _basket.GetByIdAsync(data.BuyerId) ?? new BasketData(data.BuyerId);
45-
Log.Information("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ basket @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
44+
var basket = await _basket.GetById(data.BuyerId) ?? new BasketData(data.BuyerId);
45+
46+
Log.Debug("get basket by id response={@response}", basket);
4647

4748
var catalogItems = await _catalog.GetCatalogItemsAsync(data.Items.Select(x => x.ProductId));
49+
Log.Debug("get catalog items response={@response}", catalogItems);
4850

4951
foreach (var bitem in data.Items)
5052
{
@@ -82,7 +84,7 @@ public async Task<ActionResult<BasketData>> UpdateQuantitiesAsync([FromBody] Upd
8284
}
8385

8486
// Retrieve the current basket
85-
var currentBasket = await _basket.GetByIdAsync(data.BasketId);
87+
var currentBasket = await _basket.GetById(data.BasketId);
8688
if (currentBasket == null)
8789
{
8890
return BadRequest($"Basket with id {data.BasketId} not found.");
@@ -122,7 +124,7 @@ public async Task<ActionResult> AddBasketItemAsync([FromBody] AddBasketItemReque
122124
//item.PictureUri =
123125

124126
// Step 2: Get current basket status
125-
var currentBasket = (await _basket.GetByIdAsync(data.BasketId)) ?? new BasketData(data.BasketId);
127+
var currentBasket = (await _basket.GetById(data.BasketId)) ?? new BasketData(data.BasketId);
126128
// Step 3: Search if exist product into basket
127129
var product = currentBasket.Items.SingleOrDefault(i => i.ProductId == item.Id.ToString());
128130

src/ApiGateways/Web.Bff.Shopping/aggregator/Controllers/OrderController.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers
1616
public class OrderController : ControllerBase
1717
{
1818
private readonly IBasketService _basketService;
19-
private readonly IOrderApiClient _orderClient;
20-
public OrderController(IBasketService basketService, IOrderApiClient orderClient)
19+
private readonly IOrderingService _orderingService;
20+
public OrderController(IBasketService basketService, IOrderingService orderingService)
2121
{
2222
_basketService = basketService;
23-
_orderClient = orderClient;
23+
_orderingService = orderingService;
2424
}
2525

2626
[Route("draft/{basketId}")]
@@ -34,14 +34,14 @@ public async Task<ActionResult<OrderData>> GetOrderDraftAsync(string basketId)
3434
return BadRequest("Need a valid basketid");
3535
}
3636
// Get the basket data and build a order draft based on it
37-
var basket = await _basketService.GetByIdAsync(basketId);
37+
var basket = await _basketService.GetById(basketId);
3838

3939
if (basket == null)
4040
{
4141
return BadRequest($"No basket found for id {basketId}");
4242
}
4343

44-
return await _orderClient.GetOrderDraftFromBasketAsync(basket);
44+
return await _orderingService.GetOrderDraftAsync(basket);
4545
}
4646
}
4747
}

src/ApiGateways/Web.Bff.Shopping/aggregator/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS base
1+
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
22
WORKDIR /app
33
EXPOSE 80
44

5-
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
5+
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
66
WORKDIR /src
77

88
COPY scripts scripts/

src/ApiGateways/Web.Bff.Shopping/aggregator/Filters/AuthorizeCheckOperationFilter.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Filters
22
{
33
using Microsoft.AspNetCore.Authorization;
4-
using Swashbuckle.AspNetCore.Swagger;
4+
using Microsoft.OpenApi.Models;
55
using Swashbuckle.AspNetCore.SwaggerGen;
66
using System.Collections.Generic;
77
using System.Linq;
@@ -10,22 +10,27 @@ namespace Basket.API.Infrastructure.Filters
1010
{
1111
public class AuthorizeCheckOperationFilter : IOperationFilter
1212
{
13-
public void Apply(Operation operation, OperationFilterContext context)
13+
public void Apply(OpenApiOperation operation, OperationFilterContext context)
1414
{
1515
// Check for authorize attribute
1616
var hasAuthorize = context.MethodInfo.DeclaringType.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any() ||
1717
context.MethodInfo.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any();
1818

1919
if (!hasAuthorize) return;
2020

21-
operation.Responses.TryAdd("401", new Response { Description = "Unauthorized" });
22-
operation.Responses.TryAdd("403", new Response { Description = "Forbidden" });
21+
operation.Responses.TryAdd("401", new OpenApiResponse { Description = "Unauthorized" });
22+
operation.Responses.TryAdd("403", new OpenApiResponse { Description = "Forbidden" });
2323

24-
operation.Security = new List<IDictionary<string, IEnumerable<string>>>
24+
var oAuthScheme = new OpenApiSecurityScheme
2525
{
26-
new Dictionary<string, IEnumerable<string>>
26+
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
27+
};
28+
29+
operation.Security = new List<OpenApiSecurityRequirement>
30+
{
31+
new OpenApiSecurityRequirement
2732
{
28-
{ "oauth2", new [] { "Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator" } }
33+
[ oAuthScheme ] = new [] { "Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator" }
2934
}
3035
};
3136
}

src/ApiGateways/Web.Bff.Shopping/aggregator/Models/BasketData.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class BasketData
66
{
77
public string BuyerId { get; set; }
88

9-
public List<BasketDataItem> Items { get; set; }
9+
public List<BasketDataItem> Items { get; set; } = new List<BasketDataItem>();
1010

1111
public BasketData()
1212
{
@@ -16,7 +16,6 @@ public BasketData()
1616
public BasketData(string buyerId)
1717
{
1818
BuyerId = buyerId;
19-
Items = new List<BasketDataItem>();
2019
}
2120
}
2221

src/ApiGateways/Web.Bff.Shopping/aggregator/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ public static IWebHost BuildWebHost(string[] args) =>
3333
.WriteTo.Console();
3434
})
3535
.Build();
36+
3637
}
3738
}

0 commit comments

Comments
 (0)