MotorSignal-App/website/app/api/ops/health/route.ts

57 lines
1.7 KiB
TypeScript

import { cookies } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';
import { normalizeHealthResponse } from '../../../../lib/ops-data';
export async function GET(request: NextRequest) {
const cookieStore = await cookies();
const isProduction = process.env.NODE_ENV === 'production';
const opsToken = isProduction
? cookieStore.get('__Host-motorsignal_ops')?.value
: cookieStore.get('__Host-motorsignal_ops')?.value ??
cookieStore.get('motorsignal_ops')?.value;
const baseUrl = process.env.MOTORSIGNAL_API_BASE_URL ?? 'https://api.motorsignal.app';
const headers: Record<string, string> = {
accept: 'application/json',
};
if (opsToken) {
headers['authorization'] = `Bearer ${opsToken}`;
}
try {
const response = await fetch(`${baseUrl}/api/v1/ops/health`, {
method: 'GET',
headers,
cache: 'no-store',
});
if (!response.ok) {
if (response.status === 401) {
return NextResponse.json(
{ detail: 'Authentication required.' },
{ status: 401 }
);
}
if (response.status === 403) {
return NextResponse.json(
{ detail: 'Access restricted.' },
{ status: 403 }
);
}
return NextResponse.json(
normalizeHealthResponse({ status: 'unavailable', checked_at: new Date().toISOString() }),
{ status: 503 }
);
}
const data = await response.json();
return NextResponse.json(normalizeHealthResponse(data), { status: 200 });
} catch {
return NextResponse.json(
normalizeHealthResponse({ status: 'unavailable', checked_at: new Date().toISOString() }),
{ status: 503 }
);
}
}