-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
111 lines (96 loc) · 3.22 KB
/
server.ts
File metadata and controls
111 lines (96 loc) · 3.22 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import express from "express";
import { createServer as createViteServer } from "vite";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json({ limit: '50mb' }));
// API routes FIRST
app.post("/api/models", async (req, res) => {
try {
const { endpoint, headers } = req.body;
const response = await fetch(endpoint, {
method: 'GET',
headers: headers
});
if (!response.ok) {
const errData = await response.json().catch(() => ({}));
res.status(response.status).json({ error: errData.error?.message || errData.message || `HTTP error! status: ${response.status}` });
return;
}
const data = await response.json();
res.json(data);
} catch (error: any) {
console.error("Proxy error:", error);
res.status(500).json({ error: error.message || "Internal server error" });
}
});
app.post("/api/chat", async (req, res) => {
try {
const { endpoint, headers, body, stream } = req.body;
let fetchEndpoint = endpoint;
if (stream) {
body.stream = true;
if (endpoint.includes('generativelanguage.googleapis.com')) {
fetchEndpoint = endpoint.replace(':generateContent', ':streamGenerateContent?alt=sse');
delete body.stream; // Gemini uses endpoint for streaming, not body
}
}
const response = await fetch(fetchEndpoint, {
method: 'POST',
headers: headers,
body: JSON.stringify(body)
});
if (!response.ok) {
const errData = await response.json().catch(() => ({}));
res.status(response.status).json({ error: errData.error?.message || errData.message || `HTTP error! status: ${response.status}` });
return;
}
if (stream) {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
if (response.body) {
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
res.write(value);
}
}
res.end();
} else {
const data = await response.json();
res.json(data);
}
} catch (error: any) {
console.error("Proxy error:", error);
if (!res.headersSent) {
res.status(500).json({ error: error.message || "Internal server error" });
} else {
res.end();
}
}
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), 'dist');
app.use(express.static(distPath));
app.get('*all', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();