network-resurrector/NetworkResurrector.Api/Startup.cs

86 lines
2.7 KiB
C#
Raw Normal View History

using AutoMapper;
using MediatR;
using MediatR.Pipeline;
2020-07-09 02:14:02 +03:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NDB.Extensions.Swagger;
using NDB.Security.Authentication.Identity;
2020-11-25 17:01:14 +02:00
using NetworkResurrector.Api.Extensions;
using NetworkResurrector.Application;
using Newtonsoft.Json;
using System.Reflection;
2020-07-09 02:14:02 +03:00
namespace NetworkResurrector.Api
{
public class Startup
{
2020-11-25 17:01:14 +02:00
private readonly IConfiguration _configuration;
2020-07-09 02:14:02 +03:00
public Startup(IConfiguration configuration)
{
2020-11-25 17:01:14 +02:00
_configuration = configuration;
2020-07-09 02:14:02 +03:00
}
// 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.AddBasicAuthentication(_configuration.GetSection("IdentityServer")["BaseAddress"]);
// MediatR
services.AddMediatR(GetMediatRAssemblies());
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));
// AutoMapper
services.AddAutoMapper(
typeof(Application.Mappings.MappingProfile).Assembly);
// Swagger
services.AddSwagger("NetworkResurrector API");
// Application
services.AddApplicationServices();
2020-11-25 17:01:14 +02:00
// WakeOnLan
services.AddWakeOnLan(_configuration);
2020-07-09 02:14:02 +03:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
2020-07-09 02:14:02 +03:00
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthentication();
2020-07-09 02:14:02 +03:00
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.ConfigureSwagger("NetworkResurrector API");
}
private Assembly[] GetMediatRAssemblies()
{
var assembly = typeof(Application.Commands.WakeMachine).Assembly;
return new Assembly[] { assembly };
2020-07-09 02:14:02 +03:00
}
}
}