web: verze API na jednom místě a číslo verze v patičce
This commit is contained in:
1 parent
946ff5ce12
commit
5d8b71c435
11 files changed
+102
-18
No files matched your search
@@ -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`.
|
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:** `<Version>` 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
|
## 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
|
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
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ public class CreateIssueRequestTests
|
|||||||
public async Task Hlaseni_s_prilohou_jde_do_API_jako_multipart()
|
public async Task Hlaseni_s_prilohou_jde_do_API_jako_multipart()
|
||||||
{
|
{
|
||||||
var handler = new RecordingHandler();
|
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(
|
var issue = await api.CreateIssueAsync(
|
||||||
new CreateIssueRequest("Projektor", "Bliká.", 2, BuildingId: null, LocationDetail: "B-205"),
|
new CreateIssueRequest("Projektor", "Bliká.", 2, BuildingId: null, LocationDetail: "B-205"),
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
@@ -5,8 +5,21 @@ public sealed class ApiOptions
|
|||||||
{
|
{
|
||||||
public const string Section = "Api";
|
public const string Section = "Api";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Hlavní verze REST API, proti které je web napsaný (kap. 11 návrhu). Je jen tady:
|
||||||
|
/// klienti API používají relativní cesty (<c>issues</c>, <c>auth/login</c>) a verze je
|
||||||
|
/// součástí <see cref="HttpClient.BaseAddress"/>. Přechod na <c>v2</c> = 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.
|
||||||
|
/// </summary>
|
||||||
|
public const string ApiVersion = "v1";
|
||||||
|
|
||||||
|
/// <summary>Adresa backendu bez cesty API, např. <c>http://api:8080/</c>.</summary>
|
||||||
public string BaseUrl { get; set; } = "";
|
public string BaseUrl { get; set; } = "";
|
||||||
|
|
||||||
/// <summary>Delší kvůli nahrávání příloh (až 5 × 10 MB) na pomalejším připojení.</summary>
|
/// <summary>Delší kvůli nahrávání příloh (až 5 × 10 MB) na pomalejším připojení.</summary>
|
||||||
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(100);
|
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(100);
|
||||||
|
|
||||||
|
/// <summary>Základ pro všechna volání: <c>{BaseUrl}/api/v1/</c> (koncové lomítko je nutné pro relativní cesty).</summary>
|
||||||
|
public Uri VersionedBaseAddress() => new(new Uri(BaseUrl.EndsWith('/') ? BaseUrl : BaseUrl + "/"), $"api/{ApiVersion}/");
|
||||||
}
|
}
|
||||||
@@ -4,47 +4,47 @@ using System.Net.Http.Json;
|
|||||||
namespace Helpdesk.Web.Api;
|
namespace Helpdesk.Web.Api;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Anonymní endpointy <c>/api/v1/auth</c>. Záměrně bez <see cref="BearerTokenHandler"/>:
|
/// Anonymní endpointy <c>auth/*</c>. Záměrně bez <see cref="BearerTokenHandler"/>:
|
||||||
/// obnova tokenu běží uvnitř ověřování cookie a čtení tokenu z cookie by se zacyklilo.
|
/// obnova tokenu běží uvnitř ověřování cookie a čtení tokenu z cookie by se zacyklilo.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class AuthApiClient(HttpClient http)
|
public sealed class AuthApiClient(HttpClient http)
|
||||||
{
|
{
|
||||||
public async Task<AuthResponse> LoginAsync(LoginRequest request, CancellationToken ct)
|
public async Task<AuthResponse> 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<AuthResponse>(response, ct);
|
return await ApiResponse.ReadAsync<AuthResponse>(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Registrace. Roli (student / vyučující) určí API podle domény e-mailu.</summary>
|
/// <summary>Registrace. Roli (student / vyučující) určí API podle domény e-mailu.</summary>
|
||||||
public async Task<UserDto> RegisterAsync(RegisterRequest request, CancellationToken ct)
|
public async Task<UserDto> 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<UserDto>(response, ct);
|
return await ApiResponse.ReadAsync<UserDto>(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ConfirmEmailAsync(string token, CancellationToken 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);
|
await ApiResponse.EnsureSuccessAsync(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>API odpoví stejně, ať účet existuje, nebo ne – stránka tak neprozradí registrované e-maily.</summary>
|
/// <summary>API odpoví stejně, ať účet existuje, nebo ne – stránka tak neprozradí registrované e-maily.</summary>
|
||||||
public async Task ForgotPasswordAsync(string email, CancellationToken ct)
|
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);
|
await ApiResponse.EnsureSuccessAsync(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ResetPasswordAsync(ResetPasswordRequest request, CancellationToken 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);
|
await ApiResponse.EnsureSuccessAsync(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Obnoví pár tokenů. Neplatný nebo už použitý refresh token → <c>null</c>.</summary>
|
/// <summary>Obnoví pár tokenů. Neplatný nebo už použitý refresh token → <c>null</c>.</summary>
|
||||||
public async Task<TokenSet?> RefreshAsync(string refreshToken, CancellationToken ct)
|
public async Task<TokenSet?> 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)
|
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||||
return null;
|
return null;
|
||||||
return (await ApiResponse.ReadAsync<AuthResponse>(response, ct)).Tokens;
|
return (await ApiResponse.ReadAsync<AuthResponse>(response, ct)).Tokens;
|
||||||
|
|||||||
@@ -10,13 +10,13 @@ public sealed class HelpdeskApiClient(HttpClient http)
|
|||||||
{
|
{
|
||||||
public async Task<UserDto> GetMeAsync(CancellationToken ct)
|
public async Task<UserDto> 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<UserDto>(response, ct);
|
return await ApiResponse.ReadAsync<UserDto>(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<UserDto> UpdateMeAsync(UpdateProfileRequest request, CancellationToken ct)
|
public async Task<UserDto> 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<UserDto>(response, ct);
|
return await ApiResponse.ReadAsync<UserDto>(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ public sealed class HelpdeskApiClient(HttpClient http)
|
|||||||
["page"] = query.Page.ToString(CultureInfo.InvariantCulture),
|
["page"] = query.Page.ToString(CultureInfo.InvariantCulture),
|
||||||
["pageSize"] = query.PageSize.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<PagedResult<IssueSummary>>(response, ct);
|
return await ApiResponse.ReadAsync<PagedResult<IssueSummary>>(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,33 +60,33 @@ public sealed class HelpdeskApiClient(HttpClient http)
|
|||||||
content.Add(part, "files", file.FileName);
|
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<IssueDetail>(response, ct);
|
return await ApiResponse.ReadAsync<IssueDetail>(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IReadOnlyList<CategoryDto>> GetIssueCategoriesAsync(CancellationToken ct)
|
public async Task<IReadOnlyList<CategoryDto>> 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<List<CategoryDto>>(response, ct);
|
return await ApiResponse.ReadAsync<List<CategoryDto>>(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IReadOnlyList<BuildingDto>> GetBuildingsAsync(CancellationToken ct)
|
public async Task<IReadOnlyList<BuildingDto>> 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<List<BuildingDto>>(response, ct);
|
return await ApiResponse.ReadAsync<List<BuildingDto>>(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Odhlásí ze všech zařízení – API zneplatní všechny tokeny uživatele.</summary>
|
/// <summary>Odhlásí ze všech zařízení – API zneplatní všechny tokeny uživatele.</summary>
|
||||||
public async Task LogoutAllAsync(CancellationToken ct)
|
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);
|
await ApiResponse.EnsureSuccessAsync(response, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Odhlásí toto zařízení – API zneplatní aktuální access token i předaný refresh token.</summary>
|
/// <summary>Odhlásí toto zařízení – API zneplatní aktuální access token i předaný refresh token.</summary>
|
||||||
public async Task LogoutAsync(string refreshToken, CancellationToken ct)
|
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);
|
await ApiResponse.EnsureSuccessAsync(response, ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,14 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<!--
|
||||||
|
Verze webové aplikace (semver, před vydáním 1.0 jako 0.x):
|
||||||
|
MINOR = nová část aplikace (závady, návrhy, …), PATCH = opravy.
|
||||||
|
Zobrazuje se v patičce; při sestavení z Gitu se k ní přidá krátký hash commitu.
|
||||||
|
-->
|
||||||
|
<Version>0.2.0</Version>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<InternalsVisibleTo Include="Helpdesk.Web.Tests" />
|
<InternalsVisibleTo Include="Helpdesk.Web.Tests" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Helpdesk.Web.Infrastructure;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verze běžící webové aplikace z <c><Version></c> v projektu. SDK k ní při sestavení
|
||||||
|
/// v Gitu přidá hash commitu (<c>0.2.0+6e742b4…</c>); zobrazujeme jeho prvních 7 znaků,
|
||||||
|
/// aby při ladění bylo jasné, který commit běží.
|
||||||
|
/// </summary>
|
||||||
|
public static class AppVersion
|
||||||
|
{
|
||||||
|
public static string Display { get; } = Format(
|
||||||
|
typeof(AppVersion).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.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)]})";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,7 +31,7 @@ builder.Services.AddOptions<ApiOptions>()
|
|||||||
static void ConfigureApiClient(IServiceProvider services, HttpClient http)
|
static void ConfigureApiClient(IServiceProvider services, HttpClient http)
|
||||||
{
|
{
|
||||||
var options = services.GetRequiredService<IOptions<ApiOptions>>().Value;
|
var options = services.GetRequiredService<IOptions<ApiOptions>>().Value;
|
||||||
http.BaseAddress = new Uri(options.BaseUrl.EndsWith('/') ? options.BaseUrl : options.BaseUrl + "/");
|
http.BaseAddress = options.VersionedBaseAddress();
|
||||||
http.Timeout = options.Timeout;
|
http.Timeout = options.Timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,7 @@
|
|||||||
|
|
||||||
<footer class="app-footer container-xl">
|
<footer class="app-footer container-xl">
|
||||||
Univerzitní helpdesk a portál návrhů na zlepšení · NNPRO
|
Univerzitní helpdesk a portál návrhů na zlepšení · NNPRO
|
||||||
|
<span class="app-footer-version">verze @AppVersion.Display</span>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
<script src="~/lib/bootstrap/js/bootstrap.bundle.min.js"></script>
|
<script src="~/lib/bootstrap/js/bootstrap.bundle.min.js"></script>
|
||||||
|
|||||||
@@ -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 { 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-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 */
|
/* seznam závad – karta jako IssueCard v mobilu */
|
||||||
.app-issue-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .75rem; }
|
.app-issue-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .75rem; }
|
||||||
|
|||||||
Reference in new issue
Block a user