Spaces:
Running
Running
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, | |
}); | |
} | |
}; | |