mirror of
https://github.com/XFox111/bonch-calendar.git
synced 2026-06-30 10:52:41 +03:00
feat!: native AOT for api app #26
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using BonchCalendar.Health;
|
||||
|
||||
namespace BonchCalendar;
|
||||
|
||||
[JsonSerializable(typeof(string))]
|
||||
[JsonSerializable(typeof(string[]))]
|
||||
[JsonSerializable(typeof(int))]
|
||||
[JsonSerializable(typeof(bool))]
|
||||
[JsonSerializable(typeof(StatsResponse))]
|
||||
[JsonSerializable(typeof(Dictionary<int, string>))]
|
||||
[JsonSerializable(typeof(HealthResponse))]
|
||||
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
@@ -4,11 +4,11 @@
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<PublishAot>true</PublishAot>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AngleSharp" Version="1.4.0" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="9.0.0" />
|
||||
<PackageReference Include="Ical.Net" Version="5.2.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
+12
-10
@@ -1,19 +1,21 @@
|
||||
FROM mcr.microsoft.com/dotnet/sdk:10.0-alpine AS build
|
||||
FROM mcr.microsoft.com/dotnet/sdk:10.0-alpine-aot AS build
|
||||
WORKDIR /build
|
||||
|
||||
ADD *.csproj .
|
||||
RUN dotnet restore
|
||||
ADD --link . .
|
||||
RUN --mount=type=cache,target=/root/.nuget \
|
||||
--mount=type=cache,target=/source/bin \
|
||||
--mount=type=cache,target=/source/obj \
|
||||
dotnet publish --output /out /p:PublishAot=true BonchCalendar.csproj \
|
||||
&& rm /out/*.dbg /out/*.Development.json
|
||||
|
||||
ADD . ./
|
||||
RUN dotnet publish --no-restore --configuration Release --output /out
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:10.0-alpine AS prod
|
||||
FROM mcr.microsoft.com/dotnet/runtime-deps:10.0-alpine AS prod
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=build /out/* .
|
||||
COPY --link --from=build /out/* .
|
||||
USER $APP_UID
|
||||
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=60s --retries=3 --start-period=5s --timeout=10s \
|
||||
CMD wget --no-verbose --tries 1 --spider http://localhost:8080/health || exit 1
|
||||
|
||||
ENTRYPOINT [ "dotnet", "BonchCalendar.dll" ]
|
||||
EXPOSE 8080
|
||||
ENTRYPOINT [ "./BonchCalendar" ]
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
|
||||
namespace BonchCalendar.Health;
|
||||
|
||||
public static class HealthCheckWriter
|
||||
{
|
||||
private static readonly byte[] _emptyResponse = [ (byte)'{', (byte)'}' ];
|
||||
private static JsonSerializerContext? _jsonContext = null;
|
||||
|
||||
public static JsonSerializerContext JsonContext => _jsonContext ??= CreateSerializerContext();
|
||||
|
||||
public static async Task WriteHealthCheckResponse(HttpContext context, HealthReport report)
|
||||
{
|
||||
if (report is null)
|
||||
{
|
||||
await context.Response.BodyWriter.WriteAsync(_emptyResponse).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
HealthResponse response = new(
|
||||
Status: report.Status,
|
||||
TotalDuration: report.TotalDuration,
|
||||
Entries: report.Entries.ToDictionary(
|
||||
e => e.Key,
|
||||
e => new HealthResponseEntry(
|
||||
Status: e.Value.Status,
|
||||
Description: e.Value.Description,
|
||||
Duration: e.Value.Duration,
|
||||
Data: e.Value.Data
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
context.Response.ContentType = "application/json; charset=utf-8";
|
||||
|
||||
await JsonSerializer.SerializeAsync(context.Response.Body, response, typeof(HealthResponse), JsonContext).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static AppJsonSerializerContext CreateSerializerContext()
|
||||
{
|
||||
JsonSerializerOptions options = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
};
|
||||
|
||||
options.Converters.Add(new JsonStringEnumConverter<HealthStatus>(options.PropertyNamingPolicy));
|
||||
options.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
|
||||
|
||||
return new AppJsonSerializerContext(options);
|
||||
}
|
||||
}
|
||||
|
||||
public record HealthResponse(
|
||||
HealthStatus Status,
|
||||
TimeSpan TotalDuration,
|
||||
IReadOnlyDictionary<string, HealthResponseEntry> Entries
|
||||
);
|
||||
|
||||
public record HealthResponseEntry(
|
||||
HealthStatus Status,
|
||||
string? Description,
|
||||
TimeSpan Duration,
|
||||
IReadOnlyDictionary<string, object> Data
|
||||
);
|
||||
+8
-3
@@ -4,12 +4,15 @@ using BonchCalendar;
|
||||
using BonchCalendar.Health;
|
||||
using BonchCalendar.Services;
|
||||
using BonchCalendar.Utils;
|
||||
using HealthChecks.UI.Client;
|
||||
using Ical.Net.DataTypes;
|
||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
||||
WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args);
|
||||
|
||||
builder.Services.ConfigureHttpJsonOptions(options =>
|
||||
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default)
|
||||
);
|
||||
|
||||
// Add services to the container.
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
@@ -51,7 +54,7 @@ app.MapOpenApi();
|
||||
|
||||
app.MapHealthChecks("/health", new HealthCheckOptions
|
||||
{
|
||||
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
||||
ResponseWriter = HealthCheckWriter.WriteHealthCheckResponse
|
||||
});
|
||||
|
||||
ILogger<Program> logger = app.Services.GetRequiredService<ILogger<Program>>();
|
||||
@@ -134,6 +137,8 @@ app.MapGet("/timetable/{facultyId}/{groupId}", async (
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Begin generating timetable for {FacultyId}/{GroupId}.", facultyId, groupId);
|
||||
|
||||
if (hasId)
|
||||
content = await timetableService.GetTimetableAsync(facultyId, groupId, saveToCache: true);
|
||||
else
|
||||
|
||||
@@ -45,10 +45,10 @@ public class IssueTrackingService
|
||||
report.Add("/faculties", false);
|
||||
|
||||
if (_unsuccessfulGroupFetches.Count > 0)
|
||||
report.Add("/groups", _unsuccessfulGroupFetches);
|
||||
report.Add("/groups", _unsuccessfulGroupFetches.ToArray());
|
||||
|
||||
if (_unsuccessfulTimetableFetches.Count > 0)
|
||||
report.Add("/timetable", _unsuccessfulTimetableFetches);
|
||||
report.Add("/timetable", _unsuccessfulTimetableFetches.ToArray());
|
||||
|
||||
return report;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user