2023-09-29 15:47:29 +01:00
|
|
|
using AspNetCore.ReCaptcha;
|
2023-08-13 17:33:54 +01:00
|
|
|
using Markdig;
|
2024-05-05 02:18:20 +01:00
|
|
|
using OliverBooth.Common.Services;
|
2023-08-13 17:33:54 +01:00
|
|
|
using OliverBooth.Data.Blog;
|
|
|
|
using OliverBooth.Data.Web;
|
|
|
|
using OliverBooth.Extensions;
|
2024-05-05 02:18:20 +01:00
|
|
|
using OliverBooth.Extensions.Markdig;
|
2024-05-05 02:21:40 +01:00
|
|
|
using OliverBooth.Extensions.Markdig.Markdown.Timestamp;
|
2024-05-05 02:18:20 +01:00
|
|
|
using OliverBooth.Extensions.Markdig.Services;
|
2023-08-06 15:56:08 +01:00
|
|
|
using OliverBooth.Services;
|
2023-08-12 21:06:48 +01:00
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
|
.WriteTo.Console()
|
|
|
|
.WriteTo.File("logs/latest.log", rollingInterval: RollingInterval.Day)
|
2023-08-15 17:05:16 +01:00
|
|
|
#if DEBUG
|
|
|
|
.MinimumLevel.Debug()
|
|
|
|
#endif
|
2023-08-12 21:06:48 +01:00
|
|
|
.CreateLogger();
|
2023-08-06 15:56:08 +01:00
|
|
|
|
2023-08-04 00:38:44 +01:00
|
|
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
2023-08-06 15:57:23 +01:00
|
|
|
builder.Configuration.AddTomlFile("data/config.toml", true, true);
|
2023-08-06 15:56:08 +01:00
|
|
|
builder.Logging.ClearProviders();
|
2023-08-12 21:06:48 +01:00
|
|
|
builder.Logging.AddSerilog();
|
2023-08-12 20:40:46 +01:00
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder()
|
|
|
|
.Use<TimestampExtension>()
|
2024-05-05 02:18:20 +01:00
|
|
|
.UseTemplates(provider.GetRequiredService<ITemplateService>())
|
2024-05-03 23:31:47 +01:00
|
|
|
|
|
|
|
// we have our own "alert blocks"
|
|
|
|
.UseCallouts()
|
|
|
|
|
|
|
|
// advanced extensions. add explicitly to avoid UseAlertBlocks
|
|
|
|
.UseAbbreviations()
|
|
|
|
.UseAutoIdentifiers()
|
|
|
|
.UseCitations()
|
|
|
|
.UseCustomContainers()
|
|
|
|
.UseDefinitionLists()
|
|
|
|
.UseEmphasisExtras()
|
|
|
|
.UseFigures()
|
|
|
|
.UseFooters()
|
|
|
|
.UseFootnotes()
|
|
|
|
.UseGridTables()
|
|
|
|
.UseMathematics()
|
|
|
|
.UseMediaLinks()
|
|
|
|
.UsePipeTables()
|
|
|
|
.UseListExtras()
|
|
|
|
.UseTaskLists()
|
|
|
|
.UseDiagrams()
|
|
|
|
.UseAutoLinks()
|
|
|
|
.UseGenericAttributes() // must be last as it is one parser that is modifying other parsers
|
|
|
|
|
|
|
|
// no more advanced extensions
|
2023-08-13 17:33:54 +01:00
|
|
|
.UseBootstrap()
|
|
|
|
.UseEmojiAndSmiley()
|
|
|
|
.UseSmartyPants()
|
|
|
|
.Build());
|
|
|
|
|
|
|
|
builder.Services.AddDbContextFactory<BlogContext>();
|
2023-08-06 15:57:23 +01:00
|
|
|
builder.Services.AddDbContextFactory<WebContext>();
|
2024-02-23 03:23:57 +00:00
|
|
|
builder.Services.AddHttpClient();
|
2024-04-27 00:25:32 +01:00
|
|
|
builder.Services.AddSingleton<ICodeSnippetService, CodeSnippetService>();
|
2023-12-22 14:26:18 +00:00
|
|
|
builder.Services.AddSingleton<IContactService, ContactService>();
|
2023-08-13 17:33:54 +01:00
|
|
|
builder.Services.AddSingleton<ITemplateService, TemplateService>();
|
|
|
|
builder.Services.AddSingleton<IBlogPostService, BlogPostService>();
|
|
|
|
builder.Services.AddSingleton<IBlogUserService, BlogUserService>();
|
2024-04-27 00:25:32 +01:00
|
|
|
builder.Services.AddSingleton<IProgrammingLanguageService, ProgrammingLanguageService>();
|
2023-09-24 14:36:36 +01:00
|
|
|
builder.Services.AddSingleton<IProjectService, ProjectService>();
|
2024-02-23 03:23:57 +00:00
|
|
|
builder.Services.AddSingleton<IMastodonService, MastodonService>();
|
2024-02-20 20:36:23 +00:00
|
|
|
builder.Services.AddSingleton<ITutorialService, TutorialService>();
|
2023-12-14 16:03:24 +00:00
|
|
|
builder.Services.AddSingleton<IReadingListService, ReadingListService>();
|
2024-05-05 21:10:23 +01:00
|
|
|
builder.Services.AddRazorPages();
|
2023-08-05 21:01:47 +01:00
|
|
|
builder.Services.AddControllersWithViews();
|
2023-08-04 00:38:44 +01:00
|
|
|
builder.Services.AddRouting(options => options.LowercaseUrls = true);
|
2023-09-29 15:47:29 +01:00
|
|
|
builder.Services.AddReCaptcha(builder.Configuration.GetSection("ReCaptcha"));
|
2023-05-26 19:05:44 +01:00
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
if (builder.Environment.IsProduction())
|
|
|
|
{
|
|
|
|
builder.WebHost.AddCertificateFromEnvironment(2845, 5049);
|
|
|
|
}
|
2023-08-10 23:33:15 +01:00
|
|
|
|
2023-08-04 00:38:44 +01:00
|
|
|
WebApplication app = builder.Build();
|
2023-05-26 19:05:44 +01:00
|
|
|
|
2024-05-05 20:55:13 +01:00
|
|
|
app.Use(async (ctx, next) =>
|
|
|
|
{
|
|
|
|
await next();
|
|
|
|
|
|
|
|
if (ctx.Response.HasStarted)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string? originalPath = ctx.Request.Path.Value;
|
|
|
|
ctx.Items["originalPath"] = originalPath;
|
|
|
|
|
|
|
|
bool matchedErrorPage = false;
|
|
|
|
|
|
|
|
switch (ctx.Response.StatusCode)
|
|
|
|
{
|
|
|
|
case 400:
|
|
|
|
ctx.Request.Path = "/error/401";
|
|
|
|
matchedErrorPage = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 403:
|
|
|
|
ctx.Request.Path = "/error/403";
|
|
|
|
matchedErrorPage = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 404:
|
|
|
|
ctx.Request.Path = "/error/404";
|
|
|
|
matchedErrorPage = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 410:
|
|
|
|
ctx.Request.Path = "/error/410";
|
|
|
|
matchedErrorPage = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 418:
|
|
|
|
ctx.Request.Path = "/error/418";
|
|
|
|
matchedErrorPage = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 429:
|
|
|
|
ctx.Request.Path = "/error/429";
|
|
|
|
matchedErrorPage = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 500:
|
|
|
|
ctx.Request.Path = "/error/500";
|
|
|
|
matchedErrorPage = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 503:
|
|
|
|
ctx.Request.Path = "/error/503";
|
|
|
|
matchedErrorPage = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 504:
|
|
|
|
ctx.Request.Path = "/error/504";
|
|
|
|
matchedErrorPage = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchedErrorPage)
|
|
|
|
{
|
|
|
|
await next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
app.UseStatusCodePagesWithReExecute("/error/{0}");
|
|
|
|
|
2023-05-26 19:05:44 +01:00
|
|
|
if (!app.Environment.IsDevelopment())
|
|
|
|
{
|
2024-05-05 20:55:13 +01:00
|
|
|
app.UseExceptionHandler("/error/500");
|
2023-05-26 19:05:44 +01:00
|
|
|
// 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 20:58:03 +01:00
|
|
|
|
2023-08-05 21:01:47 +01:00
|
|
|
app.MapControllers();
|
2023-05-26 19:05:44 +01:00
|
|
|
app.MapRazorPages();
|
|
|
|
|
|
|
|
app.Run();
|