-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
56 lines (48 loc) · 1.39 KB
/
Copy pathroute.ts
File metadata and controls
56 lines (48 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { NextResponse } from 'next/server';
export const runtime = 'nodejs';
const DEFAULT_AGENTATION_TARGET = 'http://127.0.0.1:4747';
const DEFAULT_AGENTATION_ENDPOINT = '/api/agentation-sync';
const AGENTATION_PROXY_TARGET =
process.env.AGENTATION_PROXY_TARGET ?? DEFAULT_AGENTATION_TARGET;
const AGENTATION_ENDPOINT =
process.env.NEXT_PUBLIC_AGENTATION_ENDPOINT ?? DEFAULT_AGENTATION_ENDPOINT;
const HEALTHCHECK_TIMEOUT_MS = 1500;
function buildHealthUrl(): string {
if (
AGENTATION_ENDPOINT.startsWith('http://') ||
AGENTATION_ENDPOINT.startsWith('https://')
) {
return new URL('/health', AGENTATION_ENDPOINT).toString();
}
try {
return new URL('/health', AGENTATION_PROXY_TARGET).toString();
} catch {
return new URL('/health', DEFAULT_AGENTATION_TARGET).toString();
}
}
function jsonResponse(ok: boolean, status: number) {
return NextResponse.json(
{ ok },
{
status,
headers: {
'Cache-Control': 'no-store',
},
}
);
}
export async function GET() {
if (process.env.NODE_ENV !== 'development') {
return jsonResponse(false, 404);
}
try {
const response = await fetch(buildHealthUrl(), {
method: 'GET',
cache: 'no-store',
signal: AbortSignal.timeout(HEALTHCHECK_TIMEOUT_MS),
});
return jsonResponse(response.ok, response.ok ? 200 : 503);
} catch {
return jsonResponse(false, 503);
}
}