Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/app/api/health/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ vi.mock("@/lib/stellar/client", () => ({
}));

const mockGetDatabaseMigrationStatus = vi.fn();
const mockRunWithDatabase = vi.fn();
vi.mock("@/lib/storage/db", () => ({
getDatabaseMigrationStatus: (...args: unknown[]) => mockGetDatabaseMigrationStatus(...args),
runWithDatabase: (...args: unknown[]) => mockRunWithDatabase(...args),
}));

const mockGetBlocklistHealth = vi.fn();
Expand All @@ -23,6 +25,7 @@ vi.mock("@/lib/security/blocklist", () => ({
describe("GET /api/health", () => {
beforeEach(() => {
vi.clearAllMocks();
mockRunWithDatabase.mockResolvedValue({ available: true });
mockGetDatabaseMigrationStatus.mockResolvedValue({
configured: false,
ready: true,
Expand Down
17 changes: 6 additions & 11 deletions src/app/api/health/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@ import { getRequestLogContext, logInfo } from "@/lib/observability/logger";
import { evaluateServiceReadiness } from "@/lib/readiness/production";
import { getBlocklistHealth } from "@/lib/security/blocklist";
import { getHorizonServer } from "@/lib/stellar/client";
import { getDatabaseMigrationStatus } from "@/lib/storage/db";
import { getDatabaseMigrationStatus, runWithDatabase } from "@/lib/storage/db";

export async function GET(request: NextRequest) {
const startedAtMs = Date.now();
const context = getRequestLogContext(request, "/api/health");

logInfo("Health check requested", context);

// Run readiness BEFORE any call to runWithDatabase(), which applies pending
// migrations and would hide a stale schema.
const readiness = await getReadiness();

const env = {
hasGroqKey: Boolean(process.env.GROQ_API_KEY),
hasAuthSecret: Boolean(process.env.FORTEXA_AUTH_SECRET),
hasHorizonUrl: Boolean(process.env.STELLAR_HORIZON_URL),
};

const migrations = await getDatabaseMigrationStatus();
const storageStatus = !migrations.configured
? "degraded"
: migrations.ready
? "healthy"
: "not_ready";
const storageCheck = await runWithDatabase("health-check", (pool) => pool.query("SELECT 1"));
const storageStatus = migrations.configured
? (migrations.ready ? "healthy" : "not_ready")
: (storageCheck.available ? "healthy" : "degraded");

let horizonStatus = "unknown";
if (env.hasHorizonUrl) {
Expand Down Expand Up @@ -57,7 +52,7 @@ export async function GET(request: NextRequest) {
const ready = storageStatus !== "not_ready";

const readiness = evaluateServiceReadiness({
databaseAvailable: storageCheck.available,
databaseAvailable: migrations.configured ? migrations.ready : storageCheck.available,
horizonStatus,
});

Expand Down
Loading