Skip to content

Commit

Permalink
Add simple server for aspire sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelldi committed Nov 25, 2023
1 parent dd13cc5 commit 82023a1
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions aspire-plugin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
src\dotnet\Directory.Build.props = src\dotnet\Directory.Build.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aspire-session-host", "src\dotnet\aspire-session-host\aspire-session-host.csproj", "{B52C8D2D-4AD8-48E5-9DBD-F856CA054369}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,5 +27,9 @@ Global
{EC305C22-B4BF-4A6B-92F0-C8D2372F0E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC305C22-B4BF-4A6B-92F0-C8D2372F0E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC305C22-B4BF-4A6B-92F0-C8D2372F0E1E}.Release|Any CPU.Build.0 = Release|Any CPU
{B52C8D2D-4AD8-48E5-9DBD-F856CA054369}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B52C8D2D-4AD8-48E5-9DBD-F856CA054369}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B52C8D2D-4AD8-48E5-9DBD-F856CA054369}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B52C8D2D-4AD8-48E5-9DBD-F856CA054369}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
65 changes: 65 additions & 0 deletions src/dotnet/aspire-session-host/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Net.WebSockets;
using System.Text.Json;

var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(it =>
{
it.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
});

var app = builder.Build();

app.UseWebSockets();

app.MapPut("/run_session", (Session session) =>
{
app.Logger.LogInformation("Session request {session}", session);
return TypedResults.Created();
});

app.MapDelete("/run_session/{sessionId:guid}", (Guid sessionId) =>
{
return TypedResults.Ok();
});

app.MapGet("/run_session/notify", async context =>
{
if (context.WebSockets.IsWebSocketRequest)
{
using var ws = await context.WebSockets.AcceptWebSocketAsync();
await Receive(ws);
}
else
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
}
});

app.Run();

static async Task Receive(WebSocket webSocket)
{
var buffer = new byte[1024 * 4];
var receiveResult = await webSocket.ReceiveAsync(
new ArraySegment<byte>(buffer), CancellationToken.None);

while (!receiveResult.CloseStatus.HasValue)
{
receiveResult = await webSocket.ReceiveAsync(
new ArraySegment<byte>(buffer), CancellationToken.None);
}

await webSocket.CloseAsync(
receiveResult.CloseStatus.Value,
receiveResult.CloseStatusDescription,
CancellationToken.None);
}

record Session(
string ProjectPath,
bool Debug,
EnvironmentVariable[] Env,
string[] Args
);

record EnvironmentVariable(string Name, string Value);
9 changes: 9 additions & 0 deletions src/dotnet/aspire-session-host/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
14 changes: 14 additions & 0 deletions src/dotnet/aspire-session-host/aspire-session-host.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>AspireSessionHost</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.RdFramework" Version="2023.3.3" />
</ItemGroup>

</Project>

0 comments on commit 82023a1

Please sign in to comment.