-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
149 lines (127 loc) · 5.43 KB
/
Startup.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
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Blazor.Polyfill.Server;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
using WebApp.SoE.Services;
using WebApp.SoE.ViewModels;
using Westwind.AspNetCore.LiveReload;
namespace WebApp.SoE
{
public class Startup
{
public Startup(IConfiguration configuration) => Configuration = configuration;
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddScoped<TerminalCodesViewModel>(sp => new()
{
JsRuntime = sp.GetRequiredService<IJSRuntime>(),
LocalStorage = sp.GetRequiredService<ProtectedLocalStorage>()
});
services.AddScoped<SlotSummaryViewModel>(sp => new() { LocalStorage = sp.GetRequiredService<ProtectedLocalStorage>() });
services.AddScoped<ComparisonViewModel>(sp => new() { LocalStorage = sp.GetRequiredService<ProtectedLocalStorage>() });
services.AddScoped<SetOffsetValueViewModel>(sp => new()
{
JsRuntime = sp.GetRequiredService<IJSRuntime>(),
LocalStorage = sp.GetRequiredService<ProtectedLocalStorage>()
});
services.AddOptions<Settings>().Bind(Configuration.GetSection(nameof(Settings)));
services.AddSingleton(cfg => cfg.GetService<IOptionsMonitor<Settings>>()!.CurrentValue);
var supportedCulturesSection = Configuration.GetSection(nameof(SupportedCultures));
services.AddOptions<SupportedCultures>().Bind(supportedCulturesSection);
var supportedCultureIds = supportedCulturesSection.GetSection("Cultures").GetChildren().Select(e => e.Value).ToList();
services.AddOptions<TooltipRandomizerOptions>().Bind(Configuration.GetSection("TooltipRandomizer"));
services.AddSingleton(cfg => cfg.GetService<IOptionsMonitor<TooltipRandomizerOptions>>()!.CurrentValue);
services.AddScoped<IExplorationStatus, ExplorationStatus>();
services.AddSingleton<ITranslator, Translator>();
services.AddSingleton<ILocalizationCollector, LocalizationCollector>();
services.AddSingleton<IMarkdownBuilder, MarkdownBuilder>();
services.AddSingleton<ITooltipRandomizer, TooltipRandomizer>();
services.AddSingleton<CrypticTooltipRandomizer>();
services.AddSingleton<IAppInfo, AppInfo>();
services.AddSingleton<IBrowserInfo, BrowserInfo>();
services.AddSingleton<IRandomResTooltip, RandomResTooltip>();
services.Configure<RequestLocalizationOptions>(
options =>
{
var defaultCulture = CultureInfo.GetCultureInfo("en");
CultureInfo.CurrentCulture = defaultCulture;
CultureInfo.CurrentUICulture = defaultCulture;
var supportedCultures = new List<CultureInfo> { defaultCulture };
foreach (var supportedCultureId in supportedCultureIds)
supportedCultures.Add(CultureInfo.GetCultureInfo(supportedCultureId));
options.SetDefaultCulture(defaultCulture.TwoLetterISOLanguageName);
// Formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// UI strings that we have localized.
options.SupportedUICultures = supportedCultures;
options.AddInitialRequestCultureProvider(new QueryStringRequestCultureProvider());
options.AddInitialRequestCultureProvider(new CookieRequestCultureProvider());
options.AddInitialRequestCultureProvider(new AcceptLanguageHeaderRequestCultureProvider());
});
services.AddTransient(cfg => cfg.GetService<IOptionsMonitor<SupportedCultures>>()!.CurrentValue);
#if DEBUG
services.AddLiveReload(config =>
{
config.LiveReloadEnabled = true;
config.ClientFileExtensions = ".css,.js,.htm,.html";
config.FolderToMonitor = "~/../";
});
#endif
services.AddHttpClient();
services.AddRazorPages().AddDataAnnotationsLocalization();
services.AddServerSideBlazor();
services.AddBlazorPolyfill();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRequestLocalization();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
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();
}
#if DEBUG
app.UseLiveReload();
#endif
app.UseHttpsRedirection();
app.UseBlazorPolyfill(o =>
{
o.ES5FallbackValidation = request =>
{
var services = request.HttpContext.RequestServices;
var browserInfo = services.GetRequiredService<IBrowserInfo>();
var userAgent = request.Headers["User-Agent"];
return !browserInfo.IsSupportedBrowser(userAgent) && browserInfo.HasES5Support(userAgent);
};
});
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapBlazorHub();
endpoints.MapRazorPages();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}