File size: 1,401 Bytes
d5e0a0f
 
 
b4297ca
 
 
 
 
d5e0a0f
b4297ca
 
 
 
 
d5e0a0f
 
b4297ca
d5e0a0f
b4297ca
 
d5e0a0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4297ca
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
import { NextRequest, NextResponse } from "next/server";

export async function GET(request: NextRequest) {
    const healthcheck_api = process.env.NEXT_PUBLIC_HEALTHCHECK_API as string;

    // Retrieve the session token from the request headers
    let session = request.headers.get('Authorization');

    // console.log('Status API - headers:', request.headers);

    // Public API key
    let api_key = null;

    // If no session, use the public API key
    if (session === null || session === undefined || session.includes('undefined')) {
        console.log('No session token found, using public API key');
        api_key = process.env.BACKEND_API_KEY as string;
        session = null; // Clear the session token
    }

    try {
        const res = await fetch(healthcheck_api, {
            signal: AbortSignal.timeout(5000), // Abort the request if it takes longer than 5 seconds
            headers: {
                'Content-Type': 'application/json',
                'Authorization': session,
                'X-API-Key': api_key,
            } as any,
        })
        const data = await res.json()
        if (!res.ok) {
            throw new Error(data.detail || 'Unknown Error');
        }
        return NextResponse.json({ data })
    } catch (error : any) {
        console.error(`${error}`);
        return NextResponse.json({ error: error.message }, { status: 500 })
    }
}