File size: 1,450 Bytes
c4d103b
 
 
160a21b
 
1e16a1f
 
 
160a21b
 
c4d103b
23b8f32
 
c4d103b
1e16a1f
c4d103b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { RequestHandler } from "@sveltejs/kit";

export const POST: RequestHandler = async ({ request }) => {
    const authHeader = request.headers.get("authorization");
    if (!authHeader || !authHeader.startsWith("Bearer ")) {
        return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 });
    }

    const accessToken = authHeader.substring("Bearer ".length);

    const payload = await request.json();
    payload.access_token = accessToken;

    const url = `https://dylanebert-3d-arena-backend.hf.space/vote`;
    // const url = `http://localhost:8000/vote`;

    try {
        const response = await fetch(url, {
            method: "POST",
            headers: {
                Authorization: `Bearer ${import.meta.env.VITE_HF_TOKEN}`,
                "Cache-Control": "no-cache",
                "Content-Type": "application/json",
            },
            body: JSON.stringify(payload),
        });

        if (response.ok) {
            const result = await response.json();
            return new Response(JSON.stringify(result), {
                status: 200,
            });
        } else {
            return new Response(JSON.stringify({ error: "Failed to process vote." }), {
                status: response.status,
            });
        }
    } catch (error) {
        return new Response(JSON.stringify({ error: "Failed to process vote." }), {
            status: 500,
        });
    }
};