-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (116 loc) · 4.79 KB
/
Program.cs
File metadata and controls
137 lines (116 loc) · 4.79 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
using InventoryManager.Data;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Options;
using InventoryManager.Data.Repositories;
using InventoryManager.Models;
using InventoryManager.Services;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(connectionString));
//Repositories
builder.Services.AddScoped<ITagRepository, TagRepository>();
builder.Services.AddScoped<IInventoryRepository, InventoryRepository>();
builder.Services.AddScoped<IItemRepository, ItemRepository>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IInventoryCommentRepository, InventoryCommentRepository>();
builder.Services.AddScoped<IInventoryAccessRepository, InventoryAccessRepository>();
builder.Services.AddScoped<ILikeRepository, LikeRepository>();
//Services
builder.Services.AddScoped<IInventoryService, InventoryService>();
builder.Services.AddScoped<IItemService, ItemService>();
builder.Services.AddScoped<IHomeService, HomeService>();
builder.Services.AddScoped<ISearchService, SearchService>();
builder.Services.AddScoped<ISupportTicketService, SupportTicketService>();
builder.Services.AddHttpClient<ISalesforceService, SalesforceService>();
// Add services to the container.
builder.Services.AddControllersWithViews()
.AddViewLocalization()
.AddDataAnnotationsLocalization();
builder.Services.AddLocalization(options => options.ResourcesPath = "");
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[] { "en", "ru" };
options.SetDefaultCulture("ru")
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new CookieRequestCultureProvider(),
new QueryStringRequestCultureProvider(),
new AcceptLanguageHeaderRequestCultureProvider()
};
});
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
options.LoginPath = "/Account/Login";
options.AccessDeniedPath = "/Account/Login";
})
.AddCookie("External")
.AddGoogle(options =>
{
options.ClientId = builder.Configuration["Authentication:Google:ClientId"]
?? throw new InvalidOperationException("Google ClientId not found in configuration.");
options.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"]
?? throw new InvalidOperationException("Google ClientSecret not found in configuration.");
options.SignInScheme = "External";
})
.AddGitHub(options =>
{
options.ClientId = builder.Configuration["Authentication:GitHub:ClientId"]
?? throw new InvalidOperationException("GitHub ClientId not found.");
options.ClientSecret = builder.Configuration["Authentication:GitHub:ClientSecret"]
?? throw new InvalidOperationException("GitHub ClientSecret not found.");
options.SignInScheme = "External";
options.Scope.Add("user:email");
});
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto;
});
var app = builder.Build();
app.UseForwardedHeaders();
var locOptions = app.Services.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value;
app.UseRequestLocalization(locOptions);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/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.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<AppDbContext>();
try
{
await context.Database.MigrateAsync();
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while migrating the database.");
}
if (!await context.Users.AnyAsync(u => u.Provider == "Seed"))
{
await DataSeeder.SeedAsync(context);
}
}
await app.RunAsync();