isn/Startup.cs

107 lines
3.5 KiB
C#

4 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
4 years ago
using Microsoft.AspNetCore.DataProtection;
4 years ago
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
4 years ago
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Hosting;
using nuget_host.Data;
using nuget_host.Interfaces;
using nuget_host.Services;
using nuget_host.Entities;
using nuget_host.Models;
3 years ago
using System.Reflection;
4 years ago
namespace nuget_host
{
public class Startup
{
4 years ago
public Startup(IConfiguration config)
4 years ago
{
Configuration = config;
}
public IConfiguration Configuration { get; }
4 years ago
public static string RootApiKeySecret { get; private set; }
4 years ago
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("DefaultConnection")));
4 years ago
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager()
.AddDefaultUI()
.AddDefaultTokenProviders();
4 years ago
services.AddMvc();
4 years ago
services.AddDataProtection();
services.AddTransient<IMailer, EmailSender>();
services.AddTransient<IEmailSender, EmailSender>();
4 years ago
3 years ago
var smtpSettingsconf = Configuration.GetSection("Smtp");
services.Configure<SmtpSettings>(smtpSettingsconf);
3 years ago
var nugetSettingsconf = Configuration.GetSection("Nuget");
services.Configure<NugetSettings>(nugetSettingsconf);
3 years ago
4 years ago
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env,
ApplicationDbContext dbContext)
4 years ago
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
4 years ago
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
3 years ago
try
{
dbContext.Database.Migrate();
3 years ago
}
catch (TargetInvocationException ex)
{
if (ex.InnerException is InvalidOperationException)
// nothing to do ?
{
// TODO (or not) Hit the developper
}
else throw ex;
}
4 years ago
}
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseAuthentication();
4 years ago
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}