From 0fd20ee9625c33bfda8b7f07d110d681844ded60 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Thu, 4 May 2023 16:06:24 -0700 Subject: [PATCH] More clean up - Make the catalog API runnable - Delete some cruft --- .../Catalog/Catalog.API/Catalog.API.csproj | 2 +- .../Catalog.API/CustomExtensionMethods.cs | 15 +++-- .../Catalog/Catalog.API/GlobalUsings.cs | 4 -- .../InternalServerErrorObjectResult.cs | 10 ---- .../Filters/HttpGlobalExceptionFilter.cs | 58 ------------------- src/Services/Catalog/Catalog.API/Program.cs | 9 +-- .../Properties/launchSettings.json | 22 +------ .../Properties/serviceDependencies.json | 14 +++++ .../Properties/serviceDependencies.local.json | 23 ++++++++ .../Catalog/Catalog.API/appsettings.json | 11 +--- 10 files changed, 50 insertions(+), 118 deletions(-) delete mode 100644 src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs delete mode 100644 src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs create mode 100644 src/Services/Catalog/Catalog.API/Properties/serviceDependencies.json create mode 100644 src/Services/Catalog/Catalog.API/Properties/serviceDependencies.local.json diff --git a/src/Services/Catalog/Catalog.API/Catalog.API.csproj b/src/Services/Catalog/Catalog.API/Catalog.API.csproj index 29573e967..3ffc06f01 100644 --- a/src/Services/Catalog/Catalog.API/Catalog.API.csproj +++ b/src/Services/Catalog/Catalog.API/Catalog.API.csproj @@ -38,7 +38,7 @@ - + diff --git a/src/Services/Catalog/Catalog.API/CustomExtensionMethods.cs b/src/Services/Catalog/Catalog.API/CustomExtensionMethods.cs index 4524c150b..962108297 100644 --- a/src/Services/Catalog/Catalog.API/CustomExtensionMethods.cs +++ b/src/Services/Catalog/Catalog.API/CustomExtensionMethods.cs @@ -12,7 +12,7 @@ public static class CustomExtensionMethods .AddSqlServer( connectionString, name: "CatalogDB-check", - tags: new string[] { "catalogdb" }); + tags: new string[] { "live", "ready" }); } var accountName = configuration["AzureStorageAccountName"]; @@ -24,7 +24,7 @@ public static class CustomExtensionMethods .AddAzureBlobStorage( $"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accountKey};EndpointSuffix=core.windows.net", name: "catalog-storage-check", - tags: new string[] { "catalogstorage" }); + tags: new string[] { "live", "ready" }); } return services; @@ -41,13 +41,12 @@ public static class CustomExtensionMethods sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }; - services.AddEntityFrameworkSqlServer() - .AddDbContext(options => - { - var connectionString = configuration.GetRequiredConnectionString("CatalogDB"); + services.AddDbContext(options => + { + var connectionString = configuration.GetRequiredConnectionString("CatalogDB"); - options.UseSqlServer(connectionString, ConfigureSqlOptions); - }); + options.UseSqlServer(connectionString, ConfigureSqlOptions); + }); services.AddDbContext(options => { diff --git a/src/Services/Catalog/Catalog.API/GlobalUsings.cs b/src/Services/Catalog/Catalog.API/GlobalUsings.cs index fdd190250..bd9e2d862 100644 --- a/src/Services/Catalog/Catalog.API/GlobalUsings.cs +++ b/src/Services/Catalog/Catalog.API/GlobalUsings.cs @@ -14,7 +14,6 @@ global using Microsoft.AspNetCore.Builder; global using Microsoft.AspNetCore.Hosting; global using Microsoft.AspNetCore.Http; global using Microsoft.AspNetCore.Mvc; -global using Microsoft.AspNetCore.Mvc.Filters; global using Microsoft.EntityFrameworkCore; global using Microsoft.EntityFrameworkCore.Design; global using Microsoft.EntityFrameworkCore.Metadata.Builders; @@ -27,10 +26,8 @@ global using Microsoft.eShopOnContainers.Services.Catalog.API; global using Microsoft.eShopOnContainers.Services.Catalog.API.Extensions; global using Microsoft.eShopOnContainers.Services.Catalog.API.Grpc; global using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; -global using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.ActionResults; global using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.EntityConfigurations; global using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.Exceptions; -global using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.Filters; global using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents; global using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling; global using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events; @@ -39,7 +36,6 @@ global using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel; global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.FileProviders; -global using Microsoft.Extensions.Hosting; global using Microsoft.Extensions.Logging; global using Microsoft.Extensions.Options; global using Polly; diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs b/src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs deleted file mode 100644 index 53af0f0b8..000000000 --- a/src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.ActionResults; - -public class InternalServerErrorObjectResult : ObjectResult -{ - public InternalServerErrorObjectResult(object error) - : base(error) - { - StatusCode = StatusCodes.Status500InternalServerError; - } -} diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs b/src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs deleted file mode 100644 index 66ee35e7b..000000000 --- a/src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs +++ /dev/null @@ -1,58 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.Filters; - -public class HttpGlobalExceptionFilter : IExceptionFilter -{ - private readonly IWebHostEnvironment env; - private readonly ILogger logger; - - public HttpGlobalExceptionFilter(IWebHostEnvironment env, ILogger logger) - { - this.env = env; - this.logger = logger; - } - - public void OnException(ExceptionContext context) - { - logger.LogError(new EventId(context.Exception.HResult), - context.Exception, - context.Exception.Message); - - if (context.Exception.GetType() == typeof(CatalogDomainException)) - { - var problemDetails = new ValidationProblemDetails() - { - Instance = context.HttpContext.Request.Path, - Status = StatusCodes.Status400BadRequest, - Detail = "Please refer to the errors property for additional details." - }; - - problemDetails.Errors.Add("DomainValidations", new string[] { context.Exception.Message.ToString() }); - - context.Result = new BadRequestObjectResult(problemDetails); - context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; - } - else - { - var json = new JsonErrorResponse - { - Messages = new[] { "An error ocurred." } - }; - - if (env.IsDevelopment()) - { - json.DeveloperMessage = context.Exception; - } - - context.Result = new InternalServerErrorObjectResult(json); - context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; - } - context.ExceptionHandled = true; - } - - private class JsonErrorResponse - { - public string[] Messages { get; set; } - - public object DeveloperMessage { get; set; } - } -} diff --git a/src/Services/Catalog/Catalog.API/Program.cs b/src/Services/Catalog/Catalog.API/Program.cs index c39d369c8..57e9c929f 100644 --- a/src/Services/Catalog/Catalog.API/Program.cs +++ b/src/Services/Catalog/Catalog.API/Program.cs @@ -4,14 +4,10 @@ var builder = WebApplication.CreateBuilder(args); builder.AddServiceDefaults(); -builder.Services.AddControllers(options => -{ - options.Filters.Add(typeof(HttpGlobalExceptionFilter)); -}) -.AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = true); - builder.Services.AddGrpc(); +builder.Services.AddControllers(); +// Applcation specific services builder.Services.AddDbContexts(builder.Configuration); builder.Services.AddApplicationOptions(builder.Configuration); builder.Services.AddHealthChecks(builder.Configuration); @@ -33,7 +29,6 @@ app.UseFileServer(new FileServerOptions app.MapGet("/", () => Results.Redirect("/swagger")); app.MapControllers(); - app.MapGrpcService(); var eventBus = app.Services.GetRequiredService(); diff --git a/src/Services/Catalog/Catalog.API/Properties/launchSettings.json b/src/Services/Catalog/Catalog.API/Properties/launchSettings.json index a85ce1869..e16d546a4 100644 --- a/src/Services/Catalog/Catalog.API/Properties/launchSettings.json +++ b/src/Services/Catalog/Catalog.API/Properties/launchSettings.json @@ -1,27 +1,9 @@ { - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:57424/", - "sslPort": 0 - } - }, "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "/swagger", - "environmentVariables": { - "ConnectionString": "server=localhost,5433;Database=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word", - "ASPNETCORE_ENVIRONMENT": "Development", - "EventBusConnection": "localhost" - } - }, - "Microsoft.eShopOnContainers.Services.Catalog.API": { + "Catalog.API": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "http://localhost:55101/", + "applicationUrl": "http://localhost:5226/", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/src/Services/Catalog/Catalog.API/Properties/serviceDependencies.json b/src/Services/Catalog/Catalog.API/Properties/serviceDependencies.json new file mode 100644 index 000000000..62b84ead2 --- /dev/null +++ b/src/Services/Catalog/Catalog.API/Properties/serviceDependencies.json @@ -0,0 +1,14 @@ +{ + "dependencies": { + "rabbitmq1": { + "type": "rabbitmq", + "connectionId": "ConnectionStrings:EventBus", + "dynamicId": null + }, + "mssql1": { + "type": "mssql", + "connectionId": "ConnectionStrings:CatalogDB", + "dynamicId": null + } + } +} \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.API/Properties/serviceDependencies.local.json b/src/Services/Catalog/Catalog.API/Properties/serviceDependencies.local.json new file mode 100644 index 000000000..9dcbd89ce --- /dev/null +++ b/src/Services/Catalog/Catalog.API/Properties/serviceDependencies.local.json @@ -0,0 +1,23 @@ +{ + "dependencies": { + "rabbitmq1": { + "containerPorts": "5672:5672,15672:15672", + "secretStore": "LocalSecretsFile", + "containerName": "rabbitmq", + "containerImage": "rabbitmq:3-management-alpine", + "type": "rabbitmq.container", + "connectionId": "ConnectionStrings:EventBus", + "dynamicId": null + }, + "mssql1": { + "serviceConnectorResourceId": "", + "containerPorts": "1434:1433", + "secretStore": "LocalSecretsFile", + "containerName": "catalog-mssql", + "containerImage": "mcr.microsoft.com/mssql/server:2019-latest", + "type": "mssql.container", + "connectionId": "ConnectionStrings:CatalogDB", + "dynamicId": null + } + } +} \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.API/appsettings.json b/src/Services/Catalog/Catalog.API/appsettings.json index 3711850cc..eb811f017 100644 --- a/src/Services/Catalog/Catalog.API/appsettings.json +++ b/src/Services/Catalog/Catalog.API/appsettings.json @@ -5,20 +5,11 @@ "ApplicationInsights": { "InstrumentationKey": "" }, - "Identity": { - "Url": "http://localhost:5105", - "ExternalUrl": "http://localhost:5105", - "Audience": "catalog", - "Scopes": { - "catalog": "Catalog API" - } - }, "OpenApi": { "Endpoint": { - "Name": "" + "Name": "Catalog.API V1" }, "Document": { - "Name": "Catalog.API V1", "Description": "The Catalog Microservice HTTP API. This is a Data-Driven/CRUD microservice sample", "Title": "eShopOnContainers - Catalog HTTP API", "Version": "v1"