mirror of
https://github.com/OpenSquawk/OpenSquawk
synced 2026-05-13 01:46:08 +08:00
feat: warn when classroom speech server is unavailable
This commit is contained in:
53
server/api/classroom/speech-server-health.get.ts
Normal file
53
server/api/classroom/speech-server-health.get.ts
Normal 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),
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user