-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
127 lines (99 loc) · 4.55 KB
/
Program.cs
File metadata and controls
127 lines (99 loc) · 4.55 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
using Amazon.S3;
using DotNetEnv;
using LMS.Components;
using LMS.Data;
using LMS.Data.Configuration;
using LMS.Data.Entities;
using LMS.DTOs.Storage;
using LMS.Interfaces;
using LMS.Services;
using LMS.Services.Storage;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.Options;
using Scalar.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
Env.Load();
builder.Configuration.AddEnvironmentVariables();
builder.Services.Configure<S3Options>(options =>
{
options.AccessKey = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY") ?? "";
options.SecretKey = Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY") ?? "";
options.ServiceUrl = Environment.GetEnvironmentVariable("AWS_SERVICE_URL") ?? "";
options.BucketName = Environment.GetEnvironmentVariable("AWS_BUCKET_NAME") ?? "";
options.Region = Environment.GetEnvironmentVariable("AWS_REGION") ?? "";
});
builder.Services.AddSingleton<IAmazonS3>(sp =>
{
var options = sp.GetRequiredService<IOptions<S3Options>>().Value;
return new AmazonS3Client(options.AccessKey, options.SecretKey, new AmazonS3Config
{
ServiceURL = options.ServiceUrl,
ForcePathStyle = true
});
});
// Настройка драйвера для игнорирования строгой проверки временных типов данных.
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
builder.Services.AddControllers();
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddCascadingAuthenticationState();
// Добавление сервисов приложения.
builder.Services.AddScoped<IFileStorageService, S3StorageService>();
builder.Services.AddScoped<IAdminService, AdminService>();
builder.Services.AddScoped<IModeratorService, ModeratorService>();
builder.Services.AddScoped<IOrganizationService, OrganizationService>();
builder.Services.AddScoped<ISubscriptionService, SubscriptionService>();
builder.Services.AddScoped<IBranchService, BranchService>();
builder.Services.AddScoped<ICourseService, CourseService>();
builder.Services.AddScoped<MaterialService, MaterialService>();
builder.Services.AddScoped<IAuditHistoryService, AuditHistoryService>();
builder.Services.AddScoped<IDirectorService, DirectorService>();
builder.Services.AddScoped<IStorageService, LocalStorageService>();
builder.Services.AddScoped<IUserSecurityService, UserSecurityService>();
// Добавление сервисов для авторизации в приложении.
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<UserRequestContext>();
builder.Services.AddScoped<UserPermissions, UserPermissions>();
builder.Services.AddScoped<PostgresConnectionInterceptor>();
builder.Services.AddDbContextFactory<DatabaseContext>((sp, options) =>
{
options.UseNpgsql(Environment.GetEnvironmentVariable("CONNECTION_STRING") ?? "",
o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
/*options.UseNpgsql(builder.Configuration.GetConnectionString("PostgresConnection"),
o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));*/
var interceptor = sp.GetRequiredService<PostgresConnectionInterceptor>();
options.AddInterceptors(interceptor);
}, ServiceLifetime.Scoped);
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/login";
options.AccessDeniedPath = "/access-denied";
options.ExpireTimeSpan = TimeSpan.FromHours(8);
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
});
builder.Services.AddAuthorization();
// Настройка инъекций моделей для приложения
builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<IPasswordHasher<Administrator>, PasswordHasher<Administrator>>();
builder.Services.AddScoped<IPasswordHasher<Moderator>, PasswordHasher<Moderator>>();
builder.Services.AddScoped<IPasswordHasher<Employee>, PasswordHasher<Employee>>();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();
app.MapControllers();
app.MapStaticAssets();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();