Skip to content

Commit

Permalink
add more di extensions method (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoli799480165 authored Jun 12, 2024
1 parent 66e114c commit 8fe3e37
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@
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;

public static class ServiceCollectionExtensions
{
public static IRhoAiasConfigurationBuilder AddRhoAiasLetsEncrypt(this IRhoAiasConfigurationBuilder builder)
{
builder.Services.AddMemoryCache();
builder.Services.AddOptions<RhoAiasLetsEncryptOptions>("RhoAias:Acme:LetsEncrypt");
builder.Services.AddKeyedSingleton<IAcmeProvider, LetsEncryptAcmeProvider>("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<RhoAiasLetsEncryptOptions>("RhoAias:Acme:LetsEncrypt");
services.AddKeyedSingleton<IAcmeProvider, LetsEncryptAcmeProvider>("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";
Expand All @@ -32,6 +43,6 @@ public static IRhoAiasApplicationBuilder UseRhoAiasLetsEncrypt(this IRhoAiasAppl

context.Response.StatusCode = 404;
}).ExcludeFromDescription(); // ignore for swagger
return app;
return routeBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ public static class ServiceCollectionExtensions
{
public static IRhoAiasConfigurationBuilder AddRhoAiasAliyunDns(this IRhoAiasConfigurationBuilder builder)
{
builder.Services.AddKeyedSingleton<IDnsProvider, AliyunDnsProvider>("Aliyun");
builder.Services.AddRhoAiasAliyunDns();
return builder;
}

public static IServiceCollection AddRhoAiasAliyunDns(this IServiceCollection services)
{
services.AddKeyedSingleton<IDnsProvider, AliyunDnsProvider>("Aliyun");
return services;
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<RhoAiasPrometheusOptions>(configKey);
builder.Configuration.GetSection(configKey).Bind(options);
builder.Services.AddOpenTelemetry().WithMetrics(b =>
services.AddOptions<RhoAiasPrometheusOptions>(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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,16 +12,27 @@ public static class ServiceCollectionExtensions
{
public static IRhoAiasConfigurationBuilder AddRhoAiasSqlite(this IRhoAiasConfigurationBuilder builder)
{
var configuration = builder.Configuration;
builder.Services.AddDbContextFactory<RhoAiasDbContext>(options => options.UseSqlite(configuration.GetRhoAiasConnectionString()));
builder.Services.Replace(new ServiceDescriptor(typeof(IRepository<>), typeof(SqliteRepository<>), ServiceLifetime.Singleton));
builder.Services.AddTransient<IDbMigrator, RhoAiasDbMigrator>();
builder.Services.AddRhoAiasSqlite(builder.Configuration);
return builder;
}

public static IRhoAiasApplicationBuilder UseRhoAiasSqlite(this IRhoAiasApplicationBuilder app)
{
var migrator = app.Services.GetService<IDbMigrator>();
app.ApplicationBuilder.UseRhoAiasSqlite();
return app;
}

public static IServiceCollection AddRhoAiasSqlite(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContextFactory<RhoAiasDbContext>(options => options.UseSqlite(configuration.GetRhoAiasConnectionString()));
services.Replace(new ServiceDescriptor(typeof(IRepository<>), typeof(SqliteRepository<>), ServiceLifetime.Singleton));
services.AddTransient<IDbMigrator, RhoAiasDbMigrator>();
return services;
}

public static IApplicationBuilder UseRhoAiasSqlite(this IApplicationBuilder app)
{
var migrator = app.ApplicationServices.GetService<IDbMigrator>();
migrator?.MigrateAsync().Wait();
return app;
}
Expand Down

0 comments on commit 8fe3e37

Please sign in to comment.