Skip to content

Commit

Permalink
feat: Realiza a configuração da API
Browse files Browse the repository at this point in the history
Utiliza Options Pattern para aplicar as configurações necessárias
para a API rodar corretamente.
  • Loading branch information
renebentes committed Jan 25, 2024
1 parent 2cb2c93 commit 4a594db
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 1 deletion.
13 changes: 13 additions & 0 deletions JwtStore.Api/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<SecretsOptionsSetup>();

return services;
}
}
4 changes: 4 additions & 0 deletions JwtStore.Api/JwtStore.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\JwtStore.Infrastructure\JwtStore.Infrastructure.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions JwtStore.Api/Options/SecretsOptionsSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using JwtStore.Infrastructure.Options;
using Microsoft.Extensions.Options;

namespace JwtStore.Api.Options;

public class SecretsOptionsSetup : IConfigureOptions<SecretsOptions>
{
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);
}
7 changes: 7 additions & 0 deletions JwtStore.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -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!");
Expand Down
8 changes: 8 additions & 0 deletions JwtStore.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 20 additions & 0 deletions JwtStore.Infrastructure/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<AppDbContext>(options
=> options.UseSqlServer(configuration.GetConnectionString(DefaultConnection))
);

return services;
}
}
2 changes: 1 addition & 1 deletion JwtStore.Infrastructure/JwtStore.Infrastructure.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down
10 changes: 10 additions & 0 deletions JwtStore.Infrastructure/Options/SecretsOptions.cs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 4a594db

Please sign in to comment.