Add entity framework (#11)
Co-authored-by: Maxime Adler <madler@sqli.com> Reviewed-on: #11
This commit was merged in pull request #11.
This commit is contained in:
@@ -1,47 +1,104 @@
|
||||
using GameIdeas.Resources;
|
||||
using System.Resources;
|
||||
using GameIdeas.WebAPI.Context;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var services = builder.Services;
|
||||
|
||||
#if DEBUG
|
||||
Load("../../../../.env");
|
||||
#else
|
||||
Load(".env");
|
||||
#endif
|
||||
|
||||
Action<DbContextOptionsBuilder> dbContextOptions = options =>
|
||||
{
|
||||
options.UseNpgsql(
|
||||
GetConnectionString(),
|
||||
npgOption =>
|
||||
{
|
||||
npgOption.CommandTimeout(60);
|
||||
npgOption.MigrationsAssembly("GameIdeas.WebAPI");
|
||||
})
|
||||
.LogTo(Console.WriteLine);
|
||||
};
|
||||
|
||||
// Add services to the container.
|
||||
services.AddDbContext<GameIdeasContext>(dbContextOptions);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
services.AddSingleton<TranslationService>();
|
||||
services.AddSingleton<Translations>();
|
||||
|
||||
services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
services.AddOpenApi();
|
||||
|
||||
builder.Services.AddCors(option => option.AddDefaultPolicy(policy =>
|
||||
policy.WithOrigins("http://localhost:5172")
|
||||
services.AddCors(option => option.AddDefaultPolicy(policy =>
|
||||
policy.WithOrigins("http://localhost:5172", "http://localhost:7060")
|
||||
.AllowAnyHeader()
|
||||
.WithMethods("GET", "POST")));
|
||||
|
||||
builder.Services.AddSingleton<TranslationService>();
|
||||
builder.Services.AddSingleton<Translations>();
|
||||
|
||||
.WithMethods("GET", "POST", "PUT", "DELETE")));
|
||||
var app = builder.Build();
|
||||
|
||||
await LoadTranslations();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
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>());
|
||||
|
||||
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 Load(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]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user