diff --git a/src/Chaldea.Fate.RhoAias.Acme.LetsEncrypt/ServiceCollectionExtensions.cs b/src/Chaldea.Fate.RhoAias.Acme.LetsEncrypt/ServiceCollectionExtensions.cs index a9c059a..c3f8e0e 100644 --- a/src/Chaldea.Fate.RhoAias.Acme.LetsEncrypt/ServiceCollectionExtensions.cs +++ b/src/Chaldea.Fate.RhoAias.Acme.LetsEncrypt/ServiceCollectionExtensions.cs @@ -2,8 +2,8 @@ using Chaldea.Fate.RhoAias.Acme.LetsEncrypt; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Caching.Memory; -using Microsoft.Extensions.Logging; namespace Microsoft.Extensions.DependencyInjection; @@ -11,18 +11,29 @@ public static class ServiceCollectionExtensions { public static IRhoAiasConfigurationBuilder AddRhoAiasLetsEncrypt(this IRhoAiasConfigurationBuilder builder) { - builder.Services.AddMemoryCache(); - builder.Services.AddOptions("RhoAias:Acme:LetsEncrypt"); - builder.Services.AddKeyedSingleton("LetsEncrypt"); + builder.Services.AddRhoAiasLetsEncrypt(); return builder; } public static IRhoAiasApplicationBuilder UseRhoAiasLetsEncrypt(this IRhoAiasApplicationBuilder app) { - app.EndpointRouteBuilder.MapGet("/.well-known/acme-challenge/{token}", + app.EndpointRouteBuilder.MapRhoAiasLetsEncrypt(); + return app; + } + + public static IServiceCollection AddRhoAiasLetsEncrypt(this IServiceCollection services) + { + services.AddMemoryCache(); + services.AddOptions("RhoAias:Acme:LetsEncrypt"); + services.AddKeyedSingleton("LetsEncrypt"); + return services; + } + + public static IEndpointRouteBuilder MapRhoAiasLetsEncrypt(this IEndpointRouteBuilder routeBuilder) + { + routeBuilder.MapGet("/.well-known/acme-challenge/{token}", async (IMemoryCache cache, HttpContext context, string token) => { - app.Logger.LogInformation($"acme-challenge: {token}"); if (cache.TryGetValue(token, out var value)) { context.Response.ContentType = "text/plain"; @@ -32,6 +43,6 @@ public static IRhoAiasApplicationBuilder UseRhoAiasLetsEncrypt(this IRhoAiasAppl context.Response.StatusCode = 404; }).ExcludeFromDescription(); // ignore for swagger - return app; + return routeBuilder; } } \ No newline at end of file diff --git a/src/Chaldea.Fate.RhoAias.Dns.Aliyun/ServiceCollectionExtensions.cs b/src/Chaldea.Fate.RhoAias.Dns.Aliyun/ServiceCollectionExtensions.cs index 5cd185f..af88bd2 100644 --- a/src/Chaldea.Fate.RhoAias.Dns.Aliyun/ServiceCollectionExtensions.cs +++ b/src/Chaldea.Fate.RhoAias.Dns.Aliyun/ServiceCollectionExtensions.cs @@ -7,7 +7,13 @@ public static class ServiceCollectionExtensions { public static IRhoAiasConfigurationBuilder AddRhoAiasAliyunDns(this IRhoAiasConfigurationBuilder builder) { - builder.Services.AddKeyedSingleton("Aliyun"); + builder.Services.AddRhoAiasAliyunDns(); return builder; } + + public static IServiceCollection AddRhoAiasAliyunDns(this IServiceCollection services) + { + services.AddKeyedSingleton("Aliyun"); + return services; + } } \ No newline at end of file diff --git a/src/Chaldea.Fate.RhoAias.Metrics.Prometheus/ServiceCollectionExtensions.cs b/src/Chaldea.Fate.RhoAias.Metrics.Prometheus/ServiceCollectionExtensions.cs index b093914..f6bb8e4 100644 --- a/src/Chaldea.Fate.RhoAias.Metrics.Prometheus/ServiceCollectionExtensions.cs +++ b/src/Chaldea.Fate.RhoAias.Metrics.Prometheus/ServiceCollectionExtensions.cs @@ -1,6 +1,7 @@ using Chaldea.Fate.RhoAias; using Chaldea.Fate.RhoAias.Metrics.Prometheus; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using OpenTelemetry.Metrics; @@ -9,22 +10,34 @@ namespace Microsoft.Extensions.DependencyInjection; public static class ServiceCollectionExtensions { public static IRhoAiasConfigurationBuilder AddRhoAiasPrometheus(this IRhoAiasConfigurationBuilder builder) + { + builder.Services.AddRhoAiasPrometheus(builder.Configuration); + return builder; + } + + public static IRhoAiasApplicationBuilder UseRhoAiasPrometheus(this IRhoAiasApplicationBuilder builder) + { + builder.EndpointRouteBuilder.MapRhoAiasPrometheus(); + return builder; + } + + public static IServiceCollection AddRhoAiasPrometheus(this IServiceCollection services, IConfiguration configuration) { var configKey = "RhoAias:Metrics:Prometheus"; var options = new RhoAiasPrometheusOptions(); - builder.Services.AddOptions(configKey); - builder.Configuration.GetSection(configKey).Bind(options); - builder.Services.AddOpenTelemetry().WithMetrics(b => + services.AddOptions(configKey); + configuration.GetSection(configKey).Bind(options); + services.AddOpenTelemetry().WithMetrics(b => { b.AddPrometheusExporter(); b.AddMeter(options.Meters); }); - return builder; + return services; } - public static IRhoAiasApplicationBuilder UseRhoAiasPrometheus(this IRhoAiasApplicationBuilder builder) + public static IEndpointRouteBuilder MapRhoAiasPrometheus(this IEndpointRouteBuilder routeBuilder) { - builder.EndpointRouteBuilder.MapPrometheusScrapingEndpoint(); - return builder; + routeBuilder.MapPrometheusScrapingEndpoint(); + return routeBuilder; } } \ No newline at end of file diff --git a/src/Chaldea.Fate.RhoAias.Repository.Sqlite/ServiceCollectionExtensions.cs b/src/Chaldea.Fate.RhoAias.Repository.Sqlite/ServiceCollectionExtensions.cs index adca796..00ecd6d 100644 --- a/src/Chaldea.Fate.RhoAias.Repository.Sqlite/ServiceCollectionExtensions.cs +++ b/src/Chaldea.Fate.RhoAias.Repository.Sqlite/ServiceCollectionExtensions.cs @@ -1,6 +1,7 @@ using Chaldea.Fate.RhoAias; using Chaldea.Fate.RhoAias.Repository; using Chaldea.Fate.RhoAias.Repository.Sqlite; +using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -11,16 +12,27 @@ public static class ServiceCollectionExtensions { public static IRhoAiasConfigurationBuilder AddRhoAiasSqlite(this IRhoAiasConfigurationBuilder builder) { - var configuration = builder.Configuration; - builder.Services.AddDbContextFactory(options => options.UseSqlite(configuration.GetRhoAiasConnectionString())); - builder.Services.Replace(new ServiceDescriptor(typeof(IRepository<>), typeof(SqliteRepository<>), ServiceLifetime.Singleton)); - builder.Services.AddTransient(); + builder.Services.AddRhoAiasSqlite(builder.Configuration); return builder; } public static IRhoAiasApplicationBuilder UseRhoAiasSqlite(this IRhoAiasApplicationBuilder app) { - var migrator = app.Services.GetService(); + app.ApplicationBuilder.UseRhoAiasSqlite(); + return app; + } + + public static IServiceCollection AddRhoAiasSqlite(this IServiceCollection services, IConfiguration configuration) + { + services.AddDbContextFactory(options => options.UseSqlite(configuration.GetRhoAiasConnectionString())); + services.Replace(new ServiceDescriptor(typeof(IRepository<>), typeof(SqliteRepository<>), ServiceLifetime.Singleton)); + services.AddTransient(); + return services; + } + + public static IApplicationBuilder UseRhoAiasSqlite(this IApplicationBuilder app) + { + var migrator = app.ApplicationServices.GetService(); migrator?.MigrateAsync().Wait(); return app; }