From 02e76034b638e781c53404dd8d91b8c0fb647d22 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Sat, 29 Jan 2022 04:22:24 +0300 Subject: [PATCH] added public product grpc service --- .../CatalogServiceHttpApiHostModule.cs | 26 ++++++++++++++++++- ...opOnAbp.CatalogService.HttpApi.Host.csproj | 12 +++++++++ .../Grpc/PublicProductGrpService.cs | 26 +++++++++++++++++++ .../Protos/product.proto | 21 +++++++++++++++ .../ApplicationBuilderHelper.cs | 9 +++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Grpc/PublicProductGrpService.cs create mode 100644 services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Protos/product.proto diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs index 2b0a897b..8114462b 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs @@ -7,9 +7,12 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; +using EShopOnAbp.CatalogService.Grpc; using EShopOnAbp.CatalogService.MongoDB; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; @@ -80,6 +83,10 @@ public class CatalogServiceHttpApiHostModule : AbpModule }); Configure(options => { options.AutoValidate = false; }); + context.Services.AddGrpc(options => + { + options.EnableDetailedErrors = true; + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) @@ -111,7 +118,24 @@ public class CatalogServiceHttpApiHostModule : AbpModule app.UseAbpSerilogEnrichers(); app.UseAuditing(); app.UseUnitOfWork(); - app.UseConfiguredEndpoints(); + app.UseConfiguredEndpoints(endpoints => + { + endpoints.MapGrpcService(); + endpoints.MapGet("/_proto/", async ctx => + { + ctx.Response.ContentType = "text/plain"; + using var fs = new FileStream(Path.Combine(env.ContentRootPath, "Protos", "product.proto"), FileMode.Open, FileAccess.Read); + using var sr = new StreamReader(fs); + while (!sr.EndOfStream) + { + var line = await sr.ReadLineAsync(); + if (line != "/* >>" || line != "<< */") + { + await ctx.Response.WriteAsync(line); + } + } + }); + }); } public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context) diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj index b7e561d5..878bd706 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj @@ -18,6 +18,18 @@ + + + + + + + + + + + + diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Grpc/PublicProductGrpService.cs b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Grpc/PublicProductGrpService.cs new file mode 100644 index 00000000..e0872370 --- /dev/null +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Grpc/PublicProductGrpService.cs @@ -0,0 +1,26 @@ +using System; +using System.Threading.Tasks; +using EShopOnAbp.CatalogService.Products; +using Grpc.Core; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.ObjectMapping; + +namespace EShopOnAbp.CatalogService.Grpc; + +public class PublicProductGrpService : ProductPublic.ProductPublicBase +{ + private readonly IRepository _productRepository; + private readonly IObjectMapper _objectMapper; + + public PublicProductGrpService(IRepository productRepository, IObjectMapper objectMapper) + { + _productRepository = productRepository; + _objectMapper = objectMapper; + } + + public override async Task GetAsync(ProductRequest request, ServerCallContext context) + { + var product = await _productRepository.GetAsync(Guid.Parse(request.Id)); + return _objectMapper.Map(product); + } +} \ No newline at end of file diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Protos/product.proto b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Protos/product.proto new file mode 100644 index 00000000..b096bd65 --- /dev/null +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Protos/product.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +option csharp_namespace = "EShopOnAbp.CatalogService.Grpc"; + +package CatalogApi; + +service ProductPublic { + rpc GetAsync(ProductRequest) returns (ProductDto) {} +} + +message ProductRequest { + string id = 1; +} + +message ProductDto { + string id = 1; + string code = 2; + string name = 3; + float price = 4; + int32 stockCount = 5; +} diff --git a/shared/EShopOnAbp.Shared.Hosting.AspNetCore/ApplicationBuilderHelper.cs b/shared/EShopOnAbp.Shared.Hosting.AspNetCore/ApplicationBuilderHelper.cs index 90faea26..25cdd1f4 100644 --- a/shared/EShopOnAbp.Shared.Hosting.AspNetCore/ApplicationBuilderHelper.cs +++ b/shared/EShopOnAbp.Shared.Hosting.AspNetCore/ApplicationBuilderHelper.cs @@ -1,5 +1,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; @@ -13,6 +15,13 @@ public static class ApplicationBuilderHelper where TStartupModule : IAbpModule { var builder = WebApplication.CreateBuilder(args); + builder.WebHost.ConfigureKestrel(serverOptions => + { + serverOptions.ConfigureEndpointDefaults(listenOptions => + { + listenOptions.Protocols = HttpProtocols.Http1AndHttp2; + }); + }); builder.Host .AddAppSettingsSecretsJson() .UseAutofac()