From 43f76a64dade309e8066a6fa614c4994e21674a2 Mon Sep 17 00:00:00 2001 From: Jean Gatto Date: Wed, 18 Dec 2024 09:32:02 -0300 Subject: [PATCH] News features --- Directory.Packages.props | 17 ++++++----- README.md | 9 ++++-- docker-compose.dcproj | 2 +- .../ServicesCollectionExtensions.cs | 29 ------------------- src/Shop.PublicApi/Program.cs | 9 ++++-- .../Properties/launchSettings.json | 8 ++--- src/Shop.PublicApi/Shop.PublicApi.csproj | 4 ++- 7 files changed, 30 insertions(+), 48 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index d5233fa..e811726 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -23,6 +23,7 @@ + @@ -57,18 +58,18 @@ - - - - - - + + + + + + - + - + \ No newline at end of file diff --git a/README.md b/README.md index 39d3bb4..fbeb9f8 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,19 @@ If you liked this project, learned something, give it a star. Thank you! ## **Technologies** +- ~~ASP.NET Core 8~~ - ASP.NET Core 9 +- ~~Entity Framework Core 8~~ - Entity Framework Core 9 +- **EF Compiled Queries** (https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/compiled-queries-linq-to-entities) - Unit & Integration Tests + xUnit + FluentAssertions - Polly - AutoMapper - FluentValidator - MediatR -- Swagger UI +- ~~Swagger UI~~ +- OpenApi +- **Scalar** - Interactive API Reference from OpenAPI/Swagger (https://github.com/scalar/scalar) - HealthChecks - SQL Server - MongoDB @@ -73,7 +78,7 @@ docker-compose up --build --abort-on-container-exit --remove-orphans Now just open the url in the browser: ```csharp -http://localhost:{port}/swagger/ +http://localhost:{port}/scalar/v1 ``` diff --git a/docker-compose.dcproj b/docker-compose.dcproj index 6b144cd..d8f0065 100644 --- a/docker-compose.dcproj +++ b/docker-compose.dcproj @@ -5,7 +5,7 @@ Linux 24ef58f6-cbf8-4910-a17c-e8de7ed96a87 LaunchBrowser - {Scheme}://localhost:{ServicePort}/swagger + {Scheme}://localhost:{ServicePort}/scalar/v1 shop-webapi diff --git a/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs b/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs index c9090f5..5d59a43 100644 --- a/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs +++ b/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs @@ -1,7 +1,5 @@ using System; using System.Diagnostics.CodeAnalysis; -using System.IO; -using System.Reflection; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; @@ -9,7 +7,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using Microsoft.OpenApi.Models; using Shop.Core.AppSettings; using Shop.Core.Extensions; using Shop.Infrastructure; @@ -28,32 +25,6 @@ internal static class ServicesCollectionExtensions private static readonly string[] DbRelationalTags = ["database", "ef-core", "sql-server", "relational"]; private static readonly string[] DbNoSqlTags = ["database", "mongodb", "no-sql"]; - public static void AddSwagger(this IServiceCollection services) - { - services.AddSwaggerGen(swaggerOptions => - { - swaggerOptions.SwaggerDoc("v1", new OpenApiInfo - { - Version = "v1", - Title = "Shop (e-commerce)", - Description = "ASP.NET Core C# CQRS Event Sourcing, REST API, DDD, SOLID Principles and Clean Architecture", - Contact = new OpenApiContact - { - Name = "Jean Gatto", - Email = "jean_gatto@hotmail.com" - }, - License = new OpenApiLicense - { - Name = "MIT License" - } - }); - - var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; - var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); - swaggerOptions.IncludeXmlComments(xmlPath, true); - }); - } - public static void AddHealthChecks(this IServiceCollection services, IConfiguration configuration) { var options = configuration.GetOptions(); diff --git a/src/Shop.PublicApi/Program.cs b/src/Shop.PublicApi/Program.cs index 3752426..aef993f 100644 --- a/src/Shop.PublicApi/Program.cs +++ b/src/Shop.PublicApi/Program.cs @@ -15,6 +15,7 @@ using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using OwaspHeaders.Core.Extensions; +using Scalar.AspNetCore; using Shop.Application; using Shop.Core; using Shop.Core.Extensions; @@ -49,7 +50,7 @@ builder.Services explorerOptions.SubstituteApiVersionInUrl = true; }); -builder.Services.AddSwagger(); +builder.Services.AddOpenApi(); builder.Services.AddDataProtection(); builder.Services.AddControllers() .ConfigureApiBehaviorOptions(behaviorOptions => @@ -104,7 +105,9 @@ var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) +{ app.UseDeveloperExceptionPage(); +} app.UseHealthChecks("/health", new HealthCheckOptions { @@ -112,9 +115,9 @@ app.UseHealthChecks("/health", new HealthCheckOptions ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); +app.MapOpenApi(); +app.MapScalarApiReference(); app.UseErrorHandling(); -app.UseSwagger(); -app.UseSwaggerUI(); app.UseResponseCompression(); app.UseHttpsRedirection(); app.UseSecureHeadersMiddleware(); diff --git a/src/Shop.PublicApi/Properties/launchSettings.json b/src/Shop.PublicApi/Properties/launchSettings.json index fd406b2..1b5a934 100644 --- a/src/Shop.PublicApi/Properties/launchSettings.json +++ b/src/Shop.PublicApi/Properties/launchSettings.json @@ -4,7 +4,7 @@ "http": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "swagger", + "launchUrl": "scalar/v1", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, @@ -15,7 +15,7 @@ "https": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "swagger", + "launchUrl": "scalar/v1", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, @@ -26,7 +26,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "swagger", + "launchUrl": "scalar/v1", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, @@ -35,7 +35,7 @@ "Docker": { "commandName": "Docker", "launchBrowser": true, - "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/scalar/v1", "environmentVariables": { "ASPNETCORE_URLS": "https://+:443;http://+:80" }, diff --git a/src/Shop.PublicApi/Shop.PublicApi.csproj b/src/Shop.PublicApi/Shop.PublicApi.csproj index e295b61..8b83c82 100644 --- a/src/Shop.PublicApi/Shop.PublicApi.csproj +++ b/src/Shop.PublicApi/Shop.PublicApi.csproj @@ -21,6 +21,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive @@ -60,7 +61,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + @@ -78,6 +79,7 @@ + \ No newline at end of file