-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
185 lines (152 loc) · 6.05 KB
/
Program.cs
File metadata and controls
185 lines (152 loc) · 6.05 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Coravel;
using Coravel.Queuing.Interfaces;
using Coravel.Scheduling.Schedule.Interfaces;
using Discord.WebSocket;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore;
using QuickFinder;
using QuickFinder.Data;
using QuickFinder.Domain.DiscordDomain;
using QuickFinder.Domain.Matchmaking;
using QuickFinder.Email;
using QuickFinder.Options;
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<DiscordServiceOptions>(
builder.Configuration.GetSection(DiscordServiceOptions.Discord)
);
builder.Services.Configure<DiscordAuthOptions>(
builder.Configuration.GetSection(DiscordAuthOptions.DiscordAuth)
);
builder.Services.Configure<MatchmakingOptions>(
builder.Configuration.GetSection(MatchmakingOptions.Matchmaking)
);
builder
.Services.AddOptions<MatchmakingOptions>()
.Bind(builder.Configuration.GetSection(MatchmakingOptions.Matchmaking))
.ValidateDataAnnotations();
// Add services to the container.
var connectionString =
builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddHttpClient();
builder.Services.AddScoped<MatchmakingService>();
builder.Services.AddScoped<TicketRepository>();
builder.Services.AddScoped<GroupTicketRepository>();
builder.Services.AddScoped<CourseRepository>();
builder.Services.AddScoped<GroupRepository>();
builder.Services.AddScoped<PreferencesRepository>();
builder.Services.AddScoped<UserService>();
builder.Services.AddScoped<GroupMatchmakingService>();
builder.Services.AddScoped<SeedDB>();
builder.Services.AddScoped<DiscordAuthHandler>();
builder.Services.AddScheduler();
builder.Services.AddQueue();
builder.Services.AddEvents();
if (builder.Environment.IsProduction())
{
builder
.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(builder.Configuration["dpkeys"] ?? "./dpkeys"));
}
builder
.Services.AddDefaultIdentity<User>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddUserManager<CustomUserManager>();
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("Student", policy => policy.RequireAuthenticatedUser());
if (builder.Environment.IsDevelopment())
{
options.AddPolicy("Teacher", policy => policy.RequireAuthenticatedUser());
options.AddPolicy("Admin", policy => policy.RequireAuthenticatedUser());
}
else
{
options.AddPolicy(
"Teacher",
policy =>
policy.RequireAssertion(context =>
context.User.HasClaim(c => c.Type == "IsTeacher" || c.Type == "IsAdmin")
)
);
options.AddPolicy("Admin", policy => policy.RequireClaim("IsAdmin"));
}
});
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = StudentRoutes.Login();
options.AccessDeniedPath = StudentRoutes.Home();
});
builder.Services.AddRazorPages(options =>
{
options.Conventions.AuthorizeFolder("/Student", policy: "Student");
options.Conventions.AuthorizeFolder("/Teacher", policy: "Teacher");
options.Conventions.AuthorizeFolder("/Admin", policy: "Admin");
options.Conventions.AuthorizeAreaFolder("Identity", "/Account", policy: "Admin");
var pages = new[] { "/Login", "/Logout", "/Manage/Index", "/Manage/PersonalData" };
foreach (var page in pages)
{
options.Conventions.AllowAnonymousToAreaPage("Identity", "/Account" + page);
}
});
builder.Services.AddSingleton<DiscordSocketClient>();
builder.Services.AddScoped<DiscordService>();
builder.Services.AddHostedService<DiscordService>();
builder.Services.AddSingleton<IEmailSender, StubEmailSender>();
builder.Services.AddTransient<RunMatchmakingInvocable>();
builder.Services.AddTransient<DeleteUnusedGroupsInvocable>();
builder.Services.AddTransient<SendDMInvocable>();
builder.Services.AddTransient<OnUserDeleted>();
builder.Services.AddTransient<NotifyUsersOnGroupFilled>();
builder.Services.AddTransient<CreateDiscordChannelOnGroupFilled>();
builder.Services.AddTransient<InviteToServerOnCourseJoined>();
// Configure forwarded headers
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedProto;
});
var app = builder.Build();
using var scope = app.Services.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<IQueue>>();
app.Services.ConfigureQueue()
.LogQueuedTaskProgress(logger)
.OnError(e => logger.LogError(e, "Error in queue:"));
app.Services.ConfigureScheduler(app.Configuration);
var registration = app.Services.ConfigureEvents();
registration.Register<UserDeleted>().Subscribe<OnUserDeleted>();
registration
.Register<GroupFilled>()
.Subscribe<NotifyUsersOnGroupFilled>()
.Subscribe<CreateDiscordChannelOnGroupFilled>();
registration.Register<GroupDisbanded>().Subscribe<DeleteDiscordChannelOnGroupDisbanded>();
registration.Register<GroupMemberLeft>().Subscribe<DeleteUserPermissionsOnGroupMemberLeft>();
registration.Register<GroupMemberAdded>();
registration.Register<CourseJoined>().Subscribe<InviteToServerOnCourseJoined>();
// 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.UseForwardedHeaders();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
using var db_scope = app.Services.CreateScope();
var seed_db = db_scope.ServiceProvider.GetRequiredService<SeedDB>();
seed_db.Seed();
}
app.Run();
public partial class Startup { }