diff --git a/src/Acme.BookStore.HttpApi.Host/Acme.BookStore.HttpApi.Host.csproj b/src/Acme.BookStore.HttpApi.Host/Acme.BookStore.HttpApi.Host.csproj index a35af32..64e7201 100644 --- a/src/Acme.BookStore.HttpApi.Host/Acme.BookStore.HttpApi.Host.csproj +++ b/src/Acme.BookStore.HttpApi.Host/Acme.BookStore.HttpApi.Host.csproj @@ -17,6 +17,7 @@ + diff --git a/src/Acme.BookStore.HttpApi.Host/Infrastructure/EnviromentLoader.cs b/src/Acme.BookStore.HttpApi.Host/Infrastructure/EnviromentLoader.cs index f8159be..ae00ea5 100644 --- a/src/Acme.BookStore.HttpApi.Host/Infrastructure/EnviromentLoader.cs +++ b/src/Acme.BookStore.HttpApi.Host/Infrastructure/EnviromentLoader.cs @@ -2,8 +2,10 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; +using Newtonsoft.Json.Linq; using System; using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Text; using System.Threading.Tasks; using VaultSharp; @@ -16,24 +18,43 @@ public static class EnviromentLoader { public static async Task SetDatabaseEnviroment([NotNull] this WebApplicationBuilder builder) { - if (!builder.Environment.IsProduction()) - return; +#if !DEBUG + builder.Host.AddAppSettingsSecretsJson(); +#else + var stream = await GetUpdatedConnectionStringsStreamAsync("appsettings.json"); + builder.Configuration.AddJsonStream(stream); +#endif - var connectionSection = builder.Configuration.GetSection("ConnectionStrings"); - var connections = connectionSection.GetChildren(); - var sb = new StringBuilder(); - - foreach (var connection in connections) - { - sb.Append(connection.Path); - sb.Append('='); - sb.Append(await UpdateVaue(connection.Key)); - } - - builder.Configuration.AddEnvironmentVariables(sb.ToString()); } - private static async Task UpdateVaue(string key) + public static async Task GetUpdatedConnectionStringsStreamAsync(string file) + { + var content = JObject.Parse(await File.ReadAllTextAsync(file)); + if (content["ConnectionStrings"] is not JObject jsonReader) + return Stream.Null; + + foreach (var item in jsonReader.Properties()) + { + var value = await UpdateValue(item.Name); + if (!string.IsNullOrEmpty(value)) + { + jsonReader[item.Name] = value; + } + } + + var memoryStream = new MemoryStream(); + using (var writer = new StreamWriter(memoryStream, leaveOpen: true)) + { + await writer.WriteAsync(content.ToString()); + await writer.FlushAsync(); + } + + memoryStream.Position = 0; + return memoryStream; + } + + + private static async Task UpdateValue(string key) { var secrate = await GetSecret(); return secrate.Data.Data.TryGetValue(key, out var val) diff --git a/src/Acme.BookStore.HttpApi.Host/Program.cs b/src/Acme.BookStore.HttpApi.Host/Program.cs index 8c3d88a..96fbc0c 100644 --- a/src/Acme.BookStore.HttpApi.Host/Program.cs +++ b/src/Acme.BookStore.HttpApi.Host/Program.cs @@ -23,16 +23,15 @@ public class Program Log.Information("Starting Acme.BookStore.HttpApi.Host."); var builder = WebApplication.CreateBuilder(args); builder.Host - .AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog((context, services, loggerConfiguration) => { loggerConfiguration - #if DEBUG +#if DEBUG .MinimumLevel.Debug() - #else +#else .MinimumLevel.Information() - #endif +#endif .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) .Enrich.FromLogContext()