feat(backend): wire ASP.NET startup with WebSocket endpoint and DI
This commit is contained in:
@@ -1,5 +1,52 @@
|
||||
// Placeholder. Rewritten in Task 6.
|
||||
using System.Net.WebSockets;
|
||||
using Backend;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// -------- Config --------
|
||||
builder.Services.Configure<LanderConfig>(builder.Configuration.GetSection("Lander"));
|
||||
|
||||
// -------- Singletons --------
|
||||
builder.Services.AddSingleton<PolicyRunner>(sp =>
|
||||
{
|
||||
var cfg = sp.GetRequiredService<Microsoft.Extensions.Options.IOptions<LanderConfig>>().Value;
|
||||
var absolute = PathResolver.Resolve(cfg.ModelPath);
|
||||
return new PolicyRunner(absolute);
|
||||
});
|
||||
|
||||
builder.Services.AddLogging();
|
||||
|
||||
var app = builder.Build();
|
||||
app.MapGet("/", () => "GameCli Backend placeholder");
|
||||
app.UseWebSockets();
|
||||
|
||||
// -------- Health --------
|
||||
app.MapGet("/", () => "GameCli Backend");
|
||||
|
||||
// -------- WebSocket endpoint --------
|
||||
app.Map("/ws/game", async (HttpContext ctx,
|
||||
PolicyRunner policy,
|
||||
Microsoft.Extensions.Options.IOptions<LanderConfig> cfg,
|
||||
ILoggerFactory loggerFactory) =>
|
||||
{
|
||||
if (!ctx.WebSockets.IsWebSocketRequest)
|
||||
{
|
||||
ctx.Response.StatusCode = 400;
|
||||
return;
|
||||
}
|
||||
using var socket = await ctx.WebSockets.AcceptWebSocketAsync();
|
||||
var cliPath = PathResolver.Resolve(cfg.Value.CliPath);
|
||||
var proc = new GameProcess(cliPath);
|
||||
var logger = loggerFactory.CreateLogger<GameSession>();
|
||||
await using var session = new GameSession(socket, proc, policy, logger);
|
||||
try
|
||||
{
|
||||
await session.RunAsync(ctx.RequestAborted);
|
||||
}
|
||||
catch (WebSocketException) { /* client disconnected */ }
|
||||
catch (OperationCanceledException) { /* shutdown */ }
|
||||
});
|
||||
|
||||
app.Run();
|
||||
|
||||
// -------- Public program class so Backend.Tests can use WebApplicationFactory --------
|
||||
public partial class Program { }
|
||||
|
||||
Reference in New Issue
Block a user