Fix proxy and split server into modules #1

Merged
probablycorey merged 18 commits from probablycorey/fix-proxy-404s into main 2026-03-11 16:35:00 +00:00
Showing only changes of commit 89bf052ca1 - Show all commits

View File

@ -7,6 +7,7 @@ const PORT = Number(process.env.PORT) || 3000
const SOCKET_PATH = DATA_DIR ? join(DATA_DIR, 'tronbyt.sock') : ''
const BIN_DIR = join(import.meta.dir, '..', 'bin')
let goHealthy = false
let goProcess: Subprocess | undefined
interface WsData {
@ -22,6 +23,7 @@ function proxyFetch(req: Request): Promise<Response> | Response {
const url = new URL(req.url)
if (url.pathname === '/ok') {
if (!goHealthy) return new Response('starting', { status: goProcess ? 200 : 503 })
return fetch('http://localhost/health', { unix: SOCKET_PATH })
.then((r) => (r.ok ? new Response('ok') : new Response('unhealthy', { status: 503 })))
.catch(() => new Response('unhealthy', { status: 503 }))
@ -176,10 +178,12 @@ async function spawnGoServer() {
goProcess.exited.then((code) => {
console.log(`Tronbyt server exited with code ${code}`)
goHealthy = false
goProcess = undefined
})
if (await waitForHealthy()) {
goHealthy = true
console.log('Tronbyt server is healthy')
} else {
console.error('Tronbyt server failed to become healthy')