53 lines
1.6 KiB
C#
53 lines
1.6 KiB
C#
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.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 { }
|