Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -81,20 +81,21 @@ app.post('/api/auth/login', (req, res) => {
|
|
| 81 |
});
|
| 82 |
app.post('/api/auth/logout', (req, res) => { activeSession = null; res.json({ success: true }); });
|
| 83 |
|
| 84 |
-
// Gemini REST Proxy (
|
| 85 |
app.post('/api/gemini/generate', async (req, res) => {
|
| 86 |
try {
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
if (!apiKey) {
|
| 90 |
-
console.error("GEMINI_API_KEY is
|
| 91 |
-
return res.status(500).json({ error: 'GEMINI_API_KEY missing' });
|
| 92 |
}
|
| 93 |
|
| 94 |
-
const {
|
| 95 |
-
const modelId = model || 'gemini-2.5-flash';
|
| 96 |
|
| 97 |
-
|
|
|
|
| 98 |
const proxyUrl = `https://corsproxy.io/?url=${encodeURIComponent(targetUrl)}`;
|
| 99 |
|
| 100 |
const response = await fetch(proxyUrl, {
|
|
@@ -104,20 +105,22 @@ app.post('/api/gemini/generate', async (req, res) => {
|
|
| 104 |
'x-goog-api-key': apiKey
|
| 105 |
},
|
| 106 |
body: JSON.stringify({
|
| 107 |
-
contents: contents
|
| 108 |
-
generationConfig: config || {}
|
| 109 |
})
|
| 110 |
});
|
| 111 |
|
| 112 |
const data = await response.json();
|
| 113 |
|
| 114 |
if (data.error) {
|
|
|
|
| 115 |
return res.status(response.status || 500).json({ error: data.error.message || 'Upstream Error' });
|
| 116 |
}
|
| 117 |
|
|
|
|
| 118 |
const text = data.candidates?.[0]?.content?.parts?.[0]?.text || '';
|
| 119 |
res.json({ text, candidates: data.candidates });
|
| 120 |
} catch (error) {
|
|
|
|
| 121 |
res.status(500).json({ error: error.message });
|
| 122 |
}
|
| 123 |
});
|
|
@@ -131,8 +134,8 @@ app.use(express.static(__dist));
|
|
| 131 |
|
| 132 |
// SPA Fallback
|
| 133 |
app.use((req, res) => {
|
| 134 |
-
if (req.path.startsWith('/api')) return res.status(404).json({ error: '
|
| 135 |
res.sendFile(path.join(__dist, 'index.html'));
|
| 136 |
});
|
| 137 |
|
| 138 |
-
app.listen(port, '0.0.0.0', () => console.log(`
|
|
|
|
| 81 |
});
|
| 82 |
app.post('/api/auth/logout', (req, res) => { activeSession = null; res.json({ success: true }); });
|
| 83 |
|
| 84 |
+
// Gemini REST Proxy (Exact Implementation as per user's curl requirement)
|
| 85 |
app.post('/api/gemini/generate', async (req, res) => {
|
| 86 |
try {
|
| 87 |
+
// Hugging Face secrets are available via process.env
|
| 88 |
+
const apiKey = process.env.GEMINI_API_KEY;
|
| 89 |
|
| 90 |
if (!apiKey) {
|
| 91 |
+
console.error("FATAL: GEMINI_API_KEY secret is not configured in Hugging Face Space.");
|
| 92 |
+
return res.status(500).json({ error: 'GEMINI_API_KEY missing from environment secrets.' });
|
| 93 |
}
|
| 94 |
|
| 95 |
+
const { contents } = req.body;
|
|
|
|
| 96 |
|
| 97 |
+
// User requested specifically gemini-2.5-flash in the URL
|
| 98 |
+
const targetUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';
|
| 99 |
const proxyUrl = `https://corsproxy.io/?url=${encodeURIComponent(targetUrl)}`;
|
| 100 |
|
| 101 |
const response = await fetch(proxyUrl, {
|
|
|
|
| 105 |
'x-goog-api-key': apiKey
|
| 106 |
},
|
| 107 |
body: JSON.stringify({
|
| 108 |
+
contents: contents
|
|
|
|
| 109 |
})
|
| 110 |
});
|
| 111 |
|
| 112 |
const data = await response.json();
|
| 113 |
|
| 114 |
if (data.error) {
|
| 115 |
+
console.error("Gemini API returned error:", data.error);
|
| 116 |
return res.status(response.status || 500).json({ error: data.error.message || 'Upstream Error' });
|
| 117 |
}
|
| 118 |
|
| 119 |
+
// Standardize text response for frontend consumption
|
| 120 |
const text = data.candidates?.[0]?.content?.parts?.[0]?.text || '';
|
| 121 |
res.json({ text, candidates: data.candidates });
|
| 122 |
} catch (error) {
|
| 123 |
+
console.error("Server proxy error:", error);
|
| 124 |
res.status(500).json({ error: error.message });
|
| 125 |
}
|
| 126 |
});
|
|
|
|
| 134 |
|
| 135 |
// SPA Fallback
|
| 136 |
app.use((req, res) => {
|
| 137 |
+
if (req.path.startsWith('/api')) return res.status(404).json({ error: 'Endpoint missing' });
|
| 138 |
res.sendFile(path.join(__dist, 'index.html'));
|
| 139 |
});
|
| 140 |
|
| 141 |
+
app.listen(port, '0.0.0.0', () => console.log(`[Lumina Proxy] Node Online on Port ${port}`));
|