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>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<PublishAot>true</PublishAot>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AngleSharp" Version="1.4.0" />
|
<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="Ical.Net" Version="5.2.2" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.8" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.8" />
|
||||||
</ItemGroup>
|
</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
|
WORKDIR /build
|
||||||
|
|
||||||
ADD *.csproj .
|
ADD --link . .
|
||||||
RUN dotnet restore
|
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 . ./
|
FROM mcr.microsoft.com/dotnet/runtime-deps:10.0-alpine AS prod
|
||||||
RUN dotnet publish --no-restore --configuration Release --output /out
|
|
||||||
|
|
||||||
FROM mcr.microsoft.com/dotnet/aspnet:10.0-alpine AS prod
|
|
||||||
WORKDIR /app
|
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 \
|
HEALTHCHECK --interval=60s --retries=3 --start-period=5s --timeout=10s \
|
||||||
CMD wget --no-verbose --tries 1 --spider http://localhost:8080/health || exit 1
|
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.Health;
|
||||||
using BonchCalendar.Services;
|
using BonchCalendar.Services;
|
||||||
using BonchCalendar.Utils;
|
using BonchCalendar.Utils;
|
||||||
using HealthChecks.UI.Client;
|
|
||||||
using Ical.Net.DataTypes;
|
using Ical.Net.DataTypes;
|
||||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
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.
|
// Add services to the container.
|
||||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||||
@@ -51,7 +54,7 @@ app.MapOpenApi();
|
|||||||
|
|
||||||
app.MapHealthChecks("/health", new HealthCheckOptions
|
app.MapHealthChecks("/health", new HealthCheckOptions
|
||||||
{
|
{
|
||||||
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
ResponseWriter = HealthCheckWriter.WriteHealthCheckResponse
|
||||||
});
|
});
|
||||||
|
|
||||||
ILogger<Program> logger = app.Services.GetRequiredService<ILogger<Program>>();
|
ILogger<Program> logger = app.Services.GetRequiredService<ILogger<Program>>();
|
||||||
@@ -134,6 +137,8 @@ app.MapGet("/timetable/{facultyId}/{groupId}", async (
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
logger.LogInformation("Begin generating timetable for {FacultyId}/{GroupId}.", facultyId, groupId);
|
||||||
|
|
||||||
if (hasId)
|
if (hasId)
|
||||||
content = await timetableService.GetTimetableAsync(facultyId, groupId, saveToCache: true);
|
content = await timetableService.GetTimetableAsync(facultyId, groupId, saveToCache: true);
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -45,10 +45,10 @@ public class IssueTrackingService
|
|||||||
report.Add("/faculties", false);
|
report.Add("/faculties", false);
|
||||||
|
|
||||||
if (_unsuccessfulGroupFetches.Count > 0)
|
if (_unsuccessfulGroupFetches.Count > 0)
|
||||||
report.Add("/groups", _unsuccessfulGroupFetches);
|
report.Add("/groups", _unsuccessfulGroupFetches.ToArray());
|
||||||
|
|
||||||
if (_unsuccessfulTimetableFetches.Count > 0)
|
if (_unsuccessfulTimetableFetches.Count > 0)
|
||||||
report.Add("/timetable", _unsuccessfulTimetableFetches);
|
report.Add("/timetable", _unsuccessfulTimetableFetches.ToArray());
|
||||||
|
|
||||||
return report;
|
return report;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ async function fetchApi<T>(path: string, defaultValue: T, alwaysReturnResponse:
|
|||||||
if (!res.ok && !alwaysReturnResponse)
|
if (!res.ok && !alwaysReturnResponse)
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
|
|
||||||
return await res.json()
|
return await res.json();
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
@@ -40,21 +40,20 @@ export type StatsResponse =
|
|||||||
|
|
||||||
export type HealthResponse =
|
export type HealthResponse =
|
||||||
{
|
{
|
||||||
status: ServiceStatus;
|
status: HealthStatus;
|
||||||
totalDuration: string;
|
totalDuration: string;
|
||||||
entries: {
|
entries: {
|
||||||
["timetable_website"]: TimetableHealth;
|
["timetable_website"]: TimetableHealthResponseEntry;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ServiceStatus = "Healthy" | "Unhealthy" | "Degraded";
|
export type HealthStatus = "healthy" | "unhealthy" | "degraded";
|
||||||
|
|
||||||
export type TimetableHealth =
|
export type TimetableHealthResponseEntry =
|
||||||
{
|
{
|
||||||
|
status: HealthStatus;
|
||||||
description?: string;
|
description?: string;
|
||||||
duration: string;
|
duration: string;
|
||||||
status: "Healthy" | "Unhealthy",
|
|
||||||
tags: unknown[],
|
|
||||||
data:
|
data:
|
||||||
{
|
{
|
||||||
"/faculties"?: false,
|
"/faculties"?: false,
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { type TimetableHealth, fetchFaculties, fetchGroups } from "./api";
|
import { type TimetableHealthResponseEntry, fetchFaculties, fetchGroups } from "./api";
|
||||||
import strings from "./strings";
|
import strings from "./strings";
|
||||||
|
|
||||||
export async function tryFormatNamesForReport(report?: TimetableHealth): Promise<TimetableHealth | undefined>
|
export async function tryFormatNamesForReport(report?: TimetableHealthResponseEntry): Promise<TimetableHealthResponseEntry | undefined>
|
||||||
{
|
{
|
||||||
if (report === undefined)
|
if (report === undefined)
|
||||||
return report;
|
return report;
|
||||||
|
|
||||||
if (report.status === "Healthy")
|
if (report.status === "healthy")
|
||||||
return report;
|
return report;
|
||||||
|
|
||||||
const isGroupsDown: boolean = report.data["/groups"] !== undefined;
|
const isGroupsDown: boolean = report.data["/groups"] !== undefined;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Button, Dialog, DialogBody, DialogContent, DialogSurface, DialogTitle, DialogTrigger, Divider, Subtitle2 } from "@fluentui/react-components";
|
import { Button, Dialog, DialogBody, DialogContent, DialogSurface, DialogTitle, DialogTrigger, Divider, Subtitle2 } from "@fluentui/react-components";
|
||||||
import { ArrowTrendingLinesFilled, CheckmarkCircleFilled, Dismiss24Regular, WarningFilled } from "@fluentui/react-icons";
|
import { ArrowTrendingLinesFilled, CheckmarkCircleFilled, Dismiss24Regular, WarningFilled } from "@fluentui/react-icons";
|
||||||
import { use, useMemo, type ReactElement } from "react";
|
import { use, useMemo, type ReactElement } from "react";
|
||||||
import { fetchHealth, fetchStats, type StatsResponse, type TimetableHealth } from "../utils/api";
|
import { fetchHealth, fetchStats, type StatsResponse, type TimetableHealthResponseEntry } from "../utils/api";
|
||||||
import strings from "../utils/strings";
|
import strings from "../utils/strings";
|
||||||
import { tryFormatNamesForReport } from "../utils/tryFormatNamesForReport";
|
import { tryFormatNamesForReport } from "../utils/tryFormatNamesForReport";
|
||||||
import { useStyles } from "./StatsView.styles";
|
import { useStyles } from "./StatsView.styles";
|
||||||
@@ -13,7 +13,7 @@ export default function StatsView(): ReactElement
|
|||||||
{
|
{
|
||||||
const cls = useStyles();
|
const cls = useStyles();
|
||||||
|
|
||||||
const health: TimetableHealth | undefined = use(healthPromise);
|
const health: TimetableHealthResponseEntry | undefined = use(healthPromise);
|
||||||
const stats: StatsResponse = use(statsPromise);
|
const stats: StatsResponse = use(statsPromise);
|
||||||
|
|
||||||
const issueCounter: number = useMemo(() =>
|
const issueCounter: number = useMemo(() =>
|
||||||
@@ -49,7 +49,7 @@ export default function StatsView(): ReactElement
|
|||||||
}
|
}
|
||||||
<Dialog>
|
<Dialog>
|
||||||
<DialogTrigger>
|
<DialogTrigger>
|
||||||
{ health?.status === "Healthy" ?
|
{ health?.status === "healthy" ?
|
||||||
<Button icon={ <CheckmarkCircleFilled className={ cls.statusIconHealthy } /> } appearance="subtle">
|
<Button icon={ <CheckmarkCircleFilled className={ cls.statusIconHealthy } /> } appearance="subtle">
|
||||||
{ strings.status_ok }
|
{ strings.status_ok }
|
||||||
</Button>
|
</Button>
|
||||||
@@ -76,7 +76,7 @@ export default function StatsView(): ReactElement
|
|||||||
{ strings.report_title }
|
{ strings.report_title }
|
||||||
</DialogTitle>
|
</DialogTitle>
|
||||||
<DialogContent className={ cls.reportContent }>
|
<DialogContent className={ cls.reportContent }>
|
||||||
{ health?.status === "Healthy" ?
|
{ health?.status === "healthy" ?
|
||||||
<div className={ cls.reportSubtitle }>
|
<div className={ cls.reportSubtitle }>
|
||||||
<CheckmarkCircleFilled className={ cls.statusIconHealthy } fontSize={ 24 } />
|
<CheckmarkCircleFilled className={ cls.statusIconHealthy } fontSize={ 24 } />
|
||||||
<Subtitle2>{ strings.report_subtitle_ok }</Subtitle2>
|
<Subtitle2>{ strings.report_subtitle_ok }</Subtitle2>
|
||||||
@@ -89,7 +89,7 @@ export default function StatsView(): ReactElement
|
|||||||
</Subtitle2>
|
</Subtitle2>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
{ health?.status !== "Healthy" &&
|
{ health?.status !== "healthy" &&
|
||||||
<ul>
|
<ul>
|
||||||
{ health === undefined &&
|
{ health === undefined &&
|
||||||
<li>{ strings.report_issue_backend }</li>
|
<li>{ strings.report_issue_backend }</li>
|
||||||
|
|||||||
Reference in New Issue
Block a user