78 lines
2.8 KiB
C#
78 lines
2.8 KiB
C#
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<StaticDataOptions>()
|
|
.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<BalanceOptions>(builder.Configuration.GetSection("Balance"));
|
|
builder.Services.Configure<WorldGenerationOptions>(builder.Configuration.GetSection("WorldGeneration"));
|
|
builder.Services.Configure<OrbitalSimulationOptions>(builder.Configuration.GetSection("OrbitalSimulation"));
|
|
|
|
builder.Services.AddSingleton<IBalanceService, BalanceService>();
|
|
builder.Services.AddTransient<SystemGenerationService>();
|
|
builder.Services.AddTransient<SpatialBuilder>();
|
|
builder.Services.AddTransient<WorldSeedingService>();
|
|
builder.Services.AddTransient<ScenarioValidationService>();
|
|
builder.Services.AddTransient<ScenarioContentBuilder>();
|
|
builder.Services.AddTransient<ScenarioLoader>();
|
|
builder.Services.AddTransient<WorldTopologyBuilder>();
|
|
builder.Services.AddTransient<WorldRuntimeAssembler>();
|
|
builder.Services.AddTransient<WorldBuilder>();
|
|
builder.Services.AddSingleton<IStaticDataProvider, StaticDataProvider>();
|
|
builder.Services.AddSingleton<WorldService>();
|
|
builder.Services.AddSingleton<TelemetryService>();
|
|
builder.Services.AddHostedService<SimulationHostedService>();
|
|
|
|
builder.Services.AddFastEndpoints();
|
|
builder.Services.SwaggerDocument();
|
|
|
|
var app = builder.Build();
|
|
app.Services.GetRequiredService<WorldService>().LoadFromScenario(StartupScenarioPath);
|
|
|
|
app.UseCors();
|
|
app.UseFastEndpoints();
|
|
app.UseSwaggerGen();
|
|
|
|
app.Run();
|