2020-10-25 16:15:06 +01:00
|
|
|
using AutoMapper;
|
|
|
|
using Birdmap.API.Middlewares;
|
2020-10-24 23:23:22 +02:00
|
|
|
using Birdmap.BLL;
|
|
|
|
using Birdmap.DAL;
|
2020-10-21 17:04:54 +02:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2020-10-21 11:05:17 +02:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-10-21 17:04:54 +02:00
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
using System.Text;
|
2020-10-21 11:05:17 +02:00
|
|
|
|
|
|
|
namespace Birdmap
|
|
|
|
{
|
|
|
|
public class Startup
|
|
|
|
{
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
Configuration = configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
2020-10-21 17:04:54 +02:00
|
|
|
services.AddControllersWithViews()
|
|
|
|
.AddJsonOptions(opt =>
|
|
|
|
{
|
|
|
|
//opt.JsonSerializerOptions.PropertyNamingPolicy = new JsonNamingPolicy()
|
|
|
|
});
|
|
|
|
|
2020-10-24 23:23:22 +02:00
|
|
|
services.ConfigureBLL(Configuration);
|
|
|
|
services.ConfigureDAL(Configuration);
|
2020-10-21 17:04:54 +02:00
|
|
|
|
2020-10-25 16:15:06 +01:00
|
|
|
services.AddAutoMapper(typeof(Startup));
|
|
|
|
|
2020-10-24 23:23:22 +02:00
|
|
|
var key = Encoding.ASCII.GetBytes(Configuration["Secret"]);
|
2020-10-21 17:04:54 +02:00
|
|
|
services.AddAuthentication(opt =>
|
|
|
|
{
|
|
|
|
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
})
|
|
|
|
.AddJwtBearer(opt =>
|
|
|
|
{
|
|
|
|
// opt.RequireHttpsMetadata = false;
|
|
|
|
opt.SaveToken = true;
|
|
|
|
opt.IncludeErrorDetails = true;
|
|
|
|
opt.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
{
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(key),
|
|
|
|
ValidateIssuer = false,
|
|
|
|
ValidateAudience = false
|
|
|
|
};
|
|
|
|
});
|
2020-10-21 11:05:17 +02:00
|
|
|
|
|
|
|
// In production, the React files will be served from this directory
|
|
|
|
services.AddSpaStaticFiles(configuration =>
|
|
|
|
{
|
|
|
|
configuration.RootPath = "ClientApp/build";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
{
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-25 16:15:06 +01:00
|
|
|
app.UseMiddleware<ExceptionHandlerMiddleware>();
|
2020-10-21 11:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseStaticFiles();
|
|
|
|
app.UseSpaStaticFiles();
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
2020-10-21 17:04:54 +02:00
|
|
|
app.UseAuthorization();
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
2020-10-21 11:05:17 +02:00
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
{
|
2020-10-25 16:15:06 +01:00
|
|
|
endpoints.MapHealthChecks("/health").RequireAuthorization();
|
2020-10-21 17:04:54 +02:00
|
|
|
endpoints.MapControllers();
|
2020-10-21 11:05:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
app.UseSpa(spa =>
|
|
|
|
{
|
|
|
|
spa.Options.SourcePath = "ClientApp";
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
spa.UseReactDevelopmentServer(npmScript: "start");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|