Files
game-ideas/src/GameIdeas/Server/GameIdeas.WebAPI/Program.cs
Egamorf 436fe3b050
All checks were successful
Game Ideas build for PR / build_blazor_app (pull_request) Successful in 1m1s
Apply filter for release date and storage
2025-04-20 15:41:54 +02:00

111 lines
3.0 KiB
C#

using GameIdeas.Resources;
using GameIdeas.WebAPI.Context;
using GameIdeas.WebAPI.Services.Categories;
using GameIdeas.WebAPI.Services.Games;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
#if DEBUG
LoadEnvironmentVariable("../../../../.env");
#else
LoadEnvironmentVariable(".env");
#endif
Action<DbContextOptionsBuilder> dbContextOptions = options =>
{
options.UseNpgsql(
GetConnectionString(),
npgOption =>
{
npgOption.CommandTimeout(60);
npgOption.MigrationsAssembly("GameIdeas.WebAPI");
});
};
// Add services to the container.
services.AddDbContext<GameIdeasContext>(dbContextOptions);
services.AddSingleton<TranslationService>();
services.AddSingleton<Translations>();
services.AddScoped<IGameReadService, GameReadService>();
services.AddScoped<IGameWriteService, GameWriteService>();
services.AddScoped<ICategoryService, CategoryService>();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
services.AddOpenApi();
services.AddCors(option => option.AddDefaultPolicy(policy =>
policy.WithOrigins("http://localhost:5172", "http://localhost:7060")
.AllowAnyHeader()
.WithMethods("GET", "POST", "PUT", "DELETE")));
var app = builder.Build();
await LoadTranslations();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseCors();
app.UseAuthorization();
app.MapControllers();
app.Run();
async Task LoadTranslations()
{
var filesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files");
var translationFiles = Directory.GetFiles(filesDirectory, "*.json");
var dictionary = new Dictionary<string, string>();
foreach (var file in translationFiles)
{
var name = file.Split('.');
var culture = name[^2];
var content = await File.ReadAllTextAsync(file);
dictionary.Add(culture, content);
}
app.Services.GetRequiredService<TranslationService>().Initialize(dictionary);
ResourcesKey.Initialize(app.Services.GetRequiredService<Translations>());
}
string GetConnectionString()
{
var host = Environment.GetEnvironmentVariable("DB_HOST");
var login = Environment.GetEnvironmentVariable("DB_USERNAME");
var pass = Environment.GetEnvironmentVariable("DB_PASSWORD");
var database = Environment.GetEnvironmentVariable("DB_DATABASE");
return $"Host={host};Username={login};Password={pass};Database={database}";
}
static void LoadEnvironmentVariable(string filePath)
{
if (!File.Exists(filePath))
return;
foreach (var line in File.ReadAllLines(filePath))
{
var parts = line.Split(
'=',
StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
continue;
Environment.SetEnvironmentVariable(parts[0], parts[1]);
}
}