2021-08-12 18:56:38 +05:30
|
|
|
|
var configuration = GetConfiguration();
|
2021-08-31 22:07:43 +05:30
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
Log.Logger = CreateSerilogLogger(configuration);
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.Information("Configuring web host ({ApplicationContext})...", Program.AppName);
|
2021-08-31 22:07:43 +05:30
|
|
|
|
var host = BuildWebHost(configuration, args);
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
Log.Information("Starting web host ({ApplicationContext})...", Program.AppName);
|
2021-08-31 22:07:43 +05:30
|
|
|
|
host.Run();
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
}
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2021-08-31 22:07:43 +05:30
|
|
|
|
IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
|
|
|
|
|
WebHost.CreateDefaultBuilder(args)
|
|
|
|
|
.CaptureStartupErrors(false)
|
2020-12-15 14:42:56 +01:00
|
|
|
|
.ConfigureKestrel(options =>
|
|
|
|
|
{
|
|
|
|
|
var ports = GetDefinedPorts(configuration);
|
|
|
|
|
options.Listen(IPAddress.Any, ports.httpPort, listenOptions =>
|
2019-01-31 13:33:36 +00:00
|
|
|
|
{
|
2020-12-15 14:42:56 +01:00
|
|
|
|
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
options.Listen(IPAddress.Any, ports.grpcPort, listenOptions =>
|
2019-01-31 13:33:36 +00:00
|
|
|
|
{
|
2020-12-15 14:42:56 +01:00
|
|
|
|
listenOptions.Protocols = HttpProtocols.Http2;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration))
|
|
|
|
|
.UseFailing(options =>
|
2019-01-31 13:33:36 +00:00
|
|
|
|
{
|
2020-12-15 14:42:56 +01:00
|
|
|
|
options.ConfigPath = "/Failing";
|
|
|
|
|
options.NotFilteredPaths.AddRange(new[] { "/hc", "/liveness" });
|
|
|
|
|
})
|
2021-08-31 22:07:43 +05:30
|
|
|
|
.UseStartup<Startup>()
|
2020-12-15 14:42:56 +01:00
|
|
|
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
2021-08-31 22:07:43 +05:30
|
|
|
|
.UseSerilog()
|
|
|
|
|
.Build();
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2021-08-09 07:27:22 -07:00
|
|
|
|
Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
|
2020-12-15 14:42:56 +01:00
|
|
|
|
{
|
|
|
|
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
|
|
|
|
|
var logstashUrl = configuration["Serilog:LogstashgUrl"];
|
|
|
|
|
return new LoggerConfiguration()
|
|
|
|
|
.MinimumLevel.Verbose()
|
|
|
|
|
.Enrich.WithProperty("ApplicationContext", Program.AppName)
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.WriteTo.Console()
|
|
|
|
|
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
|
|
|
|
|
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl)
|
|
|
|
|
.ReadFrom.Configuration(configuration)
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
}
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
IConfiguration GetConfiguration()
|
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
|
|
.AddEnvironmentVariables();
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
var config = builder.Build();
|
2019-08-19 09:24:58 +02:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
if (config.GetValue<bool>("UseVault", false))
|
|
|
|
|
{
|
2021-06-08 17:15:15 +08:00
|
|
|
|
TokenCredential credential = new ClientSecretCredential(
|
|
|
|
|
config["Vault:TenantId"],
|
2020-12-15 14:42:56 +01:00
|
|
|
|
config["Vault:ClientId"],
|
|
|
|
|
config["Vault:ClientSecret"]);
|
2021-06-08 17:15:15 +08:00
|
|
|
|
builder.AddAzureKeyVault(new Uri($"https://{config["Vault:Name"]}.vault.azure.net/"), credential);
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
2020-12-15 14:42:56 +01:00
|
|
|
|
|
|
|
|
|
return builder.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
var grpcPort = config.GetValue("GRPC_PORT", 5001);
|
|
|
|
|
var port = config.GetValue("PORT", 80);
|
|
|
|
|
return (port, grpcPort);
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
2020-12-15 14:42:56 +01:00
|
|
|
|
|
2021-01-20 17:57:32 +01:00
|
|
|
|
public class Program
|
2021-08-31 22:07:43 +05:30
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public static string Namespace = typeof(Startup).Namespace;
|
|
|
|
|
public static string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
|
2021-06-08 17:15:15 +08:00
|
|
|
|
}
|