using FastEndpoints; using FastEndpoints.Swagger; using SpaceGame.Api.Universe.Scenario; using SpaceGame.Api.Universe.Bootstrap; using SpaceGame.Api.Universe.Simulation; var builder = WebApplication.CreateBuilder(args); const string StartupScenarioPath = "scenarios/empty.json"; builder.Services.AddCors((options) => { options.AddDefaultPolicy((policy) => { policy .AllowAnyHeader() .AllowAnyMethod() .AllowAnyOrigin(); }); }); builder.Services .AddOptions() .Bind(builder.Configuration.GetSection("StaticData")) .Validate(options => !string.IsNullOrWhiteSpace(options.DataRoot), "StaticData:DataRoot must be configured.") .PostConfigure(options => { if (Path.IsPathRooted(options.DataRoot)) { options.DataRoot = Path.GetFullPath(options.DataRoot); return; } var candidatePaths = new[] { Path.GetFullPath(options.DataRoot), Path.GetFullPath(Path.Combine(builder.Environment.ContentRootPath, options.DataRoot)), Path.GetFullPath(Path.Combine(builder.Environment.ContentRootPath, "..", "..", options.DataRoot)), }; var resolvedPath = candidatePaths.FirstOrDefault(Directory.Exists); if (resolvedPath is null) { throw new InvalidOperationException($"StaticData:DataRoot '{options.DataRoot}' could not be resolved to an existing directory."); } options.DataRoot = resolvedPath; }) .ValidateOnStart(); builder.Services.Configure(builder.Configuration.GetSection("Balance")); builder.Services.Configure(builder.Configuration.GetSection("WorldGeneration")); builder.Services.Configure(builder.Configuration.GetSection("OrbitalSimulation")); builder.Services.AddSingleton(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddHostedService(); builder.Services.AddFastEndpoints(); builder.Services.SwaggerDocument(); var app = builder.Build(); app.Services.GetRequiredService().LoadFromScenario(StartupScenarioPath); app.UseCors(); app.UseFastEndpoints(); app.UseSwaggerGen(); app.Run();