-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
112 lines (87 loc) · 3.83 KB
/
Program.cs
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
100
101
102
103
104
105
106
107
108
109
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using MoviesMafia.Models.Repo;
using MoviesMafia.Models.GenericRepo;
using MoviesMafia.Models;
using dotenv.net;
var builder = WebApplication.CreateBuilder(args);
//Getting Connection string
DotEnv.Load();
string PGHOST = Environment.GetEnvironmentVariable("PGHOST");
string PGPORT = Environment.GetEnvironmentVariable("PGPORT");
string PGDATABASE = Environment.GetEnvironmentVariable("PGDATABASE");
string PGUSER = Environment.GetEnvironmentVariable("PGUSER");
string PGPASSWORD = Environment.GetEnvironmentVariable("PGPASSWORD");
string connString = $"Server={PGHOST};Port={PGPORT};Database={PGDATABASE};User Id={PGUSER};Password={PGPASSWORD}";
//Getting Assembly Name
var migrationAssembly = typeof(Program).Assembly.GetName().Name;
// Add services to the container.
builder.Services.AddDbContext<UserContext>(options =>
options.UseNpgsql(connString, sql => sql.MigrationsAssembly(migrationAssembly)));
builder.Services.AddDbContext<RecordsDBContext>(options => options.UseNpgsql(connString, sql => sql.MigrationsAssembly(migrationAssembly)));
builder.Services.Configure<IdentityOptions>(options => options.SignIn.RequireConfirmedEmail = true);
builder.Services.AddIdentity<ExtendedIdentityUser, IdentityRole>().AddEntityFrameworkStores<UserContext>().AddDefaultTokenProviders();
builder.Services.AddScoped<IUserRepo, UserRepo>();
builder.Services.AddScoped<IAPICalls, APICalls>();
builder.Services.AddScoped(typeof(IGenericRecordsDB<>), typeof(GenericRecordsDB<>));
builder.Services.AddServerSideBlazor();
// Add additional services, etc.
builder.Services.AddControllersWithViews();
builder.Services.ConfigureApplicationCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromDays(30);
options.SlidingExpiration = true;
});
var app = builder.Build();
var scope = app.Services.CreateScope();
var migUserContext = scope.ServiceProvider.GetRequiredService<UserContext>();
migUserContext.Database.MigrateAsync().Wait();
var migRecordsContext = scope.ServiceProvider.GetRequiredService<RecordsDBContext>();
migRecordsContext.Database.MigrateAsync().Wait();
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
if (!await roleManager.RoleExistsAsync("Admin"))
{
var adminRole = new IdentityRole("Admin");
await roleManager.CreateAsync(adminRole);
}
// Check if the "User" role exists and create it if it doesn't
if (!await roleManager.RoleExistsAsync("User"))
{
var userRole = new IdentityRole("User");
await roleManager.CreateAsync(userRole);
}
var adminPassword = Environment.GetEnvironmentVariable("ADMIN_PASSWORD");
var adminUsername = Environment.GetEnvironmentVariable("ADMIN_USERNAME");
var adminEmail = Environment.GetEnvironmentVariable("ADMIN_EMAIL");
var adminProfilePicturePath = Environment.GetEnvironmentVariable("ADMIN_PROFILE_PICTURE_PATH");
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ExtendedIdentityUser>>();
var adminUser = await userManager.FindByNameAsync(adminUsername);
if (adminUser == null)
{
adminUser = new ExtendedIdentityUser
{
UserName = adminUsername,
Email = adminEmail,
EmailConfirmed = true,
LockoutEnabled = false,
ProfilePicturePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "ProfilePictures", adminProfilePicturePath)
};
var result = await userManager.CreateAsync(adminUser, adminPassword);
if (result.Succeeded)
{
await userManager.AddToRoleAsync(adminUser, "Admin");
}
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=LandingPage}/{action=LandingPage}/{id?}");
app.MapBlazorHub();
app.Run();