proxmox-connector/src/server/ProxmoxConnector.Server/Startup.cs

80 lines
2.8 KiB
C#

using MediatR;
using MediatR.Pipeline;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NDB.Extensions.Swagger;
using NDB.Extensions.Swagger.Constants;
using NDB.Infrastructure.DatabaseMigration;
using NDB.Security.Authentication.Identity;
using Newtonsoft.Json;
using ProxmoxConnector.Server.Application;
using ProxmoxConnector.Server.Domain.Abstractions;
using ProxmoxConnector.Server.Services;
using System.Reflection;
namespace ProxmoxConnector.Server
{
public class Startup
{
private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson(o => o.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc);
// Add basic authentication
services.AddIdentityAuthentication(_configuration.GetSection("IdentityServer")["BaseAddress"]);
// MediatR
services.AddMediatR(GetMediatRAssemblies());
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));
services.AddHttpContextAccessor();
services.AddScoped<IUserService, UserService>();
services.AddSwagger("ProxmoxConnector.Server", AuthorizationType.InhouseIdentity);
services.AddAutoMapper(typeof(Application.Mappings.MappingProfile).Assembly);
var workspace = _configuration.GetValue<string>("Workspace");
services.AddMigration(workspace: workspace);
//services.AddDataAccess();
services.AddApplicationServices();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseExceptionHandler("/error");
app.ConfigureSwagger("CDN.Server v1");
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseMigration();
}
private Assembly[] GetMediatRAssemblies()
{
var assembly = typeof(Application.Queries.System.GetSystemVersion).Assembly;
return new Assembly[] { assembly };
}
}
}