Skip to content

Commit ff9f860

Browse files
committed
added dedicated health endpoint with database connectivity check
1 parent 81483f1 commit ff9f860

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

.github/workflows/docker-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787
-e NODE_ENV=production \
8888
-e DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:${POSTGRES_PORT}/${POSTGRES_DB} \
8989
-e REDIS_URL=redis://redis:${REDIS_PORT} \
90-
--health-cmd="curl -sf http://localhost:3000/ || exit 1" \
90+
--health-cmd="curl -sf http://localhost:3000/health || exit 1" \
9191
--health-interval=5s \
9292
--health-timeout=3s \
9393
--health-retries=60 \

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,9 @@ services:
6262
condition: service_healthy
6363
redis:
6464
condition: service_healthy
65+
healthcheck:
66+
test: ["CMD", "curl", "-sf", "http://localhost:3000/health"]
67+
interval: 10s
68+
timeout: 5s
69+
retries: 5
70+
start_period: 30s

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ const app = express()
1010

1111
app.use(express.json())
1212

13+
app.get('/health', async (req, res) => {
14+
try {
15+
await prisma.$queryRaw`SELECT 1`
16+
res.status(200).json({ status: 'healthy', timestamp: new Date().toISOString() })
17+
} catch (error) {
18+
res.status(503).json({ status: 'unhealthy', error: 'Database connection failed' })
19+
}
20+
})
21+
1322
app.post(`/signup`, async (req, res) => {
1423
const { name, email, posts } = req.body
1524

0 commit comments

Comments
 (0)