Phase 1: Monorepo scaffold, database, and dev environment
Turborepo monorepo with @forte/shared and @forte/backend workspaces. Docker Compose dev env with PostgreSQL 16 + Valkey 8. Fastify server with Pino JSON logging, request ID tracing, and health endpoint. Drizzle ORM with company + location tables. Includes: - Root config (turbo, tsconfig, eslint, prettier) - @forte/shared: types, schemas, currency/date utils - @forte/backend: Fastify entry, plugins (database, redis, cors, error-handler, dev-auth), health route, Drizzle schema + migration - Dev auth bypass via X-Dev-Company/Location/User headers - Vitest integration test with clean DB per test (forte_test) - Seed script for dev company + location
This commit is contained in:
30
packages/backend/src/routes/v1/health.test.ts
Normal file
30
packages/backend/src/routes/v1/health.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
||||
import type { FastifyInstance } from 'fastify'
|
||||
import { createTestApp } from '../../test/helpers.js'
|
||||
|
||||
describe('GET /v1/health', () => {
|
||||
let app: FastifyInstance
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await createTestApp()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close()
|
||||
})
|
||||
|
||||
it('returns ok when db and redis are connected', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/health',
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
|
||||
const body = response.json()
|
||||
expect(body.status).toBe('ok')
|
||||
expect(body.db).toBe('connected')
|
||||
expect(body.redis).toBe('connected')
|
||||
expect(body.timestamp).toBeDefined()
|
||||
})
|
||||
})
|
||||
32
packages/backend/src/routes/v1/health.ts
Normal file
32
packages/backend/src/routes/v1/health.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { FastifyPluginAsync } from 'fastify'
|
||||
import { sql } from 'drizzle-orm'
|
||||
|
||||
export const healthRoutes: FastifyPluginAsync = async (app) => {
|
||||
app.get('/health', async (request, reply) => {
|
||||
let dbStatus = 'disconnected'
|
||||
let redisStatus = 'disconnected'
|
||||
|
||||
try {
|
||||
await app.db.execute(sql`SELECT 1`)
|
||||
dbStatus = 'connected'
|
||||
} catch (err) {
|
||||
request.log.error({ err }, 'Database health check failed')
|
||||
}
|
||||
|
||||
try {
|
||||
const pong = await app.redis.ping()
|
||||
redisStatus = pong === 'PONG' ? 'connected' : 'disconnected'
|
||||
} catch (err) {
|
||||
request.log.error({ err }, 'Redis health check failed')
|
||||
}
|
||||
|
||||
const healthy = dbStatus === 'connected' && redisStatus === 'connected'
|
||||
|
||||
reply.status(healthy ? 200 : 503).send({
|
||||
status: healthy ? 'ok' : 'degraded',
|
||||
db: dbStatus,
|
||||
redis: redisStatus,
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user