feat: add /api/health endpoint for uptime monitoring
This commit is contained in:
parent
1875f16d48
commit
15fdf77be8
1 changed files with 19 additions and 0 deletions
19
server/api/health.get.js
Normal file
19
server/api/health.get.js
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import mongoose from 'mongoose'
|
||||||
|
import { connectDB } from '../utils/mongoose.js'
|
||||||
|
|
||||||
|
export default defineEventHandler(async () => {
|
||||||
|
try {
|
||||||
|
await connectDB()
|
||||||
|
const dbState = mongoose.connection.readyState
|
||||||
|
// readyState: 0 = disconnected, 1 = connected, 2 = connecting, 3 = disconnecting
|
||||||
|
if (dbState !== 1) {
|
||||||
|
throw new Error('Database not connected')
|
||||||
|
}
|
||||||
|
return { status: 'ok', db: 'connected' }
|
||||||
|
} catch {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 503,
|
||||||
|
data: { status: 'error', db: 'disconnected' },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
Loading…
Add table
Add a link
Reference in a new issue