|
|
@ -1,6 +1,7 @@ |
|
|
|
using IdentityServer4.EntityFramework.DbContexts; |
|
|
|
using Microsoft.AspNetCore; |
|
|
|
using Microsoft.AspNetCore.Hosting; |
|
|
|
using Microsoft.eShopOnContainers.Services.Identity.API; |
|
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Data; |
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
@ -11,103 +12,92 @@ using Serilog; |
|
|
|
using System; |
|
|
|
using System.IO; |
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Identity.API |
|
|
|
{ |
|
|
|
public class Program |
|
|
|
{ |
|
|
|
public static readonly string Namespace = typeof(Program).Namespace; |
|
|
|
public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1); |
|
|
|
|
|
|
|
public static int Main(string[] args) |
|
|
|
{ |
|
|
|
var configuration = GetConfiguration(); |
|
|
|
|
|
|
|
Log.Logger = CreateSerilogLogger(configuration); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
Log.Information("Configuring web host ({ApplicationContext})...", AppName); |
|
|
|
var host = BuildWebHost(configuration, args); |
|
|
|
string Namespace = typeof(Startup).Namespace; |
|
|
|
string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1); |
|
|
|
|
|
|
|
Log.Information("Applying migrations ({ApplicationContext})...", AppName); |
|
|
|
host.MigrateDbContext<PersistedGrantDbContext>((_, __) => { }) |
|
|
|
.MigrateDbContext<ApplicationDbContext>((context, services) => |
|
|
|
{ |
|
|
|
var env = services.GetService<IWebHostEnvironment>(); |
|
|
|
var logger = services.GetService<ILogger<ApplicationDbContextSeed>>(); |
|
|
|
var settings = services.GetService<IOptions<AppSettings>>(); |
|
|
|
var configuration = GetConfiguration(); |
|
|
|
|
|
|
|
new ApplicationDbContextSeed() |
|
|
|
.SeedAsync(context, env, logger, settings) |
|
|
|
.Wait(); |
|
|
|
}) |
|
|
|
.MigrateDbContext<ConfigurationDbContext>((context, services) => |
|
|
|
{ |
|
|
|
new ConfigurationDbContextSeed() |
|
|
|
.SeedAsync(context, configuration) |
|
|
|
.Wait(); |
|
|
|
}); |
|
|
|
Log.Logger = CreateSerilogLogger(configuration); |
|
|
|
|
|
|
|
Log.Information("Starting web host ({ApplicationContext})...", AppName); |
|
|
|
host.Run(); |
|
|
|
try |
|
|
|
{ |
|
|
|
Log.Information("Configuring web host ({ApplicationContext})...", AppName); |
|
|
|
var host = BuildWebHost(configuration, args); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", AppName); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
Log.CloseAndFlush(); |
|
|
|
} |
|
|
|
} |
|
|
|
Log.Information("Applying migrations ({ApplicationContext})...", AppName); |
|
|
|
host.MigrateDbContext<PersistedGrantDbContext>((_, __) => { }) |
|
|
|
.MigrateDbContext<ApplicationDbContext>((context, services) => |
|
|
|
{ |
|
|
|
var env = services.GetService<IWebHostEnvironment>(); |
|
|
|
var logger = services.GetService<ILogger<ApplicationDbContextSeed>>(); |
|
|
|
var settings = services.GetService<IOptions<AppSettings>>(); |
|
|
|
|
|
|
|
new ApplicationDbContextSeed() |
|
|
|
.SeedAsync(context, env, logger, settings) |
|
|
|
.Wait(); |
|
|
|
}) |
|
|
|
.MigrateDbContext<ConfigurationDbContext>((context, services) => |
|
|
|
{ |
|
|
|
new ConfigurationDbContextSeed() |
|
|
|
.SeedAsync(context, configuration) |
|
|
|
.Wait(); |
|
|
|
}); |
|
|
|
|
|
|
|
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) => |
|
|
|
WebHost.CreateDefaultBuilder(args) |
|
|
|
.CaptureStartupErrors(false) |
|
|
|
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration)) |
|
|
|
.UseStartup<Startup>() |
|
|
|
.UseContentRoot(Directory.GetCurrentDirectory()) |
|
|
|
.UseSerilog() |
|
|
|
.Build(); |
|
|
|
Log.Information("Starting web host ({ApplicationContext})...", AppName); |
|
|
|
host.Run(); |
|
|
|
|
|
|
|
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) |
|
|
|
{ |
|
|
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"]; |
|
|
|
var logstashUrl = configuration["Serilog:LogstashgUrl"]; |
|
|
|
return new LoggerConfiguration() |
|
|
|
.MinimumLevel.Verbose() |
|
|
|
.Enrich.WithProperty("ApplicationContext", AppName) |
|
|
|
.Enrich.FromLogContext() |
|
|
|
.WriteTo.Console() |
|
|
|
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl) |
|
|
|
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://localhost:8080" : logstashUrl) |
|
|
|
.ReadFrom.Configuration(configuration) |
|
|
|
.CreateLogger(); |
|
|
|
} |
|
|
|
return 0; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", AppName); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
Log.CloseAndFlush(); |
|
|
|
} |
|
|
|
|
|
|
|
private static IConfiguration GetConfiguration() |
|
|
|
{ |
|
|
|
var builder = new ConfigurationBuilder() |
|
|
|
.SetBasePath(Directory.GetCurrentDirectory()) |
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
|
|
|
.AddEnvironmentVariables(); |
|
|
|
IWebHost BuildWebHost(IConfiguration configuration, string[] args) => |
|
|
|
WebHost.CreateDefaultBuilder(args) |
|
|
|
.CaptureStartupErrors(false) |
|
|
|
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration)) |
|
|
|
.UseStartup<Startup>() |
|
|
|
.UseContentRoot(Directory.GetCurrentDirectory()) |
|
|
|
.UseSerilog() |
|
|
|
.Build(); |
|
|
|
|
|
|
|
var config = builder.Build(); |
|
|
|
Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) |
|
|
|
{ |
|
|
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"]; |
|
|
|
var logstashUrl = configuration["Serilog:LogstashgUrl"]; |
|
|
|
return new LoggerConfiguration() |
|
|
|
.MinimumLevel.Verbose() |
|
|
|
.Enrich.WithProperty("ApplicationContext", AppName) |
|
|
|
.Enrich.FromLogContext() |
|
|
|
.WriteTo.Console() |
|
|
|
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl) |
|
|
|
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://localhost:8080" : logstashUrl) |
|
|
|
.ReadFrom.Configuration(configuration) |
|
|
|
.CreateLogger(); |
|
|
|
} |
|
|
|
|
|
|
|
if (config.GetValue<bool>("UseVault", false)) |
|
|
|
{ |
|
|
|
builder.AddAzureKeyVault( |
|
|
|
$"https://{config["Vault:Name"]}.vault.azure.net/", |
|
|
|
config["Vault:ClientId"], |
|
|
|
config["Vault:ClientSecret"]); |
|
|
|
} |
|
|
|
IConfiguration GetConfiguration() |
|
|
|
{ |
|
|
|
var builder = new ConfigurationBuilder() |
|
|
|
.SetBasePath(Directory.GetCurrentDirectory()) |
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
|
|
|
.AddEnvironmentVariables(); |
|
|
|
|
|
|
|
return builder.Build(); |
|
|
|
} |
|
|
|
var config = builder.Build(); |
|
|
|
|
|
|
|
if (config.GetValue<bool>("UseVault", false)) |
|
|
|
{ |
|
|
|
builder.AddAzureKeyVault( |
|
|
|
$"https://{config["Vault:Name"]}.vault.azure.net/", |
|
|
|
config["Vault:ClientId"], |
|
|
|
config["Vault:ClientSecret"]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return builder.Build(); |
|
|
|
} |