using HopFrame.Api.Controller;
using HopFrame.Api.Logic;
using HopFrame.Api.Logic.Implementation;
using HopFrame.Api.Models;
using HopFrame.Database;
using HopFrame.Security.Authentication;
using HopFrame.Security.Authentication.OpenID;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace HopFrame.Api.Extensions;
public static class ServiceCollectionExtensions {
///
/// Adds all HopFrame endpoints and services to the application
///
/// The service provider to add the services to
/// The configuration used to configure HopFrame authentication
/// Configuration for how the HopFrame services are set up
/// The data source for all HopFrame entities
public static void AddHopFrame(this IServiceCollection services, ConfigurationManager configuration, HopFrameApiModuleConfig config = null) where TDbContext : HopDbContextBase {
config ??= new();
var controllers = new List();
if (config.ExposeModelEndpoints)
controllers.AddRange([typeof(UserController), typeof(GroupController)]);
var defaultAuthenticationSection = configuration.GetSection("HopFrame:Authentication:DefaultAuthentication");
if ((!defaultAuthenticationSection.Exists() || configuration.GetValue("HopFrame:Authentication:DefaultAuthentication")) && config.ExposeAuthEndpoints)
controllers.Add(typeof(AuthController));
if (configuration.GetValue("HopFrame:Authentication:OpenID:Enabled") && config.ExposeAuthEndpoints) {
IOpenIdAccessor.DefaultCallback = OpenIdController.DefaultCallback;
controllers.Add(typeof(OpenIdController));
}
AddHopFrameNoEndpoints(services, configuration, config);
services.AddMvcCore().UseSpecificControllers(controllers.ToArray());
}
///
/// Adds all HopFrame services to the application
///
/// The service provider to add the services to
/// The configuration used to configure HopFrame authentication
/// Configuration for how the HopFrame services are set up
/// The data source for all HopFrame entities
public static void AddHopFrameNoEndpoints(this IServiceCollection services, ConfigurationManager configuration, HopFrameApiModuleConfig config = null) where TDbContext : HopDbContextBase {
config ??= new();
services.AddMvcCore().ConfigureApplicationPartManager(manager => {
var endpoints = manager.ApplicationParts.SingleOrDefault(p => p.Name == typeof(ServiceCollectionExtensions).Namespace!.Replace(".Extensions", ""));
manager.ApplicationParts.Remove(endpoints);
});
services.AddSingleton(config);
services.AddHopFrameRepositories();
services.TryAddSingleton();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddHopFrameAuthentication(configuration, config);
}
}