-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
61 lines (54 loc) · 1.74 KB
/
Copy pathworker.js
File metadata and controls
61 lines (54 loc) · 1.74 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
57
58
59
60
61
// Cloudflare Worker para proteger la API Key de Groq
export default {
async fetch(request, env) {
// Configurar CORS para permitir requests desde tu dominio
const corsHeaders = {
'Access-Control-Allow-Origin': '*', // Cambia '*' por tu dominio en producción
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
// Manejar preflight request
if (request.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
// Solo aceptar POST requests
if (request.method !== 'POST') {
return new Response('Method not allowed', {
status: 405,
headers: corsHeaders
});
}
try {
// Obtener el body del request
const body = await request.json();
// Llamar a la API de Groq con la key segura desde las variables de entorno
const response = await fetch('https://api.groq.com/openai/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${env.GROQ_API_KEY}` // La key está en las variables de entorno
},
body: JSON.stringify(body)
});
const data = await response.json();
// Retornar la respuesta de Groq
return new Response(JSON.stringify(data), {
status: response.status,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
} catch (error) {
return new Response(JSON.stringify({
error: { message: 'Error en el servidor' }
}), {
status: 500,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
}
};