From 4a594db3b787e2fff77853898bad5a37bf929595 Mon Sep 17 00:00:00 2001 From: Rene Bentes Pinto Date: Thu, 25 Jan 2024 20:19:41 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20Realiza=20a=20configura=C3=A7=C3=A3o=20?= =?UTF-8?q?da=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Utiliza Options Pattern para aplicar as configurações necessárias para a API rodar corretamente. --- .../Extensions/ServiceCollectionExtensions.cs | 13 ++++++++++++ JwtStore.Api/JwtStore.Api.csproj | 4 ++++ JwtStore.Api/Options/SecretsOptionsSetup.cs | 17 ++++++++++++++++ JwtStore.Api/Program.cs | 7 +++++++ JwtStore.Api/appsettings.Development.json | 8 ++++++++ .../Extensions/ServiceCollectionExtensions.cs | 20 +++++++++++++++++++ .../JwtStore.Infrastructure.csproj | 2 +- .../Options/SecretsOptions.cs | 10 ++++++++++ 8 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 JwtStore.Api/Extensions/ServiceCollectionExtensions.cs create mode 100644 JwtStore.Api/Options/SecretsOptionsSetup.cs create mode 100644 JwtStore.Infrastructure/Extensions/ServiceCollectionExtensions.cs create mode 100644 JwtStore.Infrastructure/Options/SecretsOptions.cs diff --git a/JwtStore.Api/Extensions/ServiceCollectionExtensions.cs b/JwtStore.Api/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..2e3bc86 --- /dev/null +++ b/JwtStore.Api/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,13 @@ +using JwtStore.Api.Options; + +namespace JwtStore.Api.Extensions; + +public static class ServiceCollectionExtensions +{ + public static IServiceCollection AddConfigurations(this IServiceCollection services) + { + services.ConfigureOptions(); + + return services; + } +} diff --git a/JwtStore.Api/JwtStore.Api.csproj b/JwtStore.Api/JwtStore.Api.csproj index 1b28a01..abc5e5a 100644 --- a/JwtStore.Api/JwtStore.Api.csproj +++ b/JwtStore.Api/JwtStore.Api.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/JwtStore.Api/Options/SecretsOptionsSetup.cs b/JwtStore.Api/Options/SecretsOptionsSetup.cs new file mode 100644 index 0000000..4bf2e9e --- /dev/null +++ b/JwtStore.Api/Options/SecretsOptionsSetup.cs @@ -0,0 +1,17 @@ +using JwtStore.Infrastructure.Options; +using Microsoft.Extensions.Options; + +namespace JwtStore.Api.Options; + +public class SecretsOptionsSetup : IConfigureOptions +{ + private const string Secrets = nameof(Secrets); + private readonly IConfiguration _configuration; + + public SecretsOptionsSetup(IConfiguration configuration) + => _configuration = configuration; + + public void Configure(SecretsOptions options) + => _configuration.GetSection(Secrets) + .Bind(options); +} diff --git a/JwtStore.Api/Program.cs b/JwtStore.Api/Program.cs index 1760df1..c1ad9ad 100644 --- a/JwtStore.Api/Program.cs +++ b/JwtStore.Api/Program.cs @@ -1,4 +1,11 @@ +using JwtStore.Api.Extensions; +using JwtStore.Infrastructure.Extensions.DependencyInjection; + var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddConfigurations() + .AddPersistence(builder.Configuration); + var app = builder.Build(); app.MapGet("/", () => "Hello World!"); diff --git a/JwtStore.Api/appsettings.Development.json b/JwtStore.Api/appsettings.Development.json index 0c208ae..1273d45 100644 --- a/JwtStore.Api/appsettings.Development.json +++ b/JwtStore.Api/appsettings.Development.json @@ -1,4 +1,12 @@ { + "ConnectionStrings": { + "DefaultConnection": "Server=localhost,1433;Database=jwt-store;User ID=sa;Password=1q2w3e4r@#$;Trusted_Connection=False; TrustServerCertificate=True;" + }, + "Secrets": { + "ApiKey": "", + "JwtPrivateKey": "", + "PasswordSaltKey": "" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/JwtStore.Infrastructure/Extensions/ServiceCollectionExtensions.cs b/JwtStore.Infrastructure/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..f7a4d05 --- /dev/null +++ b/JwtStore.Infrastructure/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,20 @@ +using JwtStore.Infrastructure.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace JwtStore.Infrastructure.Extensions.DependencyInjection; + +public static class ServiceCollectionExtensions +{ + private const string DefaultConnection = nameof(DefaultConnection); + + public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration) + { + services.AddDbContext(options + => options.UseSqlServer(configuration.GetConnectionString(DefaultConnection)) + ); + + return services; + } +} diff --git a/JwtStore.Infrastructure/JwtStore.Infrastructure.csproj b/JwtStore.Infrastructure/JwtStore.Infrastructure.csproj index d336823..7030c79 100644 --- a/JwtStore.Infrastructure/JwtStore.Infrastructure.csproj +++ b/JwtStore.Infrastructure/JwtStore.Infrastructure.csproj @@ -1,4 +1,4 @@ - + net8.0 diff --git a/JwtStore.Infrastructure/Options/SecretsOptions.cs b/JwtStore.Infrastructure/Options/SecretsOptions.cs new file mode 100644 index 0000000..b6df4c0 --- /dev/null +++ b/JwtStore.Infrastructure/Options/SecretsOptions.cs @@ -0,0 +1,10 @@ +namespace JwtStore.Infrastructure.Options; + +public sealed class SecretsOptions +{ + public string ApiKey { get; init; } = string.Empty; + + public string JwtPrivateKey { get; init; } = string.Empty; + + public string PasswordSaltKey { get; init; } = string.Empty; +}