Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
34e3ce8ce1 |
@ -17,6 +17,7 @@
|
||||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="9.0.0" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.4" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Async" Version="2.1.0" />
|
||||
</ItemGroup>
|
||||
|
@ -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)
|
||||
public static async Task<Stream> GetUpdatedConnectionStringsStreamAsync(string file)
|
||||
{
|
||||
sb.Append(connection.Path);
|
||||
sb.Append('=');
|
||||
sb.Append(await UpdateVaue(connection.Key));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
builder.Configuration.AddEnvironmentVariables(sb.ToString());
|
||||
var memoryStream = new MemoryStream();
|
||||
using (var writer = new StreamWriter(memoryStream, leaveOpen: true))
|
||||
{
|
||||
await writer.WriteAsync(content.ToString());
|
||||
await writer.FlushAsync();
|
||||
}
|
||||
|
||||
private static async Task<string> UpdateVaue(string key)
|
||||
memoryStream.Position = 0;
|
||||
return memoryStream;
|
||||
}
|
||||
|
||||
|
||||
private static async Task<string> UpdateValue(string key)
|
||||
{
|
||||
var secrate = await GetSecret();
|
||||
return secrate.Data.Data.TryGetValue(key, out var val)
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user