|
| 1 | +/** |
| 2 | + * TinyGPU Gemini API Proxy - Cloudflare Worker |
| 3 | + * |
| 4 | + * This worker proxies requests to the Gemini API, keeping your API key secure. |
| 5 | + * Deploy this to Cloudflare Workers and set the GEMINI_API_KEY secret. |
| 6 | + * |
| 7 | + * Setup Instructions: |
| 8 | + * 1. Go to https://dash.cloudflare.com/ and sign up/login |
| 9 | + * 2. Go to Workers & Pages > Create Application > Create Worker |
| 10 | + * 3. Name it something like "tinygpu-gemini-proxy" |
| 11 | + * 4. Replace the default code with this file's contents |
| 12 | + * 5. Go to Settings > Variables > Add Variable |
| 13 | + * - Name: GEMINI_API_KEY |
| 14 | + * - Value: Your Gemini API key |
| 15 | + * - Click "Encrypt" to keep it secret |
| 16 | + * 6. Save and Deploy |
| 17 | + * 7. Your worker URL will be: https://tinygpu-gemini-proxy.<your-subdomain>.workers.dev |
| 18 | + */ |
| 19 | + |
| 20 | +export default { |
| 21 | + async fetch(request, env) { |
| 22 | + // Handle CORS preflight |
| 23 | + if (request.method === "OPTIONS") { |
| 24 | + return new Response(null, { |
| 25 | + headers: { |
| 26 | + "Access-Control-Allow-Origin": "*", |
| 27 | + "Access-Control-Allow-Methods": "POST, OPTIONS", |
| 28 | + "Access-Control-Allow-Headers": "Content-Type", |
| 29 | + "Access-Control-Max-Age": "86400", |
| 30 | + }, |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + // Only allow POST requests |
| 35 | + if (request.method !== "POST") { |
| 36 | + return new Response(JSON.stringify({ error: "Method not allowed" }), { |
| 37 | + status: 405, |
| 38 | + headers: { |
| 39 | + "Content-Type": "application/json", |
| 40 | + "Access-Control-Allow-Origin": "*", |
| 41 | + }, |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + // Get the request body |
| 47 | + const body = await request.json(); |
| 48 | + |
| 49 | + // Validate required fields |
| 50 | + if (!body.prompt) { |
| 51 | + return new Response(JSON.stringify({ error: "Missing prompt" }), { |
| 52 | + status: 400, |
| 53 | + headers: { |
| 54 | + "Content-Type": "application/json", |
| 55 | + "Access-Control-Allow-Origin": "*", |
| 56 | + }, |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + // Build Gemini API request |
| 61 | + const geminiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${env.GEMINI_API_KEY}`; |
| 62 | + |
| 63 | + const geminiPayload = { |
| 64 | + contents: [{ parts: [{ text: body.prompt }] }], |
| 65 | + }; |
| 66 | + |
| 67 | + // Add system instruction if provided |
| 68 | + if (body.systemPrompt) { |
| 69 | + geminiPayload.systemInstruction = { |
| 70 | + parts: [{ text: body.systemPrompt }], |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + // Call Gemini API |
| 75 | + const geminiResponse = await fetch(geminiUrl, { |
| 76 | + method: "POST", |
| 77 | + headers: { "Content-Type": "application/json" }, |
| 78 | + body: JSON.stringify(geminiPayload), |
| 79 | + }); |
| 80 | + |
| 81 | + if (!geminiResponse.ok) { |
| 82 | + const errorText = await geminiResponse.text(); |
| 83 | + return new Response( |
| 84 | + JSON.stringify({ |
| 85 | + error: "Gemini API error", |
| 86 | + status: geminiResponse.status, |
| 87 | + details: errorText, |
| 88 | + }), |
| 89 | + { |
| 90 | + status: geminiResponse.status, |
| 91 | + headers: { |
| 92 | + "Content-Type": "application/json", |
| 93 | + "Access-Control-Allow-Origin": "*", |
| 94 | + }, |
| 95 | + } |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + const data = await geminiResponse.json(); |
| 100 | + const text = |
| 101 | + data.candidates?.[0]?.content?.parts?.[0]?.text || |
| 102 | + "No response generated."; |
| 103 | + |
| 104 | + return new Response(JSON.stringify({ text }), { |
| 105 | + headers: { |
| 106 | + "Content-Type": "application/json", |
| 107 | + "Access-Control-Allow-Origin": "*", |
| 108 | + }, |
| 109 | + }); |
| 110 | + } catch (error) { |
| 111 | + return new Response(JSON.stringify({ error: error.message }), { |
| 112 | + status: 500, |
| 113 | + headers: { |
| 114 | + "Content-Type": "application/json", |
| 115 | + "Access-Control-Allow-Origin": "*", |
| 116 | + }, |
| 117 | + }); |
| 118 | + } |
| 119 | + }, |
| 120 | +}; |
0 commit comments