From 0e73b9f16a3d2ecb8073d9e07eea58be298c4099 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 22 Mar 2022 01:40:59 +0300 Subject: [PATCH] added yarp to public-web gateway --- .../EShopOnAbpWebPublicGatewayModule.cs | 105 +++++++++++------- .../appsettings.json | 85 +++++++++++++- 2 files changed, 146 insertions(+), 44 deletions(-) diff --git a/gateways/web-public/src/EShopOnAbp.WebPublicGateway/EShopOnAbpWebPublicGatewayModule.cs b/gateways/web-public/src/EShopOnAbp.WebPublicGateway/EShopOnAbpWebPublicGatewayModule.cs index a77e5660..00f4da4f 100644 --- a/gateways/web-public/src/EShopOnAbp.WebPublicGateway/EShopOnAbpWebPublicGatewayModule.cs +++ b/gateways/web-public/src/EShopOnAbp.WebPublicGateway/EShopOnAbpWebPublicGatewayModule.cs @@ -7,6 +7,7 @@ using Ocelot.Middleware; using System.Collections.Generic; using System.Linq; using EShopOnAbp.Shared.Hosting.AspNetCore; +using Microsoft.AspNetCore.Http; using Volo.Abp; using Volo.Abp.Modularity; using Microsoft.AspNetCore.Rewrite; @@ -23,21 +24,24 @@ public class EShopOnAbpWebPublicGatewayModule : AbpModule var configuration = context.Services.GetConfiguration(); var hostingEnvironment = context.Services.GetHostingEnvironment(); - SwaggerConfigurationHelper.ConfigureWithAuth( - context: context, - authority: configuration["AuthServer:Authority"], - scopes: new Dictionary /* Requested scopes for authorization code request and descriptions for swagger UI only */ - { - { "AccountService", "Account Service API" }, - { "IdentityService", "Identity Service API" }, - { "AdministrationService", "Administration Service API" }, - { "CatalogService", "Catalog Service API" }, - { "BasketService", "Basket Service API" }, - { "PaymentService", "Payment Service API" }, - { "OrderingService", "Ordering Service API" }, - }, - apiTitle: "WebPublic Gateway" - ); + // SwaggerConfigurationHelper.ConfigureWithAuth( + // context: context, + // authority: configuration["AuthServer:Authority"], + // scopes: new Dictionary /* Requested scopes for authorization code request and descriptions for swagger UI only */ + // { + // { "AccountService", "Account Service API" }, + // { "IdentityService", "Identity Service API" }, + // { "AdministrationService", "Administration Service API" }, + // { "CatalogService", "Catalog Service API" }, + // { "BasketService", "Basket Service API" }, + // { "PaymentService", "Payment Service API" }, + // { "OrderingService", "Ordering Service API" }, + // }, + // apiTitle: "WebPublic Gateway" + // ); + + context.Services.AddReverseProxy() + .LoadFromConfig(configuration.GetSection("ReverseProxy")); } public override void OnApplicationInitialization(ApplicationInitializationContext context) @@ -52,35 +56,50 @@ public class EShopOnAbpWebPublicGatewayModule : AbpModule app.UseCorrelationId(); app.UseAbpSerilogEnrichers(); - app.UseSwagger(); - app.UseSwaggerUI(options => + // app.UseSwagger(); + // app.UseSwaggerUI(options => + // { + // var configuration = context.ServiceProvider.GetRequiredService(); + // var routes = configuration.GetSection("Routes").Get>(); + // var routedServices = routes + // .GroupBy(t => t.ServiceKey) + // .Select(r => r.First()) + // .Distinct(); + // + // foreach (var config in routedServices) + // { + // var url = + // $"{config.DownstreamScheme}://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}:{config.DownstreamHostAndPorts.FirstOrDefault()?.Port}"; + // if (!env.IsDevelopment()) + // { + // url = $"https://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}"; + // } + // + // options.SwaggerEndpoint($"{url}/swagger/v1/swagger.json", $"{config.ServiceKey} API"); + // options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); + // options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); + // } + // }); + // + // app.UseRewriter(new RewriteOptions() + // // Regex for "", "/" and "" (whitespace) + // .AddRedirect("^(|\\|\\s+)$", "/swagger")); + // + // app.UseOcelot().Wait(); + // app.MapWhen( + // ctx => ctx.Request.Path.ToString().TrimEnd('/').Equals(""), + // app2 => + // { + // app2.UseRouting(); + // // app2.UseConfiguredEndpoints(); + // app2.UseEndpoints(endpoints => endpoints.MapReverseProxy()); + // } + // ); + app.UseRouting(); + app.UseEndpoints(endpoints => { - var configuration = context.ServiceProvider.GetRequiredService(); - var routes = configuration.GetSection("Routes").Get>(); - var routedServices = routes - .GroupBy(t => t.ServiceKey) - .Select(r => r.First()) - .Distinct(); - - foreach (var config in routedServices) - { - var url = - $"{config.DownstreamScheme}://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}:{config.DownstreamHostAndPorts.FirstOrDefault()?.Port}"; - if (!env.IsDevelopment()) - { - url = $"https://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}"; - } - - options.SwaggerEndpoint($"{url}/swagger/v1/swagger.json", $"{config.ServiceKey} API"); - options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); - options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); - } + endpoints.MapGet("", ctx => ctx.Response.WriteAsync("YAG")); + endpoints.MapReverseProxy(); }); - - app.UseRewriter(new RewriteOptions() - // Regex for "", "/" and "" (whitespace) - .AddRedirect("^(|\\|\\s+)$", "/swagger")); - - app.UseOcelot().Wait(); } } \ No newline at end of file diff --git a/gateways/web-public/src/EShopOnAbp.WebPublicGateway/appsettings.json b/gateways/web-public/src/EShopOnAbp.WebPublicGateway/appsettings.json index dfe10330..924dbd44 100644 --- a/gateways/web-public/src/EShopOnAbp.WebPublicGateway/appsettings.json +++ b/gateways/web-public/src/EShopOnAbp.WebPublicGateway/appsettings.json @@ -17,11 +17,94 @@ } }, "AllowedHosts": "*", - "Redis": { "Configuration": "localhost:6379" }, "ElasticSearch": { "Url": "http://localhost:9200" + }, + "ReverseProxy": { + "Routes": { + "account-route": { + "ClusterId": "account-cluster", + "Match": { + "Path": "/api/account/{**everything}" + } + }, + "administration-route": { + "ClusterId": "administration-cluster", + "Match": { + "Path": "/api/abp/{**everything}" + } + }, + "catalog-route": { + "ClusterId": "catalog-cluster", + "Match": { + "Path": "/api/catalog/{**everything}" + } + }, + "basket-route": { + "ClusterId": "basket-cluster", + "Match": { + "Path": "/api/basket/{**everything}" + } + }, + "ordering-route": { + "ClusterId": "ordering-cluster", + "Match": { + "Path": "/api/ordering/{**everything}" + } + }, + "payment-route": { + "ClusterId": "payment-cluster", + "Match": { + "Path": "/api/payment/{**everything}" + } + } + }, + "Clusters": { + "account-cluster": { + "Destinations": { + "destination1": { + "Address": "https://localhost:44330" + } + } + }, + "administration-cluster": { + "Destinations": { + "destination1": { + "Address": "https://localhost:44353" + } + } + }, + "catalog-cluster": { + "Destinations": { + "destination1": { + "Address": "https://localhost:44354" + } + } + }, + "basket-cluster": { + "Destinations": { + "destination1": { + "Address": "https://localhost:44355" + } + } + }, + "ordering-cluster": { + "Destinations": { + "destination1": { + "Address": "https://localhost:44356" + } + } + }, + "payment-cluster": { + "Destinations": { + "destination1": { + "Address": "https://localhost:44357" + } + } + } + } } }