105 lines
4.4 KiB
C#
105 lines
4.4 KiB
C#
using Core.Blueprint.Storage.Configuration;
|
|
using Core.Cerberos.Infraestructure.Caching.Contracts;
|
|
using Core.Cerberos.Infraestructure.Contexts.Mongo;
|
|
using Core.Cerberos.Provider.Contracts;
|
|
using Core.Cerberos.Provider.Providers;
|
|
using Core.Cerberos.Provider.Providers.Onboarding;
|
|
using LSA.Core.Dapper.Service.Caching;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using MongoDB.Driver;
|
|
|
|
namespace Core.Cerberos.Provider
|
|
{
|
|
public static class ServiceCollectionExtensions
|
|
{
|
|
public static IServiceCollection AddDALLayer(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
|
|
|
|
var connectionString = configuration.GetSection("ConnectionStrings:MongoDB").Value ?? string.Empty;
|
|
var databaseName = configuration.GetSection("MongoDB:DatabaseName").Value ?? string.Empty;
|
|
var audience = (environment == "Local")
|
|
? configuration.GetSection("MongoDB:LocalAudience").Value
|
|
: configuration.GetSection("MongoDB:Audience").Value;
|
|
|
|
if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(databaseName) || string.IsNullOrEmpty(audience))
|
|
{
|
|
throw new InvalidOperationException("Mongo connection is not configured correctly.");
|
|
}
|
|
|
|
services.Configure<MongoConnSettings>(options =>
|
|
{
|
|
options.ConnectionString = connectionString;
|
|
options.Databasename = databaseName;
|
|
options.Audience = audience ?? string.Empty;
|
|
});
|
|
|
|
services.AddSingleton<IMongoClient>(serviceProvider =>
|
|
{
|
|
var settings = serviceProvider.GetRequiredService<IOptions<MongoConnSettings>>().Value;
|
|
var mongoClientSettings = MongoClientSettings.FromConnectionString(settings.ConnectionString);
|
|
mongoClientSettings.Credential = MongoCredential.CreateOidcCredential(new HeathOidcCallback(settings.Audience));
|
|
return new MongoClient(mongoClientSettings);
|
|
});
|
|
|
|
services.AddSingleton<IMongoDatabase>(serviceProvider =>
|
|
{
|
|
var settings = serviceProvider.GetRequiredService<IOptions<MongoConnSettings>>().Value;
|
|
var client = serviceProvider.GetRequiredService<IMongoClient>();
|
|
return client.GetDatabase(settings.Databasename);
|
|
});
|
|
|
|
services.AddDALConfigurationLayer();
|
|
services.AddLogs();
|
|
services.AddRedisCacheService(configuration);
|
|
services.AddBlobStorage(configuration);
|
|
|
|
return services;
|
|
}
|
|
|
|
|
|
private static IServiceCollection AddDALConfigurationLayer(this IServiceCollection services)
|
|
{
|
|
services.AddHttpContextAccessor();
|
|
|
|
services.AddScoped<IUserService, UserService>();
|
|
services.AddScoped<IRoleService, RoleService>();
|
|
services.AddScoped<IPermissionService, PermissionService>();
|
|
services.AddScoped<IPermissionService, PermissionService>();
|
|
services.AddScoped<IModuleService, ModuleService>();
|
|
return services;
|
|
}
|
|
|
|
private static IServiceCollection AddLogs(this IServiceCollection services)
|
|
{
|
|
services.AddLogging();
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
//var logger = serviceProvider.GetService<ILogger<DashboardDALService>>(); //Add for Markup class later TODO
|
|
|
|
//services.AddSingleton(typeof(ILogger), logger);
|
|
|
|
return services;
|
|
}
|
|
|
|
private static IServiceCollection AddRedisCacheService(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var source = configuration.GetSection("ConnectionStrings");
|
|
|
|
var redisConnectionString = source["Redis"]?.ToString();
|
|
|
|
if (string.IsNullOrEmpty(redisConnectionString))
|
|
{
|
|
throw new InvalidOperationException("Redis connection string is not configured.");
|
|
}
|
|
|
|
services.AddSingleton<ICacheService>(provider =>
|
|
new CacheService(redisConnectionString, provider.GetRequiredService<ILogger<CacheService>>()));
|
|
|
|
return services;
|
|
}
|
|
}
|
|
}
|