From 5d8b71c4352757e4d44bc66757abfd41f0af4f32 Mon Sep 17 00:00:00 2001 From: PatTheEvil Date: Wed, 2 Dec 2026 18:54:15 +0100 Subject: [PATCH] =?UTF-8?q?web:=20verze=20API=20na=20jednom=20m=C3=ADst?= =?UTF-8?q?=C4=9B=20a=20=C4=8D=C3=ADslo=20verze=20v=20pati=C4=8Dce?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++ web/Helpdesk.Web.Tests/IssueTests.cs | 2 +- web/Helpdesk.Web.Tests/VersioningTests.cs | 31 +++++++++++++++++++ web/Helpdesk.Web/Api/ApiOptions.cs | 13 ++++++++ web/Helpdesk.Web/Api/AuthApiClient.cs | 14 ++++----- web/Helpdesk.Web/Api/HelpdeskApiClient.cs | 16 +++++----- web/Helpdesk.Web/Helpdesk.Web.csproj | 9 ++++++ web/Helpdesk.Web/Infrastructure/AppVersion.cs | 24 ++++++++++++++ web/Helpdesk.Web/Program.cs | 2 +- web/Helpdesk.Web/Views/Shared/_Layout.cshtml | 1 + web/Helpdesk.Web/wwwroot/css/site.css | 3 +- 11 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 web/Helpdesk.Web.Tests/VersioningTests.cs create mode 100644 web/Helpdesk.Web/Infrastructure/AppVersion.cs diff --git a/README.md b/README.md index e0fd9c9..ffd9884 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,11 @@ dotnet test --solution web/Helpdesk.Web.sln Schéma se mění výhradně novými číslovanými skripty `db/migrations/V{NNN}__{popis}.sql`. Provedený skript se nikdy neupravuje. Migrace spouští DbUp (`Helpdesk.Migrator`) před startem API a eviduje je v tabulce `schema_version`. +## Verzování webové aplikace + +- **Verze API:** web volá REST API `/api/v1`. Verze je na jednom místě, `ApiOptions.ApiVersion` v `web/Helpdesk.Web/Api/ApiOptions.cs`. Klienti API používají jen relativní cesty (`issues`, `auth/login`), takže přechod na `/api/v2` znamená změnit tuto konstantu a upravit DTO, která se ve v2 změnila. +- **Verze webu:** `` v `web/Helpdesk.Web/Helpdesk.Web.csproj` podle semver – před vydáním 1.0 zvyšujeme MINOR za novou část aplikace (závady, návrhy, …) a PATCH za opravy. Verze se zobrazuje v patičce webu spolu s krátkým hashem commitu, ze kterého byl web sestaven. + ## Technologie Backend C# / ASP.NET Core Web API · EF Core + Pomelo (MariaDB) · DbUp · JWT (RFC 7519) · Swagger · Web ASP.NET Core MVC · Mobil Kotlin + Jetpack Compose · Docker diff --git a/web/Helpdesk.Web.Tests/IssueTests.cs b/web/Helpdesk.Web.Tests/IssueTests.cs index b918ef3..b7175cd 100644 --- a/web/Helpdesk.Web.Tests/IssueTests.cs +++ b/web/Helpdesk.Web.Tests/IssueTests.cs @@ -61,7 +61,7 @@ public class CreateIssueRequestTests public async Task Hlaseni_s_prilohou_jde_do_API_jako_multipart() { var handler = new RecordingHandler(); - var api = new HelpdeskApiClient(new HttpClient(handler) { BaseAddress = new Uri("http://api/") }); + var api = new HelpdeskApiClient(new HttpClient(handler) { BaseAddress = new ApiOptions { BaseUrl = "http://api/" }.VersionedBaseAddress() }); var issue = await api.CreateIssueAsync( new CreateIssueRequest("Projektor", "Bliká.", 2, BuildingId: null, LocationDetail: "B-205"), diff --git a/web/Helpdesk.Web.Tests/VersioningTests.cs b/web/Helpdesk.Web.Tests/VersioningTests.cs new file mode 100644 index 0000000..093bce3 --- /dev/null +++ b/web/Helpdesk.Web.Tests/VersioningTests.cs @@ -0,0 +1,31 @@ +using Helpdesk.Web.Api; +using Helpdesk.Web.Infrastructure; + +namespace Helpdesk.Web.Tests; + +public class ApiVersioningTests +{ + [Theory] + [InlineData("http://api:8080/")] + [InlineData("http://api:8080")] + public void Zaklad_adresy_obsahuje_verzi_API(string baseUrl) => + Assert.Equal("http://api:8080/api/v1/", new ApiOptions { BaseUrl = baseUrl }.VersionedBaseAddress().ToString()); + + [Fact] + public void Relativni_cesta_se_slozi_pod_verzi() + { + var baseAddress = new ApiOptions { BaseUrl = "http://api:8080/" }.VersionedBaseAddress(); + + Assert.Equal("/api/v1/auth/login", new Uri(baseAddress, "auth/login").AbsolutePath); + } +} + +public class AppVersionTests +{ + [Theory] + [InlineData("0.2.0+6e742b4029cca8896173c0d64783b7fb7540f214", "0.2.0 (6e742b4)")] + [InlineData("0.2.0", "0.2.0")] + [InlineData(null, "neznámá")] + public void Verze_se_zobrazi_s_kratkym_hashem_commitu(string? informational, string expected) => + Assert.Equal(expected, AppVersion.Format(informational)); +} diff --git a/web/Helpdesk.Web/Api/ApiOptions.cs b/web/Helpdesk.Web/Api/ApiOptions.cs index 1a9dacb..292a2a5 100644 --- a/web/Helpdesk.Web/Api/ApiOptions.cs +++ b/web/Helpdesk.Web/Api/ApiOptions.cs @@ -5,8 +5,21 @@ public sealed class ApiOptions { public const string Section = "Api"; + /// + /// Hlavní verze REST API, proti které je web napsaný (kap. 11 návrhu). Je jen tady: + /// klienti API používají relativní cesty (issues, auth/login) a verze je + /// součástí . Přechod na v2 = změna této + /// konstanty a DTO, která se ve v2 změnila. Záměrně to není konfigurace – web je + /// napsaný proti konkrétní verzi a s jinou by nefungoval. + /// + public const string ApiVersion = "v1"; + + /// Adresa backendu bez cesty API, např. http://api:8080/. public string BaseUrl { get; set; } = ""; /// Delší kvůli nahrávání příloh (až 5 × 10 MB) na pomalejším připojení. public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(100); + + /// Základ pro všechna volání: {BaseUrl}/api/v1/ (koncové lomítko je nutné pro relativní cesty). + public Uri VersionedBaseAddress() => new(new Uri(BaseUrl.EndsWith('/') ? BaseUrl : BaseUrl + "/"), $"api/{ApiVersion}/"); } diff --git a/web/Helpdesk.Web/Api/AuthApiClient.cs b/web/Helpdesk.Web/Api/AuthApiClient.cs index 1e6caa5..4351947 100644 --- a/web/Helpdesk.Web/Api/AuthApiClient.cs +++ b/web/Helpdesk.Web/Api/AuthApiClient.cs @@ -4,47 +4,47 @@ using System.Net.Http.Json; namespace Helpdesk.Web.Api; /// -/// Anonymní endpointy /api/v1/auth. Záměrně bez : +/// Anonymní endpointy auth/*. Záměrně bez : /// obnova tokenu běží uvnitř ověřování cookie a čtení tokenu z cookie by se zacyklilo. /// public sealed class AuthApiClient(HttpClient http) { public async Task LoginAsync(LoginRequest request, CancellationToken ct) { - using var response = await http.PostAsJsonAsync("api/v1/auth/login", request, ct); + using var response = await http.PostAsJsonAsync("auth/login", request, ct); return await ApiResponse.ReadAsync(response, ct); } /// Registrace. Roli (student / vyučující) určí API podle domény e-mailu. public async Task RegisterAsync(RegisterRequest request, CancellationToken ct) { - using var response = await http.PostAsJsonAsync("api/v1/auth/register", request, ct); + using var response = await http.PostAsJsonAsync("auth/register", request, ct); return await ApiResponse.ReadAsync(response, ct); } public async Task ConfirmEmailAsync(string token, CancellationToken ct) { - using var response = await http.PostAsJsonAsync("api/v1/auth/confirm-email", new EmailTokenRequest(token), ct); + using var response = await http.PostAsJsonAsync("auth/confirm-email", new EmailTokenRequest(token), ct); await ApiResponse.EnsureSuccessAsync(response, ct); } /// API odpoví stejně, ať účet existuje, nebo ne – stránka tak neprozradí registrované e-maily. public async Task ForgotPasswordAsync(string email, CancellationToken ct) { - using var response = await http.PostAsJsonAsync("api/v1/auth/forgot-password", new ForgotPasswordRequest(email), ct); + using var response = await http.PostAsJsonAsync("auth/forgot-password", new ForgotPasswordRequest(email), ct); await ApiResponse.EnsureSuccessAsync(response, ct); } public async Task ResetPasswordAsync(ResetPasswordRequest request, CancellationToken ct) { - using var response = await http.PostAsJsonAsync("api/v1/auth/reset-password", request, ct); + using var response = await http.PostAsJsonAsync("auth/reset-password", request, ct); await ApiResponse.EnsureSuccessAsync(response, ct); } /// Obnoví pár tokenů. Neplatný nebo už použitý refresh token → null. public async Task RefreshAsync(string refreshToken, CancellationToken ct) { - using var response = await http.PostAsJsonAsync("api/v1/auth/refresh", new RefreshTokenRequest(refreshToken), ct); + using var response = await http.PostAsJsonAsync("auth/refresh", new RefreshTokenRequest(refreshToken), ct); if (response.StatusCode == HttpStatusCode.Unauthorized) return null; return (await ApiResponse.ReadAsync(response, ct)).Tokens; diff --git a/web/Helpdesk.Web/Api/HelpdeskApiClient.cs b/web/Helpdesk.Web/Api/HelpdeskApiClient.cs index 025b37e..2d2ac55 100644 --- a/web/Helpdesk.Web/Api/HelpdeskApiClient.cs +++ b/web/Helpdesk.Web/Api/HelpdeskApiClient.cs @@ -10,13 +10,13 @@ public sealed class HelpdeskApiClient(HttpClient http) { public async Task GetMeAsync(CancellationToken ct) { - using var response = await http.GetAsync("api/v1/users/me", ct); + using var response = await http.GetAsync("users/me", ct); return await ApiResponse.ReadAsync(response, ct); } public async Task UpdateMeAsync(UpdateProfileRequest request, CancellationToken ct) { - using var response = await http.PutAsJsonAsync("api/v1/users/me", request, ct); + using var response = await http.PutAsJsonAsync("users/me", request, ct); return await ApiResponse.ReadAsync(response, ct); } @@ -30,7 +30,7 @@ public sealed class HelpdeskApiClient(HttpClient http) ["page"] = query.Page.ToString(CultureInfo.InvariantCulture), ["pageSize"] = query.PageSize.ToString(CultureInfo.InvariantCulture), }; - using var response = await http.GetAsync(QueryHelpers.AddQueryString("api/v1/issues", parameters.Where(p => !string.IsNullOrEmpty(p.Value))), ct); + using var response = await http.GetAsync(QueryHelpers.AddQueryString("issues", parameters.Where(p => !string.IsNullOrEmpty(p.Value))), ct); return await ApiResponse.ReadAsync>(response, ct); } @@ -60,33 +60,33 @@ public sealed class HelpdeskApiClient(HttpClient http) content.Add(part, "files", file.FileName); } - using var response = await http.PostAsync("api/v1/issues", content, ct); + using var response = await http.PostAsync("issues", content, ct); return await ApiResponse.ReadAsync(response, ct); } public async Task> GetIssueCategoriesAsync(CancellationToken ct) { - using var response = await http.GetAsync("api/v1/issue-categories", ct); + using var response = await http.GetAsync("issue-categories", ct); return await ApiResponse.ReadAsync>(response, ct); } public async Task> GetBuildingsAsync(CancellationToken ct) { - using var response = await http.GetAsync("api/v1/buildings", ct); + using var response = await http.GetAsync("buildings", ct); return await ApiResponse.ReadAsync>(response, ct); } /// Odhlásí ze všech zařízení – API zneplatní všechny tokeny uživatele. public async Task LogoutAllAsync(CancellationToken ct) { - using var response = await http.PostAsync("api/v1/auth/logout-all", content: null, ct); + using var response = await http.PostAsync("auth/logout-all", content: null, ct); await ApiResponse.EnsureSuccessAsync(response, ct); } /// Odhlásí toto zařízení – API zneplatní aktuální access token i předaný refresh token. public async Task LogoutAsync(string refreshToken, CancellationToken ct) { - using var response = await http.PostAsJsonAsync("api/v1/auth/logout", new RefreshTokenRequest(refreshToken), ct); + using var response = await http.PostAsJsonAsync("auth/logout", new RefreshTokenRequest(refreshToken), ct); await ApiResponse.EnsureSuccessAsync(response, ct); } } diff --git a/web/Helpdesk.Web/Helpdesk.Web.csproj b/web/Helpdesk.Web/Helpdesk.Web.csproj index 859a8fe..64a026b 100644 --- a/web/Helpdesk.Web/Helpdesk.Web.csproj +++ b/web/Helpdesk.Web/Helpdesk.Web.csproj @@ -1,5 +1,14 @@ + + + 0.2.0 + + diff --git a/web/Helpdesk.Web/Infrastructure/AppVersion.cs b/web/Helpdesk.Web/Infrastructure/AppVersion.cs new file mode 100644 index 0000000..68ea900 --- /dev/null +++ b/web/Helpdesk.Web/Infrastructure/AppVersion.cs @@ -0,0 +1,24 @@ +using System.Reflection; + +namespace Helpdesk.Web.Infrastructure; + +/// +/// Verze běžící webové aplikace z <Version> v projektu. SDK k ní při sestavení +/// v Gitu přidá hash commitu (0.2.0+6e742b4…); zobrazujeme jeho prvních 7 znaků, +/// aby při ladění bylo jasné, který commit běží. +/// +public static class AppVersion +{ + public static string Display { get; } = Format( + typeof(AppVersion).Assembly.GetCustomAttribute()?.InformationalVersion); + + internal static string Format(string? informationalVersion) + { + if (string.IsNullOrWhiteSpace(informationalVersion)) + return "neznámá"; + + var parts = informationalVersion.Split('+', 2); + var commit = parts.Length == 2 ? parts[1].Split('.')[0] : null; + return string.IsNullOrEmpty(commit) ? parts[0] : $"{parts[0]} ({commit[..Math.Min(7, commit.Length)]})"; + } +} diff --git a/web/Helpdesk.Web/Program.cs b/web/Helpdesk.Web/Program.cs index c83134f..85b4272 100644 --- a/web/Helpdesk.Web/Program.cs +++ b/web/Helpdesk.Web/Program.cs @@ -31,7 +31,7 @@ builder.Services.AddOptions() static void ConfigureApiClient(IServiceProvider services, HttpClient http) { var options = services.GetRequiredService>().Value; - http.BaseAddress = new Uri(options.BaseUrl.EndsWith('/') ? options.BaseUrl : options.BaseUrl + "/"); + http.BaseAddress = options.VersionedBaseAddress(); http.Timeout = options.Timeout; } diff --git a/web/Helpdesk.Web/Views/Shared/_Layout.cshtml b/web/Helpdesk.Web/Views/Shared/_Layout.cshtml index 30eee60..2286717 100644 --- a/web/Helpdesk.Web/Views/Shared/_Layout.cshtml +++ b/web/Helpdesk.Web/Views/Shared/_Layout.cshtml @@ -51,6 +51,7 @@
Univerzitní helpdesk a portál návrhů na zlepšení · NNPRO + verze @AppVersion.Display
diff --git a/web/Helpdesk.Web/wwwroot/css/site.css b/web/Helpdesk.Web/wwwroot/css/site.css index c1b90ba..23cef67 100644 --- a/web/Helpdesk.Web/wwwroot/css/site.css +++ b/web/Helpdesk.Web/wwwroot/css/site.css @@ -105,7 +105,8 @@ body { min-height: 100vh; display: flex; flex-direction: column; } .app-empty { max-width: 32rem; margin: 3rem auto; text-align: center; } .app-empty-code { font-size: 3rem; font-weight: 700; color: var(--app-accent); margin-bottom: 0; font-variant-numeric: tabular-nums; } -.app-footer { padding-block: 1.25rem; color: var(--app-muted); font-size: .875rem; border-top: 1px solid var(--app-line); } +.app-footer { display: flex; flex-wrap: wrap; justify-content: space-between; gap: .25rem 1rem; padding-block: 1.25rem; color: var(--app-muted); font-size: .875rem; border-top: 1px solid var(--app-line); } +.app-footer-version { font-variant-numeric: tabular-nums; } /* seznam závad – karta jako IssueCard v mobilu */ .app-issue-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .75rem; }