native aot in container

This commit is contained in:
Vinzenz Schroeter 2024-04-10 22:03:36 +02:00
parent 0ca6a91a7e
commit 85ae3e302c
11 changed files with 128 additions and 61 deletions

View file

@ -1,20 +0,0 @@
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["TanksServer/TanksServer.csproj", "TanksServer/"]
RUN dotnet restore "TanksServer/TanksServer.csproj"
COPY . .
WORKDIR "/src/TanksServer"
RUN dotnet build "TanksServer.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TanksServer.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TanksServer.dll"]

View file

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
namespace TanksServer.Models;
internal sealed class Player(string name)
@ -6,18 +8,10 @@ internal sealed class Player(string name)
public Guid Id { get; } = Guid.NewGuid();
[JsonIgnore]
public PlayerControls Controls { get; } = new();
public int Kills { get; set; }
public int Deaths { get; set; }
}
internal sealed class PlayerControls
{
public bool Forward { get; set; }
public bool Backward { get; set; }
public bool TurnLeft { get; set; }
public bool TurnRight { get; set; }
public bool Shoot { get; set; }
}
}

View file

@ -0,0 +1,10 @@
namespace TanksServer.Models;
internal sealed class PlayerControls
{
public bool Forward { get; set; }
public bool Backward { get; set; }
public bool TurnLeft { get; set; }
public bool TurnRight { get; set; }
public bool Shoot { get; set; }
}

View file

@ -11,15 +11,12 @@ using TanksServer.ServicePointDisplay;
namespace TanksServer;
internal static class Program
public static class Program
{
public static async Task Main(string[] args)
public static void Main(string[] args)
{
var app = Configure(args);
app.UseCors();
app.UseWebSockets();
var clientScreenServer = app.Services.GetRequiredService<ClientScreenServer>();
var playerService = app.Services.GetRequiredService<PlayerServer>();
var controlsServer = app.Services.GetRequiredService<ControlsServer>();
@ -52,10 +49,10 @@ internal static class Program
using var ws = await context.WebSockets.AcceptWebSocketAsync();
await controlsServer.HandleClient(ws, player);
return Results.Empty;
return Results.Ok();
});
await app.RunAsync();
app.Run();
}
private static WebApplication Configure(string[] args)
@ -74,7 +71,7 @@ internal static class Program
options.SerializerOptions.TypeInfoResolverChain.Insert(0, new AppSerializerContext());
});
builder.Services.AddOptions();
builder.Services.AddHttpLogging(_ => { });
builder.Services.AddSingleton<MapService>();
builder.Services.AddSingleton<BulletManager>();
@ -107,6 +104,12 @@ internal static class Program
builder.Services.Configure<ServicePointDisplayConfiguration>(
builder.Configuration.GetSection("ServicePointDisplay"));
return builder.Build();
var app = builder.Build();
app.UseCors();
app.UseWebSockets();
app.UseHttpLogging();
return app;
}
}
}

View file

@ -14,6 +14,7 @@ internal sealed class SendToServicePointDisplay : ITickStep, IDisposable
private readonly TextDisplayBuffer _scoresBuffer;
private readonly PlayerServer _players;
private readonly ILogger<SendToServicePointDisplay> _logger;
private DateTime _nextFailLog = DateTime.Now;
private const int ScoresWidth = 12;
private const int ScoresHeight = 20;
@ -70,7 +71,11 @@ internal sealed class SendToServicePointDisplay : ITickStep, IDisposable
}
catch (SocketException ex)
{
_logger.LogWarning(ex, "could not send data to service point display");
if (DateTime.Now > _nextFailLog)
{
_logger.LogWarning("could not send data to service point display: {}", ex.Message);
_nextFailLog = DateTime.Now + TimeSpan.FromSeconds(5);
}
}
}
}

View file

@ -5,25 +5,41 @@
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<PublishAot>true</PublishAot>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3" />
</ItemGroup>
<PropertyGroup>
<AnalysisMode>Recommended</AnalysisMode>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>CA1805,CA1848</NoWarn>
</PropertyGroup>
<PropertyGroup>
<IsAotCompatible>true</IsAotCompatible>
<PublishAot>true</PublishAot>
<IlcDisableReflection>false</IlcDisableReflection>
<InvariantGlobalization>true</InvariantGlobalization>
<StaticExecutable>true</StaticExecutable>
<StripSymbols>true</StripSymbols>
<StaticallyLinked>true</StaticallyLinked>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0"/>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3"/>
</ItemGroup>
<ItemGroup>
<None Include="./assets/tank.png" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="Always" />
<Content Include="../Dockerfile">
<Link>..\Dockerfile</Link>
</Content>
<Content Include="../.dockerignore">
<Link>../Dockerfile</Link>
</Content>
<Content Include="../Makefile" />
</ItemGroup>
</Project>