Files
game-ideas/src/GameIdeas/Client/GameIdeas.BlazorApp/Program.cs
2025-04-21 00:35:36 +02:00

62 lines
1.8 KiB
C#

using System.Net.Http.Json;
using Blazored.LocalStorage;
using GameIdeas.BlazorApp;
using GameIdeas.BlazorApp.Pages.Games.Gateways;
using GameIdeas.BlazorApp.Pages.User.Gateways;
using GameIdeas.BlazorApp.Services;
using GameIdeas.Resources;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
var services = builder.Services;
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
UriBuilder uriBuilder = new(builder.HostEnvironment.BaseAddress)
{
Port = 8000
};
services.AddHttpClient(
"GameIdeas.WebAPI",
client =>
{
client.BaseAddress = uriBuilder.Uri;
client.Timeout = TimeSpan.FromMinutes(3);
});
services.AddBlazoredLocalStorage();
services.AddAuthorizationCore();
services.AddScoped<AuthenticationStateProvider, JwtAuthenticationStateProvider>();
services.AddScoped<IHttpClientService, HttpClientService>();
services.AddScoped<IAuthGateway, AuthGateway>();
services.AddScoped<IGameGateway, GameGateway>();
services.AddSingleton<TranslationService>();
services.AddSingleton<Translations>();
var app = builder.Build();
await FetchTranslation(app);
await app.RunAsync();
static async Task FetchTranslation(WebAssemblyHost app)
{
var client = app.Services.GetService<IHttpClientFactory>()?.CreateClient("GameIdeas.WebAPI") ??
throw new Exception("Http client not found");
var response = await client.GetAsync("api/Translations");
var dictionary = await response.Content.ReadFromJsonAsync<Dictionary<string, string>>();
if (dictionary != null)
{
app.Services.GetService<TranslationService>()!.Initialize(dictionary);
ResourcesKey.Initialize(app.Services.GetService<Translations>()!);
}
}