From c00ec7f9cf6fcef477dd61397610f681c81537ed Mon Sep 17 00:00:00 2001 From: JosepFe Date: Wed, 9 Apr 2025 16:16:30 +0200 Subject: [PATCH] enable secrets when local or development --- .../Extensions/ConfigurationExtensions.cs | 16 +++++ .../WebApplicationBuilderExtensions.cs | 9 ++- .../Boostrappers/ApiBoostrapper.cs | 3 + .../Faura.WebAPI/Faura.WebAPI.csproj | 1 + src/Templates/Faura.WebAPI/Faura.WebAPI.http | 6 -- src/Templates/Faura.WebAPI/Program.cs | 3 +- .../Properties/launchSettings.json | 9 ++- .../Faura.WebAPI/appsettings.Development.json | 10 +-- src/Templates/Faura.WebAPI/appsettings.json | 67 +++++++++++++++---- 9 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 src/Modules/Faura.Infrastructure.ApiBoostraper/Extensions/ConfigurationExtensions.cs delete mode 100644 src/Templates/Faura.WebAPI/Faura.WebAPI.http diff --git a/src/Modules/Faura.Infrastructure.ApiBoostraper/Extensions/ConfigurationExtensions.cs b/src/Modules/Faura.Infrastructure.ApiBoostraper/Extensions/ConfigurationExtensions.cs new file mode 100644 index 0000000..27c4378 --- /dev/null +++ b/src/Modules/Faura.Infrastructure.ApiBoostraper/Extensions/ConfigurationExtensions.cs @@ -0,0 +1,16 @@ +namespace Faura.Infrastructure.ApiBoostraper.Extensions; + +using Faura.Configurations; +using Microsoft.Extensions.Configuration; + +public static class ConfigurationExtensions +{ + public static void ConfigureUserSecrets(this IConfigurationBuilder configBuilder) + where T : class + { + if(FauraEnvironment.IsLocal || FauraEnvironment.IsDevelopment) + { + configBuilder.AddUserSecrets(optional: true); + } + } +} diff --git a/src/Modules/Faura.Infrastructure.ApiBoostraper/Extensions/WebApplicationBuilderExtensions.cs b/src/Modules/Faura.Infrastructure.ApiBoostraper/Extensions/WebApplicationBuilderExtensions.cs index 57434cc..f9a9066 100644 --- a/src/Modules/Faura.Infrastructure.ApiBoostraper/Extensions/WebApplicationBuilderExtensions.cs +++ b/src/Modules/Faura.Infrastructure.ApiBoostraper/Extensions/WebApplicationBuilderExtensions.cs @@ -1,11 +1,12 @@ namespace Faura.Infrastructure.ApiBoostraper.Extensions; using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; public static class WebApplicationBuilderExtensions { - public static WebApplicationBuilder BootstrapCommonFauraServices(this WebApplicationBuilder builder) { builder.Services.ConfigureControllers(); @@ -16,4 +17,10 @@ public static WebApplicationBuilder BootstrapCommonFauraServices(this WebApplica builder.Services.AddHeadersPropagation(builder.Configuration); return builder; } + + public static void RegisterSettingsProvider(this IHostApplicationBuilder builder) where T : class + { + builder.Configuration.AddEnvironmentVariables(); + builder.Configuration.ConfigureUserSecrets(); + } } diff --git a/src/Templates/Faura.WebAPI/Boostrappers/ApiBoostrapper.cs b/src/Templates/Faura.WebAPI/Boostrappers/ApiBoostrapper.cs index f778079..1ca31c3 100644 --- a/src/Templates/Faura.WebAPI/Boostrappers/ApiBoostrapper.cs +++ b/src/Templates/Faura.WebAPI/Boostrappers/ApiBoostrapper.cs @@ -2,11 +2,14 @@ using Faura.Infrastructure.Logger; using Faura.Infrastructure.JWT.Extensions; +using Faura.Infrastructure.ApiBoostraper.Extensions; public static class ApiBoostrapper { public static WebApplicationBuilder RegisterDependencies(this WebApplicationBuilder builder) { + builder.RegisterSettingsProvider(); + builder.BootstrapCommonFauraServices(); builder.Host.SetupLogging(); builder.Services.SetUpJwt(builder.Configuration); diff --git a/src/Templates/Faura.WebAPI/Faura.WebAPI.csproj b/src/Templates/Faura.WebAPI/Faura.WebAPI.csproj index 7a86c8b..ba82298 100644 --- a/src/Templates/Faura.WebAPI/Faura.WebAPI.csproj +++ b/src/Templates/Faura.WebAPI/Faura.WebAPI.csproj @@ -4,6 +4,7 @@ net8.0 enable enable + bc927e2b-51aa-47ca-b33f-8175e77799fa diff --git a/src/Templates/Faura.WebAPI/Faura.WebAPI.http b/src/Templates/Faura.WebAPI/Faura.WebAPI.http deleted file mode 100644 index e1bbad4..0000000 --- a/src/Templates/Faura.WebAPI/Faura.WebAPI.http +++ /dev/null @@ -1,6 +0,0 @@ -@Faura.WebAPI_HostAddress = http://localhost:5069 - -GET {{Faura.WebAPI_HostAddress}}/weatherforecast/ -Accept: application/json - -### diff --git a/src/Templates/Faura.WebAPI/Program.cs b/src/Templates/Faura.WebAPI/Program.cs index 3d47799..3b390c5 100644 --- a/src/Templates/Faura.WebAPI/Program.cs +++ b/src/Templates/Faura.WebAPI/Program.cs @@ -3,7 +3,8 @@ var builder = WebApplication.CreateBuilder(args); -builder.BootstrapCommonFauraServices(); +var a = builder.Configuration.Sources; + builder.RegisterDependencies(); builder.RegisterApplicationDependencies(); diff --git a/src/Templates/Faura.WebAPI/Properties/launchSettings.json b/src/Templates/Faura.WebAPI/Properties/launchSettings.json index 187b41f..b41f448 100644 --- a/src/Templates/Faura.WebAPI/Properties/launchSettings.json +++ b/src/Templates/Faura.WebAPI/Properties/launchSettings.json @@ -16,7 +16,8 @@ "launchUrl": "swagger", "applicationUrl": "http://localhost:5069", "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "ASPNETCORE_ENVIRONMENT": "Local", + "FAURA_ENVIRONMENT": "Development" } }, "https": { @@ -26,7 +27,8 @@ "launchUrl": "swagger", "applicationUrl": "https://localhost:7140;http://localhost:5069", "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "ASPNETCORE_ENVIRONMENT": "Local", + "FAURA_ENVIRONMENT": "Development" } }, "IIS Express": { @@ -34,7 +36,8 @@ "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "ASPNETCORE_ENVIRONMENT": "Local", + "FAURA_ENVIRONMENT": "Development" } } } diff --git a/src/Templates/Faura.WebAPI/appsettings.Development.json b/src/Templates/Faura.WebAPI/appsettings.Development.json index efab286..043d80b 100644 --- a/src/Templates/Faura.WebAPI/appsettings.Development.json +++ b/src/Templates/Faura.WebAPI/appsettings.Development.json @@ -44,11 +44,11 @@ }, - "JWT": { - "MetadataAddress": "{MetadataAddress-PlaceHolder}", - "ValidIssuer": "{ValidIssuer-PlaceHolder}", - "Audience": "account" - }, + "JWT": { + "MetadataAddress": "http://localhost:18080/realms/nexus/.well-known/openid-configuration", + "ValidIssuer": "http://localhost:18080/realms/nexus", + "Audience": "account" + }, "ConnectionStrings": { "Employee": "{connectionString_placeholder}" diff --git a/src/Templates/Faura.WebAPI/appsettings.json b/src/Templates/Faura.WebAPI/appsettings.json index e080cd9..183b5fa 100644 --- a/src/Templates/Faura.WebAPI/appsettings.json +++ b/src/Templates/Faura.WebAPI/appsettings.json @@ -1,17 +1,56 @@ { - "Logging": { - "MinimumLevel": { - "Default": "Information", - "Override": { - "Microsoft": "Error", - "System": "Error" - } - }, - "Outputs": { - "Console": { - "Enable": true - } - } + "Logging": { + "MinimumLevel": { + "Default": "Information", + "Override": { + "Microsoft": "Error", + "System": "Error" + } }, - "AllowedHosts": "*" + "ApplicationName": "FauraApp", + "Outputs": { + "Console": { + "Enable": true, + "LogTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj} | CorrelationId: {CorrelationId} | ApplicationName: {ApplicationName}{NewLine}{Exception}" + } + } + }, + + "Swagger": { + "Authentication": { + "OAuth2": { + "Enable": true, + "Name": "OAuth2", + "AuthenticationURL": "{AuthenticationURL-PlaceHolder}", + "Scopes": { + "openid": "openid", + "profile": "profile" + } + }, + "Bearer": { + "Enable": true, + "Name": "Bearer" + }, + "BasicAuth": { + "Enable": false, + "Name": "Basic" + }, + "ApiKey": { + "Enable": false, + "Name": "X-API-Key", + "In": "Header" // "Header" o "Query" + } + } + }, + + + "JWT": { + "MetadataAddress": "http://localhost:18080/realms/nexus/.well-known/openid-configuration", + "ValidIssuer": "http://localhost:18080/realms/nexus", + "Audience": "account" + }, + + "ConnectionStrings": { + "Employee": "{connectionString_placeholder}" + } }