isn/src/isnd/Startup.cs

122 lines
4.5 KiB
C#
Raw Normal View History

2021-06-18 23:57:02 +01:00
using Microsoft.AspNetCore.Builder;
2021-04-08 02:03:17 +01:00
using Microsoft.AspNetCore.Hosting;
2021-04-25 12:12:50 +01:00
using Microsoft.EntityFrameworkCore;
2021-04-08 02:03:17 +01:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2021-04-25 12:12:50 +01:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Hosting;
2021-07-05 12:55:52 +01:00
using isn.Data;
using isn.Interfaces;
using isn.Services;
using isn.Entities;
using isn.Authorization;
using isn.Data.Roles;
2021-06-18 23:57:02 +01:00
using Microsoft.AspNetCore.Authorization;
2021-07-05 12:55:52 +01:00
using Unleash;
using System.Collections.Generic;
using System;
using Unleash.ClientFactory;
using isnd.Entities;
using Microsoft.Extensions.Options;
2021-04-08 02:03:17 +01:00
2021-07-05 12:55:52 +01:00
namespace isn
2021-04-08 02:03:17 +01:00
{
public class Startup
{
2021-04-08 03:08:20 +01:00
public Startup(IConfiguration config)
2021-04-08 02:03:17 +01:00
{
Configuration = config;
2021-07-05 12:55:52 +01:00
2021-04-08 02:03:17 +01:00
}
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)
{
2021-04-25 12:12:50 +01:00
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("DefaultConnection")));
2021-07-05 12:55:52 +01:00
2021-04-08 03:08:20 +01:00
2021-04-25 12:12:50 +01:00
services.AddIdentity<ApplicationUser, IdentityRole>()
2021-06-18 23:57:02 +01:00
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
2021-04-25 12:12:50 +01:00
.AddSignInManager()
.AddDefaultUI()
.AddDefaultTokenProviders();
2021-04-08 02:03:17 +01:00
services.AddMvc();
2021-04-08 03:08:20 +01:00
services.AddDataProtection();
2021-07-05 12:55:52 +01:00
2021-04-25 12:12:50 +01:00
services.AddTransient<IMailer, EmailSender>();
services.AddTransient<IEmailSender, EmailSender>();
2021-04-08 03:08:20 +01:00
2021-06-18 23:57:02 +01:00
services.AddAuthorization(options =>
{
options.AddPolicy(Constants.RequireAdminPolicyName,
policy => policy.RequireRole(Constants.AdministratorRoleName));
options.AddPolicy(Constants.RequireValidApiKey, policy =>
policy.Requirements.Add(new ValidApiKeyRequirement()));
});
services.AddSingleton<IAuthorizationHandler, ValidApiKeyRequirementHandler>();
2021-05-02 16:37:03 +01:00
var smtpSettingsconf = Configuration.GetSection("Smtp");
2021-04-25 12:12:50 +01:00
services.Configure<SmtpSettings>(smtpSettingsconf);
2021-05-02 16:37:03 +01:00
var nugetSettingsconf = Configuration.GetSection("Nuget");
2021-05-02 15:22:46 +01:00
services.Configure<NugetSettings>(nugetSettingsconf);
2021-06-18 23:57:02 +01:00
var adminStartupListConf = Configuration.GetSection("AdminList");
services.Configure<AdminStartupList>(adminStartupListConf);
2021-07-05 12:55:52 +01:00
var unleashConf = Configuration.GetSection("Unleash");
services.Configure<UnleashClientSettings>(unleashConf);
2021-06-21 23:38:05 +01:00
services.Configure<MigrationsEndPointOptions>(o => o.Path = "~/migrate");
2021-04-08 02:03:17 +01:00
}
2021-07-05 12:55:52 +01:00
public static IUnleash UnleashĈlient { get; private set; }
2021-04-08 02:03:17 +01:00
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2021-07-05 12:55:52 +01:00
public void Configure(IApplicationBuilder app,
Microsoft.AspNetCore.Hosting.IHostingEnvironment env,
IOptions<UnleashClientSettings> unleashClientSettings,
2021-05-09 01:52:46 +01:00
ApplicationDbContext dbContext)
2021-04-08 02:03:17 +01:00
{
if (env.IsDevelopment())
{
2021-04-25 12:12:50 +01:00
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
2021-04-08 02:03:17 +01:00
}
else
{
2021-05-23 01:00:57 +01:00
// app.UseExceptionHandler("/Home/Error");
app.UseDeveloperExceptionPage();
dbContext.Database.Migrate();
2021-04-08 02:03:17 +01:00
}
2021-07-05 12:55:52 +01:00
var unleashSettings = new UnleashSettings
{
UnleashApi = new Uri(unleashClientSettings.Value.ApiUrl),
AppName = "isnd",
Environment = env.EnvironmentName,
CustomHttpHeaders = new Dictionary<string, string>
{
{ "Authorization", unleashClientSettings.Value.ClientApiKey }
}
};
UnleashClientFactory unleashClientFactory = new UnleashClientFactory();
UnleashĈlient = unleashClientFactory.CreateClient(unleashSettings);
2021-04-08 02:03:17 +01:00
2021-07-05 12:55:52 +01:00
app.UseStatusCodePages().UseStaticFiles().UseAuthentication().UseMvc(routes =>
2021-04-08 02:03:17 +01:00
{
routes.MapRoute(
name: "default",
2021-05-23 01:00:57 +01:00
template: "{controller=PackageVersion}/{action=Index}/{PackageId?}");
2021-04-08 02:03:17 +01:00
});
}
}
}