Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Allow HEAD method on API healthcheck (#2241)
Leendert de Borst3c3ceff388a3
diff --git a/apps/server/AliasVault.Api/Controllers/RootController.cs b/apps/server/AliasVault.Api/Controllers/RootController.csindex 13cb86ff3..1356ecc28 100644--- a/apps/server/AliasVault.Api/Controllers/RootController.cs+++ b/apps/server/AliasVault.Api/Controllers/RootController.cs@@ -24,29 +24,49 @@ public class RootController(IAliasServerDbContextFactory dbContextFactory) : Con/// </summary>/// <returns>Http 200 if database connection is successful.</returns>[HttpGet]+ [HttpHead][ProducesResponseType<int>(StatusCodes.Status200OK)][ProducesResponseType<int>(StatusCodes.Status500InternalServerError)]public async Task<IActionResult> Get(){- await using var context = await dbContextFactory.CreateDbContextAsync();-try{- var appliedMigrations = await context.Database.GetAppliedMigrationsAsync();- var allMigrations = context.Database.GetMigrations();+ await using var context = await dbContextFactory.CreateDbContextAsync();+ // Verify we can actually reach the database.+ if (!await context.Database.CanConnectAsync())+ {+ return StatusCode(500, "ERROR: Database unreachable");+ }++ // Check if the database schema exists.+ IEnumerable<string> appliedMigrations;+ try+ {+ appliedMigrations = await context.Database.GetAppliedMigrationsAsync();+ }+ catch+ {+ return StatusCode(500, "ERROR: Database schema not initialized");+ }++ // Check if the database schema is up-to-date.+ var allMigrations = context.Database.GetMigrations();if (allMigrations.Except(appliedMigrations).Any()){- // There are pending migrations- return StatusCode(500, "There are pending migrations. Please run 'dotnet ef database update' to apply them.");+ return StatusCode(500, "ERROR: Database schema outdated (pending migrations)");}- // Database is up-to-date+ // Database is reachable and up-to-date.return Ok("OK");}+ catch (Exception ex) when (ex is TimeoutException or OperationCanceledException)+ {+ return StatusCode(500, "ERROR: Database timeout");+ }catch{- return StatusCode(500, "Internal server error");+ return StatusCode(500, "ERROR: Database error");}}}