diff --git a/src/Services/Basket/Basket.API/Basket.API.csproj b/src/Services/Basket/Basket.API/Basket.API.csproj index c9d7929a8..bab47e191 100644 --- a/src/Services/Basket/Basket.API/Basket.API.csproj +++ b/src/Services/Basket/Basket.API/Basket.API.csproj @@ -1,54 +1,23 @@ <Project Sdk="Microsoft.NET.Sdk.Web"> - <PropertyGroup> - <TargetFramework>net7.0</TargetFramework> - <AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> - <DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath> - <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks> - <IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled> - </PropertyGroup> + <PropertyGroup> + <TargetFramework>net7.0</TargetFramework> + <DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath> + </PropertyGroup> - <ItemGroup> - <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" /> - <PackageReference Include="System.IdentityModel.Tokens.Jwt" /> - <PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" /> - <PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" /> - <PackageReference Include="AspNetCore.HealthChecks.Redis" /> - <PackageReference Include="AspNetCore.HealthChecks.UI.Client" /> - <PackageReference Include="Autofac.Extensions.DependencyInjection" /> - <PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" /> - <PackageReference Include="Azure.Identity" /> - <PackageReference Include="Google.Protobuf" /> - <PackageReference Include="Grpc.AspNetCore.Server" /> - <PackageReference Include="Grpc.Tools" PrivateAssets="All" /> - <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" /> - <PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" /> - <PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" /> - <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" /> - <PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" /> - <PackageReference Include="Microsoft.AspNetCore.HealthChecks" /> - <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" /> - <PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" /> - <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" /> - <PackageReference Include="Serilog.AspNetCore" /> - <PackageReference Include="Serilog.Enrichers.Environment" /> - <PackageReference Include="Serilog.Settings.Configuration" /> - <PackageReference Include="Serilog.Sinks.Console" /> - <PackageReference Include="Serilog.Sinks.Http" /> - <PackageReference Include="Serilog.Sinks.Seq" /> - <PackageReference Include="Swashbuckle.AspNetCore" /> - <PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" /> - </ItemGroup> + <ItemGroup> + <PackageReference Include="Google.Protobuf" /> + <PackageReference Include="Grpc.AspNetCore.Server" /> + <PackageReference Include="Grpc.Tools" PrivateAssets="All" /> + <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" /> + </ItemGroup> - <ItemGroup> - <Protobuf Include="Proto\basket.proto" GrpcServices="Server" Generator="MSBuild:Compile" /> - <Content Include="@(Protobuf)" /> - <None Remove="@(Protobuf)" /> - </ItemGroup> + <ItemGroup> + <Protobuf Include="Proto\basket.proto" GrpcServices="Server" Generator="MSBuild:Compile" /> + <Content Include="@(Protobuf)" /> + <None Remove="@(Protobuf)" /> + </ItemGroup> - <ItemGroup> - <ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" /> - <ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" /> - <ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" /> - <ProjectReference Include="..\..\Services.Common\Services.Common.csproj" /> - </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\..\Services.Common\Services.Common.csproj" /> + </ItemGroup> </Project> diff --git a/src/Services/Basket/Basket.API/GlobalUsings.cs b/src/Services/Basket/Basket.API/GlobalUsings.cs index 84a9edcbb..bef9dcdae 100644 --- a/src/Services/Basket/Basket.API/GlobalUsings.cs +++ b/src/Services/Basket/Basket.API/GlobalUsings.cs @@ -34,4 +34,5 @@ global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Hosting; global using Microsoft.Extensions.Logging; global using Serilog.Context; +global using Services.Common; global using StackExchange.Redis; diff --git a/src/Services/Basket/Basket.API/Program.cs b/src/Services/Basket/Basket.API/Program.cs index e7dd796e2..0563867fb 100644 --- a/src/Services/Basket/Basket.API/Program.cs +++ b/src/Services/Basket/Basket.API/Program.cs @@ -1,6 +1,4 @@ -using Services.Common; - -var builder = WebApplication.CreateBuilder(args); +var builder = WebApplication.CreateBuilder(args); builder.AddServiceDefaults(); @@ -25,10 +23,10 @@ builder.Services.AddTransient<IIdentityService, IdentityService>(); var app = builder.Build(); -app.MapGet("/", () => Results.Redirect("/swagger")); - app.UseServiceDefaults(); +app.MapGet("/", () => Results.Redirect("/swagger")); + app.MapGrpcService<BasketService>(); app.MapControllers(); @@ -39,10 +37,6 @@ eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHan try { - app.Logger.LogInformation("Configuring web host ({ApplicationContext})...", AppName); - - - app.Logger.LogInformation("Starting web host ({ApplicationContext})...", AppName); await app.RunAsync(); return 0; diff --git a/src/Services/Basket/Basket.API/Properties/launchSettings.json b/src/Services/Basket/Basket.API/Properties/launchSettings.json index 60a56b153..5cb0a5315 100644 --- a/src/Services/Basket/Basket.API/Properties/launchSettings.json +++ b/src/Services/Basket/Basket.API/Properties/launchSettings.json @@ -1,26 +1,12 @@ { - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:58017/", - "sslPort": 0 - } - }, "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "swagger", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "Microsoft.eShopOnContainers.Services.Basket.API": { + "Basket.API": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "http://localhost:55103/", + "applicationUrl": "http://localhost:5222", "environmentVariables": { + "Identity__Url": "http://localhost:5225", + "Identity__ExternalUrl": "http://localhost:5225", "ASPNETCORE_ENVIRONMENT": "Development" } } diff --git a/src/Services/Basket/Basket.API/appsettings.json b/src/Services/Basket/Basket.API/appsettings.json index e628394a8..171521d27 100644 --- a/src/Services/Basket/Basket.API/appsettings.json +++ b/src/Services/Basket/Basket.API/appsettings.json @@ -1,41 +1,29 @@ { - "Serilog": { - "SeqServerUrl": null, - "LogstashgUrl": null, - "MinimumLevel": { + "Logging": { + "LogLevel": { "Default": "Information", - "Override": { - "Microsoft": "Warning", - "Microsoft.eShopOnContainers": "Information", - "System": "Warning" - } + "Microsoft.AspNetCore": "Warning" } }, - "Kestrel": { - "EndpointDefaults": { - "Protocols": "Http2" - } - }, - "ApplicationInsights": { - "InstrumentationKey": "" - }, "OpenApi": { "Endpoint": { - "Name": "" + "Name": "Basket.API V1" }, "Document": { - "Name": "Basket.API V1", "Description": "The Basket Service HTTP API", "Title": "eShopOnContainers - Basket HTTP API", "Version": "v1" + }, + "Auth": { + "ClientId": "basketswaggerui", + "AppName": "Basket Swagger UI" } }, "ConnectionStrings": { - "Redis": "127.0.0.1" + "Redis": "localhost" }, "Identity": { "Audience": "basket", - "Scope": "basket", "Scopes": { "basket": "Basket API" } diff --git a/src/Services/Identity/Identity.API/Properties/launchSettings.json b/src/Services/Identity/Identity.API/Properties/launchSettings.json index e52e9f99c..83fe7f511 100644 --- a/src/Services/Identity/Identity.API/Properties/launchSettings.json +++ b/src/Services/Identity/Identity.API/Properties/launchSettings.json @@ -1,26 +1,11 @@ { - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:54010/", - "sslPort": 0 - } - }, "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "http://localhost:55105", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "eShopOnContainers.Identity": { + "Identity.API": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "http://localhost:55105", + "applicationUrl": "http://localhost:5225", "environmentVariables": { + "BasketApiClient": "http://localhost:5222", "ASPNETCORE_ENVIRONMENT": "Development" } } diff --git a/src/Services/Identity/Identity.API/appsettings.json b/src/Services/Identity/Identity.API/appsettings.json index 087e17fe7..1f239f782 100644 --- a/src/Services/Identity/Identity.API/appsettings.json +++ b/src/Services/Identity/Identity.API/appsettings.json @@ -11,9 +11,8 @@ "MinimumLevel": { "Default": "Information", "Override": { - "Microsoft": "Warning", - "Microsoft.eShopOnContainers": "Information", - "System": "Warning" + "Microsoft.AspNetCore": "Warning", + "Microsoft.EntityFramework": "Warning" } } }, diff --git a/src/Services/Services.Common/CommonExtensions.cs b/src/Services/Services.Common/CommonExtensions.cs index d03764ce2..a523eef83 100644 --- a/src/Services/Services.Common/CommonExtensions.cs +++ b/src/Services/Services.Common/CommonExtensions.cs @@ -1,13 +1,10 @@ using System.IdentityModel.Tokens.Jwt; -using System.Net; -using Autofac.Core; using Azure.Identity; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; @@ -37,10 +34,7 @@ public static class CommonExtensions builder.Services.AddDefaultHealthChecks(builder.Configuration); // Configure the default logging for this application - builder.Host.UseDefaultSerilog(builder.Configuration, builder.Environment.ApplicationName); - - // Configure the default ports for this service (http and grpc ports read from configuration) - builder.WebHost.UseDefaultPorts(builder.Configuration); + // builder.Host.UseDefaultSerilog(builder.Configuration, builder.Environment.ApplicationName); // Customizations for this application @@ -80,7 +74,7 @@ public static class CommonExtensions public static IApplicationBuilder UseDefaultOpenApi(this IApplicationBuilder app, IConfiguration configuration) { - var openApiSection = configuration.GetRequiredSection("OpenApi"); + var openApiSection = configuration.GetSection("OpenApi"); if (!openApiSection.Exists()) { @@ -122,7 +116,7 @@ public static class CommonExtensions public static IServiceCollection AddDefaultOpenApi(this IServiceCollection services, IConfiguration configuration) { - var openApi = configuration.GetRequiredSection("OpenApi"); + var openApi = configuration.GetSection("OpenApi"); if (!openApi.Exists()) { @@ -169,7 +163,7 @@ public static class CommonExtensions // } var identityUrlExternal = identitySection.GetRequiredValue("ExternalUrl"); - var scopes = identitySection.GetRequiredSection("Scopes").AsEnumerable().ToDictionary(p => p.Key, p => p.Value); + var scopes = identitySection.GetRequiredSection("Scopes").GetChildren().ToDictionary(p => p.Key, p => p.Value); options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { @@ -194,8 +188,7 @@ public static class CommonExtensions // { // "Identity": { // "Url": "http://identity", - // "Audience": "basket", - // "Scope": "basket" + // "Audience": "basket" // } // } @@ -221,40 +214,9 @@ public static class CommonExtensions options.TokenValidationParameters.ValidateAudience = false; }); - services.AddAuthorization(options => - { - var scope = identitySection.GetRequiredValue("Scope"); - - options.AddPolicy("ApiScope", policy => - { - policy.RequireAuthenticatedUser(); - policy.RequireClaim("scope", scope); - }); - }); - return services; } - public static IWebHostBuilder UseDefaultPorts(this IWebHostBuilder builder, IConfiguration configuration) - { - builder.UseKestrel(options => - { - var (httpPort, grpcPort) = GetDefinedPorts(configuration); - - options.Listen(IPAddress.Any, httpPort, listenOptions => - { - listenOptions.Protocols = HttpProtocols.Http1AndHttp2; - }); - - options.Listen(IPAddress.Any, grpcPort, listenOptions => - { - listenOptions.Protocols = HttpProtocols.Http2; - }); - }); - - return builder; - } - public static ConfigurationManager AddKeyVault(this ConfigurationManager configuration) { // { @@ -432,15 +394,25 @@ public static class CommonExtensions { var seqServerUrl = configuration["Serilog:SeqServerUrl"]; var logstashUrl = configuration["Serilog:LogstashgUrl"]; - return new LoggerConfiguration() + + var loggingConfiguration = new LoggerConfiguration() .MinimumLevel.Verbose() .Enrich.WithProperty("ApplicationContext", name) .Enrich.FromLogContext() .WriteTo.Console() - .WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl) - .WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl, null) - .ReadFrom.Configuration(configuration) - .CreateLogger(); + .ReadFrom.Configuration(configuration); + + if (!string.IsNullOrEmpty(seqServerUrl)) + { + loggingConfiguration.WriteTo.Seq(seqServerUrl); + } + + if (!string.IsNullOrEmpty(logstashUrl)) + { + loggingConfiguration.WriteTo.Http(logstashUrl, null); + } + + return loggingConfiguration.CreateLogger(); } } @@ -458,13 +430,6 @@ public static class CommonExtensions }); } - static (int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config) - { - var grpcPort = config.GetValue("GRPC_PORT", 5001); - var port = config.GetValue("PORT", 80); - return (port, grpcPort); - } - private static string GetRequiredValue(this IConfiguration configuration, string name) => configuration[name] ?? throw new InvalidOperationException($"Configuration missing value for: {(configuration is IConfigurationSection s ? s.Path + ":" + name : name)}"); }