-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
99 lines (82 loc) · 3.38 KB
/
Program.cs
File metadata and controls
99 lines (82 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using BrewComp.Areas.Identity;
using BrewComp.Data;
using BrewComp.Identity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.EntityFrameworkCore;
using MudBlazor.Services;
namespace BrewComp;
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var connStr = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<BrewCompDbContext>(o => o.UseNpgsql(connStr));
builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddIdentity<BrewCompUser, IdentityRole>(o => o.SignIn.RequireConfirmedEmail = false)
.AddDefaultTokenProviders()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddEntityFrameworkStores<BrewCompDbContext>();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddMudServices();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<BrewCompUser>>();
builder.Services.AddSingleton<IEmailSender, EmailSender>();
builder.Services.AddTransient<IAuthorizationHandler, CoordinatorHandler>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
});
app.MapRazorPages();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
//Default Roles
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetService<BrewCompDbContext>();
db?.Database.EnsureCreated();
var _roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
string[] _defaultRoles = { "siteadmin", "coordinator", "participant", "steward", "judge" };
foreach (var role in _defaultRoles)
{
if (!await _roleManager.RoleExistsAsync(role))
{
await _roleManager.CreateAsync(new IdentityRole(role));
}
}
var _context = scope.ServiceProvider.GetRequiredService<BrewCompDbContext>();
if(_context.Clubs.Count() == 0)
{
HomebrewClub.PopulateDb(_context, app.Logger);
}
}
app.Run();
// This thread is now blocked, won't exit until app is closed/shutdown
}
}