feat: warn when classroom speech server is unavailable

This commit is contained in:
itsrubberduck
2026-04-23 09:21:23 +02:00
parent 3038f0f611
commit b7df1e86f2
2 changed files with 142 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
import { defineEventHandler } from 'h3'
import { requireUserSession } from '../../utils/auth'
import { getServerRuntimeConfig } from '../../utils/runtimeConfig'
type SpeechServerHealth = {
configured: boolean
reachable: boolean
}
function buildHealthUrls(baseUrl: string): string[] {
const trimmed = baseUrl.replace(/\/+$/, '')
return [
`${trimmed}/health`,
`${trimmed}/v1/models`,
trimmed,
]
}
async function canReach(url: string): Promise<boolean> {
const controller = new AbortController()
const timeout = setTimeout(() => controller.abort(), 2500)
try {
const response = await fetch(url, {
method: 'GET',
signal: controller.signal,
})
return response.status < 500
} catch {
return false
} finally {
clearTimeout(timeout)
}
}
export default defineEventHandler(async (event): Promise<SpeechServerHealth> => {
await requireUserSession(event)
const { speachesBaseUrl } = getServerRuntimeConfig()
if (!speachesBaseUrl) {
return {
configured: false,
reachable: false,
}
}
const results = await Promise.all(buildHealthUrls(speachesBaseUrl).map(url => canReach(url)))
return {
configured: true,
reachable: results.some(Boolean),
}
})