Skip to content

Instantly share code, notes, and snippets.

@yzorg
Last active June 11, 2021 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yzorg/917c9d07aa372e56e47bb7de00b4e43d to your computer and use it in GitHub Desktop.
Save yzorg/917c9d07aa372e56e47bb7de00b4e43d to your computer and use it in GitHub Desktop.
simplest k8s Main()
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Middle.MyApp
{
class Program
{
static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
var switchMappings = new Dictionary<string, string>()
{
{ "-s", "Start" },
{ "--start", "Start" }
};
return Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddKeyPerFile(directoryPath: "/app/secrets/shared", optional: true);
config.AddKeyPerFile(directoryPath: "/app/secrets/namespace", optional: true);
config.AddCommandLine(args, switchMappings);
})
//.ConfigureLogging(opt => { opt.AddConsole(c => { c.TimestampFormat = "[HH:mm:ss] "; }); }) // CORE31
.ConfigureLogging(opt =>
{
opt.AddSimpleConsole(consoleOpt =>
{
consoleOpt.TimestampFormat = "yyyy-MM-ddTHH:mm:ss.ff ";
//consoleOpt.UseUtcTimestamp = true; // not needed, k8s node local time will be UTC, and I'd prefer not to see UTC when running on Win10/WSL
});
})
.ConfigureServices((hostContext, services) =>
{
new Startup(hostContext.Configuration)
.ConfigureServices(services);
});
}
public record Startup(IConfiguration Configuration)
{
public void ConfigureServices(IServiceCollection services)
{
services.Configure<LoaderOptions>(Configuration);
services.AddDbContextFactory<Models.AppSqlContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("AppSql"), sqlOptions =>
{
sqlOptions.EnableRetryOnFailure(3);
});
});
services.AddHostedService<Worker>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment