oliverbooth.dev/OliverBooth/Program.cs

91 lines
2.9 KiB
C#
Raw Normal View History

2023-08-10 22:33:15 +00:00
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
2023-08-07 23:34:15 +00:00
using Markdig;
using NLog;
2023-08-06 14:56:08 +00:00
using NLog.Extensions.Logging;
using OliverBooth.Data;
using OliverBooth.Markdown;
using OliverBooth.Markdown.Timestamp;
using OliverBooth.Middleware;
2023-08-06 14:56:08 +00:00
using OliverBooth.Services;
using X10D.Hosting.DependencyInjection;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddTomlFile("data/config.toml", true, true);
2023-05-26 18:05:44 +00:00
2023-08-06 14:56:08 +00:00
builder.Logging.ClearProviders();
builder.Logging.AddNLog();
builder.Services.AddHostedSingleton<LoggingService>();
builder.Services.AddSingleton<ConfigurationService>();
builder.Services.AddSingleton<TemplateService>();
builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder()
.Use<TimestampExtension>()
2023-08-09 22:19:19 +00:00
.Use(new TemplateExtension(provider.GetRequiredService<TemplateService>()))
2023-08-07 23:34:15 +00:00
.UseAdvancedExtensions()
.UseBootstrap()
2023-08-09 22:19:39 +00:00
.UseEmojiAndSmiley()
2023-08-09 22:19:29 +00:00
.UseSmartyPants()
2023-08-07 23:34:15 +00:00
.Build());
2023-08-06 14:56:08 +00:00
builder.Services.AddDbContextFactory<BlogContext>();
builder.Services.AddDbContextFactory<WebContext>();
builder.Services.AddSingleton<BlogService>();
2023-08-06 14:55:12 +00:00
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
2023-08-05 20:01:47 +00:00
builder.Services.AddControllersWithViews();
builder.Services.AddRouting(options => options.LowercaseUrls = true);
2023-05-26 18:05:44 +00:00
2023-08-10 22:33:15 +00:00
builder.WebHost.UseKestrel(kestrel =>
{
string certPath = Environment.GetEnvironmentVariable("SSL_CERT_PATH")!;
if (!File.Exists(certPath))
{
kestrel.ListenAnyIP(5049);
return;
}
string? keyPath = Environment.GetEnvironmentVariable("SSL_KEY_PATH");
if (string.IsNullOrWhiteSpace(keyPath) || !File.Exists(keyPath)) keyPath = null;
kestrel.ListenAnyIP(2845, options =>
{
X509Certificate2 cert = CreateCertFromPemFile(certPath, keyPath);
options.UseHttps(cert);
});
return;
static X509Certificate2 CreateCertFromPemFile(string certPath, string? keyPath)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return X509Certificate2.CreateFromPemFile(certPath, keyPath);
//workaround for windows issue https://github.com/dotnet/runtime/issues/23749#issuecomment-388231655
using var cert = X509Certificate2.CreateFromPemFile(certPath, keyPath);
return new X509Certificate2(cert.Export(X509ContentType.Pkcs12));
}
});
WebApplication app = builder.Build();
2023-05-26 18:05:44 +00:00
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
2023-08-05 19:58:03 +00:00
2023-08-05 20:01:47 +00:00
app.MapControllers();
2023-08-11 13:08:14 +00:00
app.MapControllerRoute("scoped", "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
2023-05-26 18:05:44 +00:00
app.MapRazorPages();
app.MapRssFeed("/blog/feed");
2023-05-26 18:05:44 +00:00
app.Run();
LogManager.Shutdown();