70 lines
2.1 KiB
C#
70 lines
2.1 KiB
C#
using Azure.Identity;
|
|
using Hutopy.Application.Common.Interfaces;
|
|
using Hutopy.Domain.Interfaces;
|
|
using Hutopy.Infrastructure.Data;
|
|
using Hutopy.Infrastructure.Services;
|
|
using Hutopy.Web.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NSwag;
|
|
using NSwag.Generation.Processors.Security;
|
|
|
|
namespace Hutopy.Web;
|
|
|
|
public static class DependencyInjection
|
|
{
|
|
public static IServiceCollection AddWebServices(this IServiceCollection services)
|
|
{
|
|
services.AddDatabaseDeveloperPageExceptionFilter();
|
|
|
|
services.AddScoped<IUser, CurrentUser>();
|
|
services.AddScoped<IUserService, UserService>();
|
|
|
|
|
|
services.AddHttpContextAccessor();
|
|
|
|
services.AddHealthChecks()
|
|
.AddDbContextCheck<ApplicationDbContext>();
|
|
|
|
services.AddExceptionHandler<CustomExceptionHandler>();
|
|
|
|
services.AddRazorPages();
|
|
|
|
// Customise default API behaviour
|
|
services.Configure<ApiBehaviorOptions>(options =>
|
|
options.SuppressModelStateInvalidFilter = true);
|
|
|
|
services.AddEndpointsApiExplorer();
|
|
|
|
services.AddOpenApiDocument((configure, sp) =>
|
|
{
|
|
configure.Title = "Hutopy API";
|
|
|
|
// Add JWT
|
|
configure.AddSecurity("JWT", Enumerable.Empty<string>(), new OpenApiSecurityScheme
|
|
{
|
|
Type = OpenApiSecuritySchemeType.ApiKey,
|
|
Name = "Authorization",
|
|
In = OpenApiSecurityApiKeyLocation.Header,
|
|
Description = "Type into the textbox: Bearer {your JWT token}."
|
|
});
|
|
|
|
configure.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT"));
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IServiceCollection AddKeyVaultIfConfigured(this IServiceCollection services, ConfigurationManager configuration)
|
|
{
|
|
var keyVaultUri = configuration["KeyVaultUri"];
|
|
if (!string.IsNullOrWhiteSpace(keyVaultUri))
|
|
{
|
|
configuration.AddAzureKeyVault(
|
|
new Uri(keyVaultUri),
|
|
new DefaultAzureCredential());
|
|
}
|
|
|
|
return services;
|
|
}
|
|
}
|