Description
Implement a GET /health endpoint that returns the overall health of the API, including database connectivity (Prisma) and Stellar Horizon network status. This is crucial for monitoring and deployment (e.g., Kubernetes liveness probes). The endpoint should be fast, lightweight, and not require authentication. It should attempt a simple query to the database (e.g., SELECT 1 via Prisma) and a ping to Horizon, then return a JSON response with status 'ok' or 'degraded'.
Context & Requirements
- The API is deployed as a public service; a health check endpoint is standard for operational readiness.
- The endpoint must be accessible without authentication (open to all).
- Database check: use
prisma.$queryRawUnsafe('SELECT 1') to verify the connection is alive.
- Stellar check: call
HorizonServer.loadAccount('G...') with a known public key (e.g., the testnet friendbot address) or use a simple ping to Horizon server (e.g., GET / on the Horizon server). Use the existing stellarService or a direct Axios/fetch.
- Response format:
{ status: 'ok', database: { connected: true }, stellar: { reachable: true, network: 'testnet' }, timestamp: '...' }.
- If either check fails, return HTTP 503 with status 'degraded' and details.
- Timeout for each check: 5 seconds (to avoid hanging).
Acceptance Criteria
Implementation Guidance
- File:
src/routes/health.ts – create a new route file.
- Register the route in
src/index.ts (likely with app.register(import('./routes/health'))).
- Use
prisma.$queryRawUnsafe('SELECT 1') for DB check.
- For Stellar check, use
fetch('https://horizon-testnet.stellar.org/') (get the base URL from config).
- Use
Promise.allSettled to run both checks concurrently; if any fails, overall status is 'degraded'.
- Set timeout with
AbortController or Promise.race with a timeout.
- Return appropriate HTTP status code based on overall health.
Testing & Validation
- Write unit tests for the health check function (mock Prisma and fetch).
- Integration tests: call the endpoint and verify response structure.
- Test with simulated DB failure (mock reject) and verify 503.
- Run
npm test.
Submission Guidelines
- Branch from
main with name feat/health-check.
- PR includes new route file and registration.
- Closes #.
- Assign yourself before starting.
Wave complexity: Trivial (100 points)
Description
Implement a
GET /healthendpoint that returns the overall health of the API, including database connectivity (Prisma) and Stellar Horizon network status. This is crucial for monitoring and deployment (e.g., Kubernetes liveness probes). The endpoint should be fast, lightweight, and not require authentication. It should attempt a simple query to the database (e.g.,SELECT 1via Prisma) and a ping to Horizon, then return a JSON response with status 'ok' or 'degraded'.Context & Requirements
prisma.$queryRawUnsafe('SELECT 1')to verify the connection is alive.HorizonServer.loadAccount('G...')with a known public key (e.g., the testnet friendbot address) or use a simple ping to Horizon server (e.g.,GET /on the Horizon server). Use the existingstellarServiceor a direct Axios/fetch.{ status: 'ok', database: { connected: true }, stellar: { reachable: true, network: 'testnet' }, timestamp: '...' }.Acceptance Criteria
GET /healthreturns 200 with status 'ok' when both DB and Stellar are reachable.Implementation Guidance
src/routes/health.ts– create a new route file.src/index.ts(likely withapp.register(import('./routes/health'))).prisma.$queryRawUnsafe('SELECT 1')for DB check.fetch('https://horizon-testnet.stellar.org/')(get the base URL from config).Promise.allSettledto run both checks concurrently; if any fails, overall status is 'degraded'.AbortControllerorPromise.racewith a timeout.Testing & Validation
npm test.Submission Guidelines
mainwith namefeat/health-check.Wave complexity: Trivial (100 points)