feat(backend): wire ASP.NET startup with WebSocket endpoint and DI
This commit is contained in:
34
Backend/LanderConfig.cs
Normal file
34
Backend/LanderConfig.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
namespace Backend;
|
||||||
|
|
||||||
|
public sealed class LanderConfig
|
||||||
|
{
|
||||||
|
public string CliPath { get; set; } = "publish/GameCli/GameCli";
|
||||||
|
public string ModelPath { get; set; } = "models/ppo_lander.onnx";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Walks up from <see cref="AppContext.BaseDirectory"/> to find the repo root
|
||||||
|
/// (marker file: <c>GameCli.sln</c>) and resolves the configured paths against it.
|
||||||
|
/// </summary>
|
||||||
|
public static class PathResolver
|
||||||
|
{
|
||||||
|
public static string RepoRoot()
|
||||||
|
{
|
||||||
|
var dir = AppContext.BaseDirectory;
|
||||||
|
while (dir is not null)
|
||||||
|
{
|
||||||
|
if (File.Exists(Path.Combine(dir, "GameCli.sln"))) return dir;
|
||||||
|
var parent = Directory.GetParent(dir);
|
||||||
|
if (parent is null) break;
|
||||||
|
dir = parent.FullName;
|
||||||
|
}
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
"could not locate repo root (no GameCli.sln found in any ancestor)");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string Resolve(string relativeOrAbsolute)
|
||||||
|
{
|
||||||
|
if (Path.IsPathRooted(relativeOrAbsolute)) return relativeOrAbsolute;
|
||||||
|
return Path.GetFullPath(Path.Combine(RepoRoot(), relativeOrAbsolute));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,52 @@
|
|||||||
// Placeholder. Rewritten in Task 6.
|
using System.Net.WebSockets;
|
||||||
|
using Backend;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
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();
|
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();
|
app.Run();
|
||||||
|
|
||||||
|
// -------- Public program class so Backend.Tests can use WebApplicationFactory --------
|
||||||
|
public partial class Program { }
|
||||||
|
|||||||
Reference in New Issue
Block a user