refact: Fully refactor the conceptional structure and functional behaviour.

This commit is contained in:
2024-06-21 22:59:30 +08:00
parent 257f5d94ee
commit 2efe2fd5cd
64 changed files with 307 additions and 574 deletions

View File

@@ -1,59 +0,0 @@
using Markdig;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using YaeBlog.Core.Builder;
using YaeBlog.Core.Models;
using YaeBlog.Core.Processors;
using YaeBlog.Core.Services;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace YaeBlog.Core.Extensions;
public static class BlogApplicationBuilderExtension
{
internal static void ConfigureDefaultBlogApplicationBuilder(this BlogApplicationBuilder builder)
{
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json",
optional: true, reloadOnChange: true);
builder.Configuration.AddEnvironmentVariables();
builder.Services.Configure<BlogOptions>(
builder.Configuration.GetSection(BlogOptions.OptionName));
builder.YamlDeserializerBuilder.WithNamingConvention(CamelCaseNamingConvention.Instance);
builder.YamlDeserializerBuilder.IgnoreUnmatchedProperties();
builder.Services.AddSingleton<MarkdownPipeline>(
_ => builder.MarkdigPipelineBuilder.Build());
builder.Services.AddSingleton<IDeserializer>(
_ => builder.YamlDeserializerBuilder.Build());
builder.Services.AddHostedService<BlogHostedService>();
builder.Services.AddSingleton<EssayScanService>();
builder.Services.AddSingleton<RendererService>();
builder.Services.AddSingleton<EssayContentService>();
// 设置图像处理器
builder.Services.AddSingleton<ImagePostRenderProcessor>();
ImagePostRenderProcessor.AddImageApiEndpoint(builder);
builder.Services.AddHostedService<WebApplicationHostedService>((provider)
=> new WebApplicationHostedService(builder.WebApplicationBuilderConfigurations,
builder.WebApplicationConfigurations, provider));
}
public static void ConfigureWebApplicationBuilder(this BlogApplicationBuilder builder,
Action<WebApplicationBuilder> configureWebApplicationBuilder)
{
builder.WebApplicationBuilderConfigurations.Add(configureWebApplicationBuilder);
}
public static void ConfigureWebApplication(this BlogApplicationBuilder builder,
Action<WebApplication> configureWebApplication)
{
builder.WebApplicationConfigurations.Add(configureWebApplication);
}
}

View File

@@ -1,35 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using YaeBlog.Core.Abstractions;
using YaeBlog.Core.Builder;
using YaeBlog.Core.Processors;
using YaeBlog.Core.Services;
namespace YaeBlog.Core.Extensions;
public static class BlogApplicationExtension
{
internal static void ConfigureDefaultBlogApplication(this BlogApplication application)
{
application.UsePostRenderProcessor<ImagePostRenderProcessor>();
}
public static void UsePreRenderProcessor<T>(this BlogApplication application)
where T : IPreRenderProcessor
{
RendererService rendererService =
application.Services.GetRequiredService<RendererService>();
T preRenderProcessor =
application.Services.GetRequiredService<T>();
rendererService.AddPreRenderProcessor(preRenderProcessor);
}
public static void UsePostRenderProcessor<T>(this BlogApplication application)
where T : IPostRenderProcessor
{
RendererService rendererService =
application.Services.GetRequiredService<RendererService>();
T postRenderProcessor =
application.Services.GetRequiredService<T>();
rendererService.AddPostRenderProcessor(postRenderProcessor);
}
}

View File

@@ -0,0 +1,30 @@
using Markdig;
using Microsoft.Extensions.DependencyInjection;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace YaeBlog.Core.Extensions;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddMarkdig(this IServiceCollection collection)
{
MarkdownPipelineBuilder builder = new();
collection.AddSingleton<MarkdownPipeline>(_ => builder.Build());
return collection;
}
public static IServiceCollection AddYamlParser(this IServiceCollection collection)
{
DeserializerBuilder builder = new();
builder.WithNamingConvention(CamelCaseNamingConvention.Instance);
builder.IgnoreUnmatchedProperties();
collection.AddSingleton<IDeserializer>(_ => builder.Build());
return collection;
}
}

View File

@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.FluentUI.AspNetCore.Components;
using YaeBlog.Core.Models;
using YaeBlog.Core.Processors;
using YaeBlog.Core.Services;
namespace YaeBlog.Core.Extensions;
public static class WebApplicationBuilderExtensions
{
public static WebApplicationBuilder AddYaeBlog(this WebApplicationBuilder builder)
{
builder.Services.Configure<BlogOptions>(builder.Configuration.GetSection(BlogOptions.OptionName));
builder.Services.AddHttpClient();
builder.Services.AddFluentUIComponents();
builder.Services.AddMarkdig();
builder.Services.AddYamlParser();
builder.Services.AddSingleton<EssayScanService>();
builder.Services.AddSingleton<RendererService>();
builder.Services.AddSingleton<EssayContentService>();
builder.Services.AddTransient<ImagePostRenderProcessor>();
builder.Services.AddTransient<BlogOptions>(provider =>
provider.GetRequiredService<IOptions<BlogOptions>>().Value);
builder.Services.AddHostedService<BlogHostedService>();
return builder;
}
}

View File

@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using YaeBlog.Core.Abstractions;
using YaeBlog.Core.Processors;
using YaeBlog.Core.Services;
namespace YaeBlog.Core.Extensions;
public static class WebApplicationExtensions
{
public static WebApplication UseMiddleRenderProcessors(this WebApplication application)
{
application.UsePostRenderProcessor<ImagePostRenderProcessor>();
return application;
}
private static void UsePreRenderProcessor<T>(this WebApplication application) where T : IPreRenderProcessor
{
RendererService rendererService = application.Services.GetRequiredService<RendererService>();
T preRenderProcessor = application.Services.GetRequiredService<T>();
rendererService.AddPreRenderProcessor(preRenderProcessor);
}
private static void UsePostRenderProcessor<T>(this WebApplication application) where T : IPostRenderProcessor
{
RendererService rendererService = application.Services.GetRequiredService<RendererService>();
T postRenderProcessor = application.Services.GetRequiredService<T>();
rendererService.AddPostRenderProcessor(postRenderProcessor);
}
}