-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
174 lines (143 loc) · 6.07 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
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
using ELabel.Controllers;
using ELabel.Data;
using ELabel.Middleware;
using ELabel.Models;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using System.Globalization;
using System.Net;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
var builder = WebApplication.CreateBuilder(args);
// 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.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
// Load Producer configuration from appsettings.json
builder.Services.Configure<Producer>(builder.Configuration.GetSection("Producer"));
// Load environment variable with ELABEL prefix
builder.Configuration.AddEnvironmentVariables(prefix: "ELABEL_");
builder.Services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.SignIn.RequireConfirmedAccount = false;
options.User.RequireUniqueEmail = false;
options.Password.RequiredLength = 4;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
// Adding the view/localization services
builder.Services.AddLocalization();
builder.Services.AddControllersWithViews().AddViewLocalization();
builder.Services.AddSingleton<IStringLocalizerFactory, ResourceManagerStringLocalizerFactory>();
builder.Services.AddRazorPages();
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
string? knownProxy = builder.Configuration.GetValue<string>("KnownProxy");
if (!String.IsNullOrWhiteSpace(knownProxy))
options.KnownProxies.Add(IPAddress.Parse(knownProxy));
string? knownNetworks = builder.Configuration.GetValue<string>("KnownNetworks");
if (!String.IsNullOrWhiteSpace(knownNetworks))
{
foreach (string knownNetwork in knownNetworks.Split(','))
{
options.KnownNetworks.Add(IPNetwork.Parse(knownNetwork));
}
}
});
//builder.Services.AddTransient<UrlResolver>();
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddTransient<DatabaseInitializationController>();
builder.Services.AddTransient<IStartupFilter, DatabaseInitializationStartupFilter>();
// Configure supported cultures and localization options
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
// Official languages of the EU (and ISO 639-1 language codes)
CultureInfo[] supportedCultures = new[]
{
new CultureInfo("bg"), // Bulgarian (BG)
new CultureInfo("hr"), // Croatian (HR)
new CultureInfo("cs"), // Czech (CS)
new CultureInfo("da"), // Danish (DA)
new CultureInfo("nl"), // Dutch (NL)
new CultureInfo("en"), // English (EN)
new CultureInfo("et"), // Estonian (ET)
new CultureInfo("fi"), // Finnish (FI)
new CultureInfo("fr"), // French (FR)
new CultureInfo("de"), // German (DE)
new CultureInfo("el"), // Greek (EL)
new CultureInfo("hu"), // Hungarian (HU)
new CultureInfo("ga"), // Irish (GA)
new CultureInfo("it"), // Italian (IT)
new CultureInfo("lv"), // Latvian (LV)
new CultureInfo("lt"), // Lithuanian (LT)
new CultureInfo("mt"), // Maltese (MT)
new CultureInfo("pl"), // Polish (PL)
new CultureInfo("pt"), // Portuguese (PT)
new CultureInfo("ro"), // Romanian (RO)
new CultureInfo("sk"), // Slovak (SK)
new CultureInfo("sl"), // Slovene (SL)
new CultureInfo("es"), // Spanish (ES)
new CultureInfo("sv"), // Swedish (SV)
};
// State what the default culture for your application is. This will be used if no specific culture
// can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// These are the cultures the app supports for UI strings, i.e. we have localized resources for.
options.SupportedUICultures = supportedCultures;
// Using Accept-Language HTTP header from browsers
options.ApplyCurrentCultureToResponseHeaders = true;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.Migrate();
}
app.UseExceptionHandler("/Home/Error/500");
// 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.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
app.UseForwardedHeaders();
app.UseHttpsRedirection();
var cacheMaxAgeOneWeek = (60 * 60 * 24 * 7).ToString();
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
ctx.Context.Response.Headers.Append(
"Cache-Control", $"public, max-age={cacheMaxAgeOneWeek}");
}
});
app.UseRouting();
var locOptions = app.Services.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions?.Value!);
app.UseAuthorization();
app.MapControllerRoute(
name: "Admin",
pattern: "{area:exists}/{controller=Dashboard}/{action=Index}/{id?}"
);
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
app.MapRazorPages();
app.Run();